Skip to content

Commit

Permalink
move start date filter to lib/facets.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanVanVugt committed Aug 3, 2023
1 parent 0c5d243 commit b25e712
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
23 changes: 4 additions & 19 deletions app/models/concerns/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,11 @@ def search_and_filter(user, search_params = '', selected_facets = {}, page: 1, s
any do
# Set all facets
normal_facets.each do |facet_title, facet_value|
# Start date is a AND search parameter in my opinion.
if facet_title == 'start'
any do # Conjunction clause
# Add to array that get executed lower down
active_facets[facet_title] ||= []
lb, ub = Facets.process(facet_title, facet_value)
# We should think about timezone handling here
if lb && ub
active_facets[facet_title] << with(facet_title).between(lb..ub)
elsif lb
active_facets[facet_title] << with(facet_title).greater_than_or_equal_to(lb)
elsif ub
active_facets[facet_title] << with(facet_title).less_than_or_equal_to(ub)
else
end
else
any do # Conjunction clause
# Add to array that get executed lower down
active_facets[facet_title] ||= []
val = Facets.process(facet_title, facet_value)
active_facets[facet_title] << with(facet_title, val)
end
val = Facets.process(facet_title, facet_value)
active_facets[facet_title] << with(facet_title, val)
end
end
end
Expand Down
13 changes: 7 additions & 6 deletions app/views/search/common/_facet_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ Variable that should be available

<% boolean_facets, regular_facets = available_facets(resources).partition { |f| f.field_name == :online } %>
<% regular_facets.each do |facet| %>
<% if facet.field_name == :start %>
<%= render partial: 'search/common/facet_sidebar_date_filter', locals: { facet: facet, resources: resources } %>
<% else %>
<%= render partial: 'search/common/facet_sidebar_filter', locals: { facet: facet, resources: resources } %>
<% end %>
<% if resource_type.name == 'Event' %>
<%= render partial: 'search/common/facet_sidebar_date_filter', locals: { facet_field: 'start' } %>
<% end %>
<%# We ignore the start facet, since this needs to be always available, not just when there are records%>
<% regular_facets.reject{ |f| f.field_name.to_s == 'start' }.each do |facet| %>
<%= render partial: 'search/common/facet_sidebar_filter', locals: { facet: facet, resources: resources } %>
<% end %>
<% boolean_facets.each do |facet| %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<% facet_field = facet.field_name.to_s %>
<% lb, ub = params[facet_field]&.split('/') %>
<li class="sidebar-group mt-4">
<ul>
Expand Down
17 changes: 16 additions & 1 deletion lib/facets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Facets
days_since_scrape: -> (c) { c.method_defined?(:last_scraped) },
elixir: -> (c) { ['Event', 'Material', 'ContentProvider'].include?(c.name) },
max_age: -> (c) { ['Event', 'Material'].include?(c.name) },
start: -> (c) { c.name == 'Event' },
include_hidden: -> (c) { c.method_defined?(:user_requires_approval?) }
}.with_indifferent_access.freeze

Expand All @@ -13,7 +14,7 @@ module Facets
include_expired: -> (value) { value == 'true'},
include_archived: -> (value) { value == 'true'},
max_age: -> (value) { Subscription::FREQUENCY.detect { |f| f[:title] == value }.try(:[], :period) },
start: -> (value) { value.split('/').map {|d| Date.parse(d) rescue nil } },
start: -> (value) { value&.split('/')&.map {|d| Date.parse(d) rescue nil } },
include_hidden: -> (value) { value == 'true'}
}

Expand Down Expand Up @@ -41,6 +42,20 @@ def max_age(scope, age, _)
end
end

def start(scope, bounds, _)
lb, ub = bounds

sunspot_scoped(scope) do
if lb && ub
with(:start).between(lb..ub)
elsif lb
with(:start).greater_than_or_equal_to(lb)
elsif ub
with(:start).less_than_or_equal_to(ub)
end
end
end

def include_expired(scope, value, _)
sunspot_scoped(scope) { with('end').greater_than(Time.zone.now) } unless value
end
Expand Down

0 comments on commit b25e712

Please sign in to comment.