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.

Parameters:



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

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



100
101
102
103
104
105
106
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 100

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)


74
75
76
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 74

def constraint_label
  label
end

#field_labelObject



41
42
43
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 41

def field_label
  facet_field_presenter.label
end

#hitsObject



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

def hits
  return unless @facet_item.respond_to? :hits

  @facet_item.hits
end

#href(path_options = {}) ⇒ Object



86
87
88
89
90
91
92
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 86

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

#itemsObject



28
29
30
31
32
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 28

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)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 49

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



95
96
97
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 95

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)


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

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

#valueObject



78
79
80
81
82
83
84
# File 'app/presenters/blacklight/facet_item_presenter.rb', line 78

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