Module: Blacklight::FacetsHelperBehavior
- Includes:
- Facet
- Included in:
- FacetsHelper
- Defined in:
- app/helpers/blacklight/facets_helper_behavior.rb
Instance Method Summary collapse
-
#facet_display_value(field, item) ⇒ String
Get the displayable version of a facet's value.
- #facet_field_id(facet_field) ⇒ Object
-
#facet_field_in_params?(field) ⇒ Boolean
Are any facet restrictions for a field in the query parameters?.
-
#facet_in_params?(field, item) ⇒ Boolean
Check if the query parameters have the given facet field with the given value.
-
#facet_params(field) ⇒ Object
Get the values of the facet set in the blacklight query string.
-
#facet_partial_name(display_facet = nil) ⇒ String
The name of the partial to use to render a facet field.
-
#has_facet_values?(fields = facet_field_names, options = {}) ⇒ Boolean
Check if any of the given fields have values.
-
#path_for_facet(facet_field, item) ⇒ String
Where should this facet link to?.
-
#render_facet_count(num, options = {}) ⇒ String
Renders a count value for facet limits.
-
#render_facet_item(facet_field, item) ⇒ Object
Renders a single facet item.
-
#render_facet_limit(display_facet, options = {}) ⇒ String
Renders a single section for facet limit with a specified solr field used for faceting.
-
#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.
-
#render_facet_partials(fields = facet_field_names, options = {}) ⇒ Object
Render a collection of facet fields.
-
#render_facet_value(facet_field, item, options = {}) ⇒ String
Standard display of a facet value in a list.
-
#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).
-
#should_collapse_facet?(facet_field) ⇒ Boolean
Determine whether a facet should be rendered as collapsed or not.
-
#should_render_facet?(display_facet) ⇒ Boolean
Determine if Blacklight should render the display_facet or not.
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
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 = facet_config.date == true ? {} : facet_config.date l(value.to_datetime, ) 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?
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.
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'
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
11 12 13 |
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 11 def has_facet_values? fields = facet_field_names, = {} 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?
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.
167 168 169 170 |
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 167 def render_facet_count(num, = {}) classes = ([:classes] || []) << "facet-count" content_tag("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.
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, = {}) return unless should_render_facet?(display_facet) = .dup [:partial] ||= facet_partial_name(display_facet) [:layout] ||= "facet_layout" unless .key?(:layout) [:locals] ||= {} [:locals][:field_name] ||= display_facet.name [:locals][:solr_field] ||= display_facet.name # deprecated [:locals][:facet_field] ||= facet_configuration_for_field(display_facet.name) [:locals][:display_facet] ||= display_facet render() 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| content_tag(wrapping_element,item)}) end |
#render_facet_partials(fields = facet_field_names, options = {}) ⇒ Object
Render a collection of facet fields.
22 23 24 25 26 |
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 22 def render_facet_partials fields = facet_field_names, = {} safe_join(facets_from_request(fields).map do |display_facet| render_facet_limit(display_facet, ) 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.
121 122 123 124 125 126 |
# File 'app/helpers/blacklight/facets_helper_behavior.rb', line 121 def render_facet_value(facet_field, item, ={}) path = path_for_facet(facet_field, item) content_tag(:span, :class => "facet-label") do link_to_unless([: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)
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)) content_tag(:span, class: "facet-label") do content_tag(:span, facet_display_value(facet_field, item), class: "selected") + # remove link link_to(remove_href, class: "remove") do content_tag(:span, '', class: "glyphicon glyphicon-remove") + content_tag(: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
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.
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 |