Skip to content

Commit

Permalink
limit events fetched for calendar to month requested
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanVanVugt committed Aug 25, 2023
1 parent ea7eeb1 commit cdce641
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ def calendar
# include expired results so we also fill past pages
params[:per_page] ||= 200
params[:include_expired] ||= 'true'
@start_date = Date.parse(params.fetch(:start_date, Date.today.to_s)).beginning_of_month

set_params
# override @facet_params to get only events relevant for the current month view
@facet_params[:running_during] = "#{@start_date.to_s}/#{(@start_date + 1.month).to_s}"

fetch_resources
# now customize the list by moving all events longer than 3 days into a separate array
@long_events, @events = @events.partition { |e| e.end.nil? || e.start.nil? || e.start + TeSS::Config.site.fetch(:calendar_event_maxlength, 5).to_i.days < e.end }
@start_date = params[:start_date]

respond_to do |format|
format.js
Expand Down
16 changes: 16 additions & 0 deletions lib/facets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Facets
elixir: -> (c) { ['Event', 'Material', 'ContentProvider'].include?(c.name) },
max_age: -> (c) { ['Event', 'Material'].include?(c.name) },
start: -> (c) { c.name == 'Event' },
running_during: -> (c) { c.name == 'Event' },
include_hidden: -> (c) { c.method_defined?(:user_requires_approval?) }
}.with_indifferent_access.freeze

Expand All @@ -15,6 +16,7 @@ module Facets
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 } },
running_during: -> (value) { value&.split('/')&.map {|d| Date.parse(d) rescue nil } },
include_hidden: -> (value) { value == 'true'}
}

Expand Down Expand Up @@ -56,6 +58,20 @@ def start(scope, bounds, _)
end
end

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

sunspot_scoped(scope) do
if lb && ub
with(:end).between(lb..(ub + TeSS::Config.site.fetch(:calendar_event_maxlength, 5).to_i.days))
elsif lb
with(:end).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 cdce641

Please sign in to comment.