Module: Blacklight::FacetsHelperBehavior

Includes:
Facet
Included in:
FacetsHelper
Defined in:
app/helpers/blacklight/facets_helper_behavior.rb

Instance Method Summary collapse

Methods included from Facet

#facet_by_field_name, #facet_field_names, #facet_paginator, #facets_from_request

Instance Method Details

#facet_display_value(field, item) ⇒ String

Get the displayable version of a facet's value

Parameters:

  • field (Object)
  • item (String)

    value

Returns:

  • (String)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 208

def facet_display_value field, item
  facet_config = facet_configuration_for_field(field)
  
  value = if item.respond_to? :label
    item.label
  else
    facet_value_for_facet_item(item)
  end

  if facet_config.helper_method
    send facet_config.helper_method, value
  elsif facet_config.query && facet_config.query[value]
    facet_config.query[value][:label]
  elsif facet_config.date
    localization_options = facet_config.date == true ? {} : facet_config.date

    l(value.to_datetime, localization_options)
  else
    value
  end
end

#facet_field_id(facet_field) ⇒ Object



230
231
232
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 230

def facet_field_id facet_field
  "facet-#{facet_field.key.parameterize}"
end

#facet_field_in_params?(field) ⇒ Boolean

Are any facet restrictions for a field in the query parameters?

Parameters:

  • field (String)

Returns:

  • (Boolean)


177
178
179
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 177

def facet_field_in_params? field
  !facet_params(field).blank?
end

#facet_in_params?(field, item) ⇒ Boolean

Check if the query parameters have the given facet field with the given value.

Parameters:

  • field (Object)
  • item (Object)

    facet value

Returns:

  • (Boolean)


188
189
190
191
192
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 188

def facet_in_params?(field, item)
  value = facet_value_for_facet_item(item)

  (facet_params(field) || []).include? value
end

#facet_params(field) ⇒ Object

Get the values of the facet set in the blacklight query string



196
197
198
199
200
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 196

def facet_params field
  config = facet_configuration_for_field(field)

  params[:f][config.key] if params[:f]
end

#facet_partial_name(display_facet = nil) ⇒ String

The name of the partial to use to render a facet field. uses the value of the “partial” field if set in the facet configuration otherwise uses “facet_pivot” if this facet is a pivot facet defaults to 'facet_limit'

Returns:

  • (String)


104
105
106
107
108
109
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 104

def facet_partial_name(display_facet = nil)
  config = facet_configuration_for_field(display_facet.name)
  name = config.try(:partial)
  name ||= "facet_pivot" if config.pivot
  name ||= "facet_limit"
end

#has_facet_values?(fields = facet_field_names, options = {}) ⇒ Boolean

Check if any of the given fields have values

Parameters:

  • fields (Array<String>) (defaults to: facet_field_names)
  • options (Hash) (defaults to: {})

Returns:

  • (Boolean)


11
12
13
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 11

def has_facet_values? fields = facet_field_names, options = {}
  facets_from_request(fields).any? { |display_facet| !display_facet.items.empty? && should_render_facet?(display_facet) }
end

#path_for_facet(facet_field, item) ⇒ String

Where should this facet link to?

Parameters:

Returns:

  • (String)


133
134
135
136
137
138
139
140
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 133

def path_for_facet(facet_field, item)
  facet_config = facet_configuration_for_field(facet_field)
  if facet_config.url_method
    send(facet_config.url_method, facet_field, item)
  else
    search_action_path(search_state.add_facet_params_and_redirect(facet_field, item))
  end
end

#render_facet_count(num, options = {}) ⇒ String

Renders a count value for facet limits. Can be over-ridden locally to change style. And can be called by plugins to get consistent display.

Parameters:

  • num (Integer)

    number of facet results

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

Options Hash (options):

  • an (Array<String>)

    array of classes to add to count span.

Returns:

  • (String)


167
168
169
170
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 167

def render_facet_count(num, options = {})
  classes = (options[:classes] || []) << "facet-count"
  ("span", t('blacklight.search.facets.count', :number => number_with_delimiter(num)), :class => classes)
end

#render_facet_item(facet_field, item) ⇒ Object

Renders a single facet item



63
64
65
66
67
68
69
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 63

def render_facet_item(facet_field, item)
  if facet_in_params?(facet_field, item.value )
    render_selected_facet_value(facet_field, item)          
  else
    render_facet_value(facet_field, item)
  end
end

#render_facet_limit(display_facet, options = {}) ⇒ String

Renders a single section for facet limit with a specified solr field used for faceting. Can be over-ridden for custom display on a per-facet basis.

Parameters:

Options Hash (options):

  • :partial (String)

    partial to render

  • :layout (String)

    partial layout to render

  • :locals (Hash)

    locals to pass to the partial

Returns:

  • (String)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 39

def render_facet_limit(display_facet, options = {})
  return unless should_render_facet?(display_facet)
  options = options.dup
  options[:partial] ||= facet_partial_name(display_facet)
  options[:layout] ||= "facet_layout" unless options.key?(:layout)
  options[:locals] ||= {}
  options[:locals][:field_name] ||= display_facet.name
  options[:locals][:solr_field] ||= display_facet.name # deprecated
  options[:locals][:facet_field] ||= facet_configuration_for_field(display_facet.name)
  options[:locals][:display_facet] ||= display_facet 

  render(options)
end

#render_facet_limit_list(paginator, facet_field, wrapping_element = :li) ⇒ Object

Renders the list of values removes any elements where render_facet_item returns a nil value. This enables an application to filter undesireable facet items so they don't appear in the UI



57
58
59
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 57

def render_facet_limit_list(paginator, facet_field, wrapping_element=:li)
  safe_join(paginator.items.map { |item| render_facet_item(facet_field, item) }.compact.map { |item| (wrapping_element,item)})
end

#render_facet_partials(fields = facet_field_names, options = {}) ⇒ Object

Render a collection of facet fields.

Parameters:

  • fields (Array<String>) (defaults to: facet_field_names)
  • options (Hash) (defaults to: {})

Returns:

  • String

See Also:



22
23
24
25
26
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 22

def render_facet_partials fields = facet_field_names, options = {}
  safe_join(facets_from_request(fields).map do |display_facet|
    render_facet_limit(display_facet, options)
  end.compact, "\n")
end

#render_facet_value(facet_field, item, options = {}) ⇒ String

Standard display of a facet value in a list. Used in both _facets sidebar partial and catalog/facet expanded list. Will output facet value name as a link to add that to your restrictions, with count in parens.

Parameters:

Options Hash (options):

  • :suppress_link (Boolean)

    display the facet, but don't link to it

Returns:

  • (String)


121
122
123
124
125
126
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 121

def render_facet_value(facet_field, item, options ={})
  path = path_for_facet(facet_field, item)
  (:span, :class => "facet-label") do
    link_to_unless(options[:suppress_link], facet_display_value(facet_field, item), path, :class=>"facet_select")
  end + render_facet_count(item.hits)
end

#render_selected_facet_value(facet_field, item) ⇒ Object

Standard display of a SELECTED facet value (e.g. without a link and with a remove button)

Parameters:

See Also:



147
148
149
150
151
152
153
154
155
156
157
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 147

def render_selected_facet_value(facet_field, item)
  remove_href = search_action_path(search_state.remove_facet_params(facet_field, item))
  (:span, class: "facet-label") do
    (:span, facet_display_value(facet_field, item), class: "selected") +
    # remove link
    link_to(remove_href, class: "remove") do
      (:span, '', class: "glyphicon glyphicon-remove") +
      (:span, '[remove]', class: 'sr-only')
    end
  end + render_facet_count(item.hits, :classes => ["selected"])
end

#should_collapse_facet?(facet_field) ⇒ Boolean

Determine whether a facet should be rendered as collapsed or not.

- if the facet is 'active', don't collapse
- if the facet is configured to collapse (the default), collapse
- if the facet is configured not to collapse, don't collapse

Parameters:

Returns:

  • (Boolean)


93
94
95
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 93

def should_collapse_facet? facet_field
  !facet_field_in_params?(facet_field.key) && facet_field.collapse
end

#should_render_facet?(display_facet) ⇒ Boolean

Determine if Blacklight should render the display_facet or not

By default, only render facets with items.

Parameters:

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 78

def should_render_facet? display_facet
  # display when show is nil or true
  facet_config = facet_configuration_for_field(display_facet.name)
  display = should_render_field?(facet_config, display_facet)
  display && display_facet.items.present?
end