Module: Supplejack::Controllers::Helpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/supplejack/controllers/helpers.rb

Instance Method Summary collapse

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.

Parameters:

  • record (Record)

    Class which includes the Supplejack::Record module

  • attribute (Symbol)

    The name of the attribute to return

  • options (Hash) (defaults to: {})

    Hash of options to customize the output, supported options: :label, :limit, :delimiter, :link_path, :tag

Options Hash (options):

  • :label (true, false)

    Display the attribute name

  • :label_tag (String)

    HTML tag to surround label

  • :label_class (String)

    CSS class to apply to label_tag

  • :limit (Integer)

    Number of charachters to truncate or number of values (when multivalue field)

  • :delimiter (Integer)

    Used to separate multi value attributes

  • :link_path (String)

    The method name of a routes path when provided it will generate links for every value

  • :tag (Symbol)

    HTML tag to wrap the label and value

  • :link (true, false, String)

    When true will try to make the value into a link, When is a string it will try to find a {value} within the string and replace it with the value.

  • :extra_html (String)

    HTML which will be included inside the tag at the end.

  • :tag_class (Symbol)

    The class for the attribute tag

Returns:

  • (String)

    A HTML snippet with the attribute name and value



43
44
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
# File 'lib/supplejack/controllers/helpers.rb', line 43

def attribute(record, attributes, options={})
  options.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 options[:limit] && options[:limit].to_i > 0
      value = value[0..(options[:limit].to_i-1)]
    end

    if options[:link_path]
      value = value.map {|v| link_to(v, send(options[:link_path], {i: {attribute => v}})) }
    end

    if options[:link]
      value = value.map do |v|
        attribute_link_replacement(v, options[:link])
      end
    end

    value = value.join(options[:delimiter]).html_safe
    value = truncate(value, length: options[:limit]) if options[:limit].to_i > 20
    value
  else
    if options[:limit] && options[:limit].to_i > 0
      value = truncate(value, length: options[:limit].to_i)
    end

    if options[:link]
      value = attribute_link_replacement(value, options[:link])
    end
  end

  content = ""
  if options[:label]
    if options[:trans_key].present?
      translation = I18n.t(options[: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 = (options[:label_tag], translation, class: options[:label_class]).html_safe
    content << "<br/>".html_safe unless options[:label_inline]
  end

  content << value.to_s
  content << options[:extra_html] if options[:extra_html]
  if value.present? and value != "Not specified"
    options[:tag] ? (options[:tag], content.html_safe, class: options[:tag_class]) : content.html_safe
  end
end


160
161
162
163
164
165
166
167
168
# File 'lib/supplejack/controllers/helpers.rb', line 160

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

Parameters:

  • search (Dnz::Search)

    A instance of the Dnz::Search class

  • options (Hash) (defaults to: {})

    Hash of options to remove any filter

Options Hash (options):

  • except (Array)

    A array of fields which should not generate a hidden field

Returns:

  • (String)

    A HTML snippet with hidden fields



181
182
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
# File 'lib/supplejack/controllers/helpers.rb', line 181

def form_fields(search, options={})
  if search
    tags = "".html_safe

    fields = [:record_type, :sort, :direction]
    fields.delete(:record_type) if search.record?

    if options[:except].try(:any?)
      fields.delete_if {|field| options[:except].include?(field)}
    end

    fields.each do |field|
      tags += 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) || options[: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| tags << hidden_field_tag(field_name, v) }
        end
      end
    end

    tags
  end
end

#generate_path(name, options = {}) ⇒ Object



325
326
327
328
329
330
331
332
# File 'lib/supplejack/controllers/helpers.rb', line 325

def generate_path(name, options={})
  segments = name.split(".")
  if segments.size == 1
    send("#{segments[0]}_path", options)
  elsif segments.size == 2
    send(segments[0]).send("#{segments[1]}_path", options)
  end
end

Returns a link with the existing search options and adds the specified facet and value

Parameters:

  • name (Symbol)

    The name of the facet. Ex: :category, :subject

  • value (String)

    The value withing the facet. Ex: “Wellington”, “Books”, etc..

  • path_name (String)

    The name of the path used to generate the url path. For example if you have a records_path route in your app, then specify “records” and it will call the records_path method

  • options (Hash) (defaults to: {})

    Set of options to customize the output

  • html_options (Hash) (defaults to: {})

    HTML options that are passed directly to the link_to method

Returns:

  • (String)

    A link to with the correct search options.



241
242
243
244
245
246
247
# File 'lib/supplejack/controllers/helpers.rb', line 241

def link_to_add_filter(name, value, path_name, options={}, html_options={}, &block)
  symbol = search.record_type == 0 ? :i : :h
  options[:except] = Util.array(options[:except]) + [:page]
  path = generate_path(path_name, search.options(plus: {symbol => {name => value}}, except: options[:except]))
  link_text =  options[:display_name].present? ? options[:display_name] : I18n.t("facets.values.#{value}", default: value)
  link_to block_given? ? capture(&block) : link_text, path.html_safe, html_options
end


249
250
251
252
253
254
# File 'lib/supplejack/controllers/helpers.rb', line 249

def link_to_lock_filter(name, value, path_name, options={}, html_options={}, &block)
  symbol = search.record_type == 0 ? :il : :hl
  path = generate_path(path_name, search.options(except: [{name => value}], plus: {symbol => {name => value}}))
  link_text = options[:display_name].present? ? options[:display_name] : I18n.t("facets.values.#{value}", default: value)
  link_to block_given? ? capture(&block) : link_text, path.html_safe, html_options
end

Provides a link to record landing page that is augmented with a persisted search object Used in results listings



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/supplejack/controllers/helpers.rb', line 265

def link_to_record(*args, &block)
  if block_given?
    name = capture(&block)
    url = args[0]
    search_options = args[1] || {}
    html_options = args[2] || {}
  else
    name = args[0]
    url = args[1]
    search_options = args[2] || {}
    html_options = args[3] || {}
  end
  url = url + "?" + {search: search_options}.to_query if search_options.try(:any?)
  link_to(name, url, html_options)
end

Returns a link with all existing search options except the specified in the params

Parameters:

  • name (Symbol)

    The name of the facet. Ex: :category, :subject

  • value (String)

    The value withing the facet. Ex: “Wellington”, “Books”, etc..

  • path_name (String)

    The name of the path used to generate the url path. For example if you have a records_path route in your app, then specify “records” and it will call the records_path method

  • options (Hash) (defaults to: {})

    Set of options to customize the output

  • html_options (Hash) (defaults to: {})

    HTML options that are passed directly to the link_to method

Returns:

  • (String)

    A link to with the correct search options.



223
224
225
226
227
# File 'lib/supplejack/controllers/helpers.rb', line 223

def link_to_remove_filter(name, value, path_name, options={}, html_options={}, &block)
  path = generate_path(path_name, search.options(except: [{name => value}, :page]))
  link_text = options[:display_name].present? ? options[:display_name] : I18n.t("facets.values.#{value}", default: value)
  link_to block_given? ? capture(&block) : link_text, path.html_safe, html_options
end

Displays the next and/or previous links based on the record and current search

Parameters:

  • options (Hash)

    a customizable set of options



128
129
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
# File 'lib/supplejack/controllers/helpers.rb', line 128

def next_previous_links(record, html_options={})
  html_options.reverse_merge!({prev_class: "prev", next_class: "next", wrapper_class: 'nav', prev_label: nil, next_label: nil})

  return "" unless params[:search]
  links = "".html_safe

  options = search.options

  previous_label = html_options[:prev_label] ||= t('supplejack_client.previous', default: "Previous")
  next_label = html_options[:next_label] ||= t('supplejack_client.next', default: "Next")
  previous_label = previous_label.html_safe
  next_label = next_label.html_safe

  options[:path] = params[:search][:path].gsub(/(\W|\d)/, '') if params[:search] && params[:search][:path]

  if record.previous_record
    options[:page] = record.previous_page if record.previous_page.to_i > 1
    links += link_to(raw(previous_label), record_path(record.previous_record, search: options), class: html_options[:prev_class]).html_safe
  else
    links += (:span, previous_label, class: html_options[:prev_class])
  end

  if record.next_record
    options[:page] = record.next_page if record.next_page.to_i > 1
    links += link_to(raw(next_label), record_path(record.next_record, search: options), class: html_options[:next_class]).html_safe
  else
    links += (:span, next_label, class: html_options[:next_class])
  end

  (:span, links, class: html_options[:wrapper_class])
end

#search(special_params = nil) ⇒ Object



15
16
17
18
19
# File 'lib/supplejack/controllers/helpers.rb', line 15

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