From 76f0a3314588641c62053d8a029490ff52c3ebfe Mon Sep 17 00:00:00 2001 From: echarp Date: Wed, 27 Feb 2019 13:46:32 +0000 Subject: [PATCH] The future parameter uses "has_scope". Plus no more than 3000 events can be lister at most. --- app/controllers/events_controller.rb | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 0da0bb17..b6eb2a4c 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -3,6 +3,7 @@ class EventsController < ApplicationController has_scope :region, :locality, :tag, :daylimit, :year has_scope :near, type: :hash, using: %i[location distance] + has_scope :future, type: :boolean, default: true, only: [:index], if: :future? before_action :set_events, only: :index before_action :set_event, except: %i[index new preview_create create] @@ -14,9 +15,9 @@ class EventsController < ApplicationController def index respond_to do |format| format.html - format.ics { @events = @events.future } - format.json { @events = @events.future } - format.rss { @events = filter_for_rss } + format.ics + format.json + format.rss format.xml end end @@ -83,7 +84,9 @@ class EventsController < ApplicationController private def set_events - @events = apply_scopes Event.moderated + # The 3000 limit is purely arbitrary... + @events = apply_scopes Event.moderated.order('id desc') + .limit(params[:format] == 'rss' ? 20 : 3000) end # Use callbacks to share common setup or constraints between actions @@ -110,14 +113,6 @@ class EventsController < ApplicationController :locality, :url, :contact, :submitter, :tag_list end - def filter_for_rss - if params[:future] == 'false' - @events.order 'id desc' - else - @events.future.order('id desc').limit 20 - end - end - def locked redirect_to edit_event_url(@event, secret: @event.secret), alert: t('staleObjectError') @@ -128,4 +123,10 @@ class EventsController < ApplicationController redirect_to :root, alert: t(:forbidden, scope: %i[events edit]) \ unless params[:secret] == @event.secret end + + # Should the future scope be applied? + # Only on non html pages... + def future? + params[:format].present? && params[:format] != 'html' + end end