Skip to content

Commit 58b6bb2

Browse files
committed
add eds_pub_date facet
1 parent 9a72361 commit 58b6bb2

File tree

6 files changed

+70
-6
lines changed

6 files changed

+70
-6
lines changed

app/components/articles/response/facet_group_component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<%= label_tag("eds_limiter_#{limiter.id}") do %>
1414
<%= check_box(
1515
'f',
16-
'eds_search_limiters_facet',
16+
limiter.facet_field,
1717
{ multiple: true, id: "eds_limiter_#{limiter.id}", checked: limiter.selected? },
1818
limiter.label,
1919
nil

app/components/articles_range_limit_component.html.erb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33
<%= @facet_field.label %>
44
<% end %>
55
<% component.with_body do %>
6+
<% if month_facets.present? %>
7+
<div class="facet-options mt-1 ms-1" data-behavior='facet-options-checkboxes'>
8+
<ul class="checkbox-facet-list">
9+
<% month_facets.each do |limiter| %>
10+
<li data-behavior='facet-options-checkbox'>
11+
<%= label_tag("eds_limiter_#{limiter.id}") do %>
12+
<%= radio_button('f', limiter.facet_field,
13+
limiter.label,
14+
{ id: "eds_limiter_#{limiter.id}", checked: limiter.selected? })%>
15+
<%= link_to(limiter.search_url, data: { turbo: false }) do %>
16+
<%= limiter.label %>
17+
<% end %>
18+
<% end %>
19+
</li>
20+
<% end %>
21+
</ul>
22+
<div data-behavior='loading-spinner' class='loading-spinner' style='display:none'></div>
23+
</div>
24+
<% end %>
625
<% if @facet_field.selected_range_facet_item %>
726
<%= render BlacklightRangeLimit::RangeSegmentsComponent.new(facet_field: @facet_field, facet_items: [@facet_field.selected_range_facet_item], classes: ['current']) %>
827
<% end %>

app/components/articles_range_limit_component.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ class ArticlesRangeLimitComponent < BlacklightRangeLimit::RangeFacetComponent
44
def date_range
55
Eds::DateRangeParser.new(@facet_field.response, params, @facet_field.key)
66
end
7+
8+
def month_facets
9+
helpers.facet_options_presenter.month_facets
10+
end
711
end

app/controllers/articles_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def eds_init
175175
# Setting `if: false` for the limiters facet so the facet does not render as
176176
# a facet but we still can apploy our configured label to the breadcrumbs
177177
config.add_facet_field 'eds_search_limiters_facet', label: 'Settings', if: false
178+
config.add_facet_field 'eds_pub_date_facet', label: '', single: true, item_presenter: PubDatePresenter
178179
config.add_facet_field 'pub_year_tisim', label: 'Date', component: ArticlesRangeLimitComponent, range: true
179180
config.add_facet_field 'eds_publication_type_facet', label: 'Source type', component: Articles::Response::AdditionalSelectionsFacetComponent
180181
config.add_facet_field 'eds_language_facet', label: 'Language', component: Articles::Response::LimitedFacetFieldListComponent

app/presenters/facet_options_presenter.rb

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ def initialize(params:, context:)
66
@context = context
77
end
88

9+
def month_facets
10+
(0..2).map do |i|
11+
date = Time.zone.today << i
12+
PubDateLimiter.new({
13+
'Id' => 'DT1',
14+
'SearchField' => "#{date.strftime('%Y-%m')}/#{Time.zone.today.strftime('%Y-%m')}",
15+
'Label' => "Since #{date.strftime('%B %Y')}"
16+
}, params, context)
17+
end
18+
end
19+
920
def limiters
1021
@limiters ||= available_limiters.map do |limiter|
1122
Limiter.new(limiter, params, context)
@@ -34,7 +45,6 @@ def eds_params
3445
end
3546

3647
class Limiter
37-
FACET_FIELD = 'eds_search_limiters_facet'
3848
delegate :facet_item_presenter, :search_action_path, :search_state, :facet_configuration_for_field, to: :context
3949

4050
def initialize(limiter, params, context)
@@ -43,6 +53,10 @@ def initialize(limiter, params, context)
4353
@context = context
4454
end
4555

56+
def facet_field
57+
'eds_search_limiters_facet'
58+
end
59+
4660
def id
4761
limiter['Id']
4862
end
@@ -51,6 +65,10 @@ def label
5165
limiter['Label']
5266
end
5367

68+
def search_value
69+
label
70+
end
71+
5472
def type
5573
limiter['Type']
5674
end
@@ -64,23 +82,33 @@ def enabled_by_default?
6482
end
6583

6684
def selected?
67-
search_state.filter(config).include?(label)
85+
search_state.filter(config).include?(search_value)
6886
end
6987

7088
def search_url
7189
if selected?
72-
search_action_path(search_state.filter(FACET_FIELD).remove(label).to_h)
90+
search_action_path(search_state.filter(facet_field).remove(search_value).to_h)
7391
else
74-
(config.item_presenter || Blacklight::FacetItemPresenter).new(label, config, context, FACET_FIELD).href
92+
(config.item_presenter || Blacklight::FacetItemPresenter).new(search_value, config, context, facet_field).href
7593
end
7694
end
7795

7896
def config
79-
facet_configuration_for_field(FACET_FIELD)
97+
facet_configuration_for_field(facet_field)
8098
end
8199

82100
private
83101

84102
attr_reader :limiter, :params, :context
85103
end
104+
105+
class PubDateLimiter < Limiter
106+
def facet_field
107+
'eds_pub_date_facet'
108+
end
109+
110+
def search_value
111+
limiter['SearchField']
112+
end
113+
end
86114
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
# eds_pub_date_facet value is YYYY-MM/YYYY-MM we want to display the human readable value
4+
# i.e. Since May 2025
5+
class PubDatePresenter < Blacklight::FacetItemPresenter
6+
def constraint_label
7+
formatted_date = Date.strptime(value, "%Y-%m").strftime("%B %Y")
8+
"Since #{formatted_date}"
9+
rescue StandardError
10+
value
11+
end
12+
end

0 commit comments

Comments
 (0)