The future parameter uses "has_scope".

Plus no more than 3000 events can be lister at most.
This commit is contained in:
echarp 2019-02-27 13:46:32 +00:00
parent 0ad35f1fb7
commit 76f0a33145

View File

@ -3,6 +3,7 @@
class EventsController < ApplicationController class EventsController < ApplicationController
has_scope :region, :locality, :tag, :daylimit, :year has_scope :region, :locality, :tag, :daylimit, :year
has_scope :near, type: :hash, using: %i[location distance] 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_events, only: :index
before_action :set_event, except: %i[index new preview_create create] before_action :set_event, except: %i[index new preview_create create]
@ -14,9 +15,9 @@ class EventsController < ApplicationController
def index def index
respond_to do |format| respond_to do |format|
format.html format.html
format.ics { @events = @events.future } format.ics
format.json { @events = @events.future } format.json
format.rss { @events = filter_for_rss } format.rss
format.xml format.xml
end end
end end
@ -83,7 +84,9 @@ class EventsController < ApplicationController
private private
def set_events 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 end
# Use callbacks to share common setup or constraints between actions # Use callbacks to share common setup or constraints between actions
@ -110,14 +113,6 @@ class EventsController < ApplicationController
:locality, :url, :contact, :submitter, :tag_list :locality, :url, :contact, :submitter, :tag_list
end 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 def locked
redirect_to edit_event_url(@event, secret: @event.secret), redirect_to edit_event_url(@event, secret: @event.secret),
alert: t('staleObjectError') alert: t('staleObjectError')
@ -128,4 +123,10 @@ class EventsController < ApplicationController
redirect_to :root, alert: t(:forbidden, scope: %i[events edit]) \ redirect_to :root, alert: t(:forbidden, scope: %i[events edit]) \
unless params[:secret] == @event.secret unless params[:secret] == @event.secret
end end
# Should the future scope be applied?
# Only on non html pages...
def future?
params[:format].present? && params[:format] != 'html'
end
end end