Class: Blacklight::FacetItemPresenter

Inherits:
Object
  • Object
show all
Defined in:
app/presenters/blacklight/facet_item_presenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(facet_item, facet_config, view_context, facet_field, search_state = view_context.search_state) ⇒ FacetItemPresenter

Returns a new instance of FacetItemPresenter.



9
10
11
12
13
14
15
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 9

def initialize(facet_item, facet_config, view_context, facet_field, search_state = view_context.search_state)
  @facet_item = facet_item
  @facet_config = facet_config
  @view_context = view_context
  @facet_field = facet_field
  @search_state = search_state
end

Instance Attribute Details

#facet_configObject (readonly)

Returns the value of attribute facet_config.



5
6
7
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 5

def facet_config
  @facet_config
end

#facet_fieldObject (readonly)

Returns the value of attribute facet_field.



5
6
7
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 5

def facet_field
  @facet_field
end

#facet_itemObject (readonly)

Returns the value of attribute facet_item.



5
6
7
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 5

def facet_item
  @facet_item
end

#search_stateObject (readonly)

Returns the value of attribute search_state.



5
6
7
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 5

def search_state
  @search_state
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



5
6
7
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 5

def view_context
  @view_context
end

Instance Method Details

#add_href(path_options = {}) ⇒ Object



95
96
97
98
99
100
101
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 95

def add_href(path_options = {})
  if facet_config.url_method
    view_context.public_send(facet_config.url_method, facet_config.key, facet_item)
  else
    view_context.search_action_path(search_state.add_facet_params_and_redirect(facet_config.key, facet_item).merge(path_options))
  end
end

#constraint_labelString

Get the displayable version of the facet’s value for use in e.g. the constraints widget

Returns:

  • (String)


69
70
71
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 69

def constraint_label
  label
end

#field_labelObject



36
37
38
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 36

def field_label
  facet_field_presenter.label
end

#hitsObject



17
18
19
20
21
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 17

def hits
  return unless @facet_item.respond_to? :hits

  @facet_item.hits
end

#href(path_options = {}) ⇒ Object



81
82
83
84
85
86
87
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 81

def href(path_options = {})
  if selected?
    remove_href
  else
    add_href(path_options)
  end
end

#itemsObject



23
24
25
26
27
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 23

def items
  return unless @facet_item.respond_to? :items

  @facet_item.items
end

#labelString

Get the displayable version of a facet’s value

Returns:

  • (String)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 44

def label
  label_value = if facet_item.respond_to? :label
                  facet_item.label
                else
                  value
                end

  if facet_config.helper_method
    view_context.public_send(facet_config.helper_method, label_value)
  elsif facet_config.query && facet_config.query[label_value]
    facet_config.query[label_value][:label]
  elsif value == Blacklight::SearchState::FilterField::MISSING
    I18n.t("blacklight.search.facets.missing")
  elsif facet_config.date
    localization_options = facet_config.date == true ? {} : facet_config.date
    I18n.l(Time.zone.parse(label_value), **localization_options)
  else
    label_value.to_s
  end
end

#remove_href(path = search_state) ⇒ Object



90
91
92
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 90

def remove_href(path = search_state)
  view_context.search_action_path(path.filter(facet_config.key).remove(facet_item))
end

#selected?Boolean

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

Returns:

  • (Boolean)


32
33
34
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 32

def selected?
  search_state.filter(facet_config).include?(value)
end

#valueObject



73
74
75
76
77
78
79
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 73

def value
  if facet_item.respond_to? :value
    facet_item.value
  else
    facet_item
  end
end