Module: Supplejack::Controllers::Helpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/supplejack/controllers/helpers.rb
Instance Method Summary collapse
-
#attribute(record, attributes, options = {}) ⇒ String
Displays a record attribute with a label and allows you to customize the HTML markup generated.
- #attribute_link_replacement(value, link_pattern) ⇒ Object
-
#form_fields(search, options = {}) ⇒ String
Generates hidden fields with all the filters of a search object It is used in forms, so that when a user enteres another term in the search box the state of the search is preserved.
- #generate_path(name, options = {}) ⇒ Object
-
#link_to_add_filter(name, value, path_name, options = {}, html_options = {}, &block) ⇒ String
Returns a link with the existing search options and adds the specified facet and value.
- #link_to_lock_filter(name, value, path_name, options = {}, html_options = {}, &block) ⇒ Object
-
#link_to_record(*args, &block) ⇒ Object
Provides a link to record landing page that is augmented with a persisted search object Used in results listings.
-
#link_to_remove_filter(name, value, path_name, options = {}, html_options = {}, &block) ⇒ String
Returns a link with all existing search options except the specified in the params.
-
#next_previous_links(record, html_options = {}) ⇒ Object
Displays the next and/or previous links based on the record and current search.
- #search(special_params = nil) ⇒ Object
Instance Method Details
#attribute(record, attributes, options = {}) ⇒ String
Displays a record attribute with a label and allows you to customize the HTML markup generated. It will not display anything if the value is blank or nil.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/supplejack/controllers/helpers.rb', line 45 def attribute(record, attributes, ={}) .reverse_merge!(label: true, label_inline: true, limit: nil, delimiter: ", ", link_path: false, tag: Supplejack.attribute_tag, label_tag: Supplejack.label_tag, label_class: Supplejack.label_class, trans_key: nil, link: false, extra_html: nil, tag_class: nil) value = [] attributes = [attributes] unless attributes.is_a?(Array) attributes.each do |attribute| if attribute.is_a?(String) && attribute.match(/\./) object, attr = attribute.split(".") v = record.try(object.to_sym).try(attr.to_sym) if object && attr else v = record.send(attribute) end if v.is_a?(Array) value += v.compact else value << v if v.present? end end value = value.first if value.size == 1 attribute = attributes.first if value.is_a?(Array) if [:limit] && [:limit].to_i > 0 value = value[0..([:limit].to_i-1)] end if [:link_path] value = value.map {|v| link_to(v, send([:link_path], {i: {attribute => v}})) } end if [:link] value = value.map do |v| attribute_link_replacement(v, [:link]) end end value = value.join([:delimiter]).html_safe value = truncate(value, length: [:limit]) if [:limit].to_i > 20 value else if [:limit] && [:limit].to_i > 0 value = truncate(value, length: [:limit].to_i) end if [:link] value = attribute_link_replacement(value, [:link]) end end content = "" if [:label] if [:trans_key].present? translation = I18n.t([:trans_key], default: attribute.to_s.capitalize) + ": " else i18n_class_name = record.class.to_s.tableize.downcase.gsub(/\//, "_") translation = "#{I18n.t("#{i18n_class_name}.#{attribute}", default: attribute.to_s.capitalize)}: " end content = content_tag([:label_tag], translation, class: [:label_class]).html_safe content << "<br/>".html_safe unless [:label_inline] end content << value.to_s content << [:extra_html] if [:extra_html] if value.present? and value != "Not specified" [:tag] ? content_tag([:tag], content.html_safe, class: [:tag_class]) : content.html_safe end end |
#attribute_link_replacement(value, link_pattern) ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'lib/supplejack/controllers/helpers.rb', line 162 def attribute_link_replacement(value, link_pattern) if link_pattern.is_a?(String) link_pattern = URI.decode(link_pattern) url = link_pattern.gsub("{{value}}", value) link_to(value, url) else auto_link value end end |
#form_fields(search, options = {}) ⇒ String
Generates hidden fields with all the filters of a search object It is used in forms, so that when a user enteres another term in the search box the state of the search is preserved
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/supplejack/controllers/helpers.rb', line 183 def form_fields(search, ={}) if search = "".html_safe fields = [:record_type, :sort, :direction] fields.delete(:record_type) if search.record? if [:except].try(:any?) fields.delete_if {|field| [:except].include?(field)} end fields.each do |field| += hidden_field_tag(field.to_s, search.send(field)) unless search.send(field).blank? end {i: :i_unlocked, il: :i_locked, h: :h_unlocked, hl: :h_locked}.each_pair do |symbol, instance_name| if Supplejack.sticky_facets || [:il, :hl].include?(symbol) || [:all_filters] filters = search.url_format.send(instance_name) rescue {} filters.each do |name, value| field_name = value.is_a?(Array) ? "#{symbol.to_s}[#{name.to_s}][]" : "#{symbol.to_s}[#{name.to_s}]" values = *value values.each {|v| << hidden_field_tag(field_name, v) } end end end end end |
#generate_path(name, options = {}) ⇒ Object
327 328 329 330 331 332 333 334 |
# File 'lib/supplejack/controllers/helpers.rb', line 327 def generate_path(name, ={}) segments = name.split(".") if segments.size == 1 send("#{segments[0]}_path", ) elsif segments.size == 2 send(segments[0]).send("#{segments[1]}_path", ) end end |
#link_to_add_filter(name, value, path_name, options = {}, html_options = {}, &block) ⇒ String
Returns a link with the existing search options and adds the specified facet and value
243 244 245 246 247 248 249 |
# File 'lib/supplejack/controllers/helpers.rb', line 243 def link_to_add_filter(name, value, path_name, ={}, ={}, &block) symbol = search.record_type == 0 ? :i : :h [:except] = Util.array([:except]) + [:page] path = generate_path(path_name, search.(plus: {symbol => {name => value}}, except: [:except])) link_text = [:display_name].present? ? [:display_name] : I18n.t("facets.values.#{value}", default: value) link_to block_given? ? capture(&block) : link_text, path.html_safe, end |
#link_to_lock_filter(name, value, path_name, options = {}, html_options = {}, &block) ⇒ Object
251 252 253 254 255 256 |
# File 'lib/supplejack/controllers/helpers.rb', line 251 def link_to_lock_filter(name, value, path_name, ={}, ={}, &block) symbol = search.record_type == 0 ? :il : :hl path = generate_path(path_name, search.(except: [{name => value}], plus: {symbol => {name => value}})) link_text = [:display_name].present? ? [:display_name] : I18n.t("facets.values.#{value}", default: value) link_to block_given? ? capture(&block) : link_text, path.html_safe, end |
#link_to_record(*args, &block) ⇒ Object
Provides a link to record landing page that is augmented with a persisted search object Used in results listings
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/supplejack/controllers/helpers.rb', line 267 def link_to_record(*args, &block) if block_given? name = capture(&block) url = args[0] = args[1] || {} = args[2] || {} else name = args[0] url = args[1] = args[2] || {} = args[3] || {} end url = url + "?" + {search: }.to_query if .try(:any?) link_to(name, url, ) end |
#link_to_remove_filter(name, value, path_name, options = {}, html_options = {}, &block) ⇒ String
Returns a link with all existing search options except the specified in the params
225 226 227 228 229 |
# File 'lib/supplejack/controllers/helpers.rb', line 225 def link_to_remove_filter(name, value, path_name, ={}, ={}, &block) path = generate_path(path_name, search.(except: [{name => value}, :page])) link_text = [:display_name].present? ? [:display_name] : I18n.t("facets.values.#{value}", default: value) link_to block_given? ? capture(&block) : link_text, path.html_safe, end |
#next_previous_links(record, html_options = {}) ⇒ Object
Displays the next and/or previous links based on the record and current search
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/supplejack/controllers/helpers.rb', line 130 def next_previous_links(record, ={}) .reverse_merge!({prev_class: "prev", next_class: "next", wrapper_class: 'nav', prev_label: nil, next_label: nil}) return "" unless params[:search] links = "".html_safe = search. previous_label = [:prev_label] ||= t('supplejack_client.previous', default: "Previous") next_label = [:next_label] ||= t('supplejack_client.next', default: "Next") previous_label = previous_label.html_safe next_label = next_label.html_safe [:path] = params[:search][:path].gsub(/(\W|\d)/, '') if params[:search] && params[:search][:path] if record.previous_record [:page] = record.previous_page if record.previous_page.to_i > 1 links += link_to(raw(previous_label), record_path(record.previous_record, search: ), class: [:prev_class]).html_safe else links += content_tag(:span, previous_label, class: [:prev_class]) end if record.next_record [:page] = record.next_page if record.next_page.to_i > 1 links += link_to(raw(next_label), record_path(record.next_record, search: ), class: [:next_class]).html_safe else links += content_tag(:span, next_label, class: [:next_class]) end content_tag(:span, links, class: [:wrapper_class]) end |
#search(special_params = nil) ⇒ Object
17 18 19 20 21 |
# File 'lib/supplejack/controllers/helpers.rb', line 17 def search(special_params=nil) return @supplejack_search if @supplejack_search klass = Supplejack.search_klass ? Supplejack.search_klass.classify.constantize : Supplejack::Search @supplejack_search = klass.new(special_params || params[:search] || params) end |