Skip to content

Commit

Permalink
- Add new helper method to remove (currently only) the year filter wh…
Browse files Browse the repository at this point in the history
…en adding a new filter. This is used to allow drill-down behaviour for the date facets.

- Add new helper methods to assess date spread of results, allowing year-month filters to be displayed automatically if the data is already limited to within a single year.
- De-dupe applied search filters before displaying them
  • Loading branch information
j-corry committed Sep 17, 2024
1 parent a541493 commit 29d76eb
Show file tree
Hide file tree
Showing 9 changed files with 862 additions and 420 deletions.
6 changes: 6 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ def apply_filter_url(params, filter_name, filter_value)
params.except(:page).merge(filter: params.dig(:filter).nil? ? { filter_name => [filter_value] } : params.dig(:filter).merge(filter_name => [filter_value, params.dig(:filter, filter_name)].compact.flatten))
end

def replace_filter_url(params, filter_name, filter_value)
# used for year-month filters; performs the additional step of removing year filters first
adjusted_params = params.except(:filter, :year)
apply_filter_url(adjusted_params, filter_name, filter_value)
end

def year_filter
params.dig(:filter, :year)
end
Expand Down
26 changes: 19 additions & 7 deletions app/models/search_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,17 @@ def facet_ses_ids
end

def years
# extract years from returned facet data
year_buckets = search&.dig(:data, "facets", "year", "buckets")
return if year_buckets.blank?
return [] if year_buckets.blank?

year_buckets.sort_by { |h| h["val"] }.uniq.reverse
end

def months
# extract months from returned facet data
month_buckets = search.dig(:data, "facets", "month", "buckets")
return if month_buckets.blank?
def months(year_string)
month_buckets = search&.dig(:data, "facets", "month", "buckets")
return [] if month_buckets.blank?

month_buckets.sort_by { |h| h["val"] }.uniq
month_buckets.select { |m| m['val'].first(4) == year_string }.sort_by { |h| h["val"] }.uniq
end

def hierarchy_data
Expand Down Expand Up @@ -273,4 +271,18 @@ def session_facets
def sort_facets(facet_field)
facet_field.sort_by { |h| h["count"] }.reverse
end

def single_data_year?
data_years.size == 1
end

def data_years
return [] unless search

date_facet = search.dig(:data, 'facets', 'date_dt')

return [] if date_facet.blank?

date_facet.dig("buckets").map { |b| b.dig("val").to_date.strftime("%Y") }.uniq
end
end
11 changes: 10 additions & 1 deletion app/models/solr_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,21 @@ def facet_hash
ret['year'] = {
"type": "range",
"field": "date_dt",
"start": "1500-01-01T00:00:00Z",
# "start": "1500-01-01T00:00:00Z",
"end": "#{Date.today.strftime("%Y-%m-%d")}T23:59:59Z",
"gap": "+1YEAR",
"mincount": 1,
"limit": 100
}
ret['month'] = {
"type": "range",
"field": "date_dt",
# "start": "1500-01-01T00:00:00Z",
"end": "#{Date.today.strftime("%Y-%m-%d")}T23:59:59Z",
"gap": "+1MONTH",
"mincount": 0,
"domain": { excludeTags: 'month' }
}
end

SolrSearch.sessions.each do |session|
Expand Down
2 changes: 1 addition & 1 deletion app/views/search/_applied_filters.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% unless request.params.dig(:filter).blank? %>
<section class="applied-filters">
<div class="applied-filter-container">
<% request.params.dig(:filter).sort.each do |filter| %>
<% request.params.dig(:filter).sort.uniq.each do |filter| %>
<% filter.last.sort.each do |filter_value| %>
<div class="filter-wrapper">
<div class="filter">
Expand Down
21 changes: 19 additions & 2 deletions app/views/search/results/facets/_date_facet.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,26 @@
<strong>Date</strong>
</div>
<div class="facets">
<% @search_data.months.each_with_index do |month, index| %>
<% @search_data.months(year).each_with_index do |month, index| %>
<div>
<%= link_to("#{month['val'].to_date.strftime("%B")} #{year} (#{month['count']})", url_for(apply_filter_url(request.params, "month", year + "-#{index + 1}")), class: "menu-link modifiable-link") %>
<%= link_to_unless(request.params.dig(:filter, "month")&.include?(month['val'].to_date.strftime("%Y-%-m")), "#{month['val'].to_date.strftime("%B")} #{year} (#{month['count']})", url_for(replace_filter_url(request.params, "month", year + "-#{index + 1}")), class: "menu-link modifiable-link") %>
</div>
<% end %>
</div>
</div>
</section>
<% end %>
<% elsif @search_data.single_data_year? %>
<% @search_data.data_years.each do |year| %>
<section class="facet">
<div>
<div>
<strong>Date</strong>
</div>
<div class="facets">
<% @search_data.months(year).each_with_index do |month, index| %>
<div>
<%= link_to_unless(request.params.dig(:filter, "month")&.include?(month['val'].to_date.strftime("%Y-%-m")), "#{month['val'].to_date.strftime("%B")} #{year} (#{month['count']})", url_for(replace_filter_url(request.params, "month", year + "-#{index + 1}")), class: "menu-link modifiable-link") %>
</div>
<% end %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion coverage/.last_run.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"result": {
"line": 81.33
"line": 81.51
}
}
Loading

0 comments on commit 29d76eb

Please sign in to comment.