From 702e35650f8662d964d7137a66db740ded682988 Mon Sep 17 00:00:00 2001 From: Peng DU Date: Fri, 24 Jun 2016 18:26:11 +0200 Subject: [PATCH] event cache --- app/models/event.rb | 2 ++ app/policies/event_policy.rb | 4 ++-- app/views/api/events/index.json.jbuilder | 13 +++++++------ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/models/event.rb b/app/models/event.rb index 8007f6a4e..412580b70 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -14,6 +14,8 @@ class Event < ActiveRecord::Base after_create :event_recurrence before_save :update_nb_free_places + # update event updated_at for index cache + after_save -> { self.touch } def name title diff --git a/app/policies/event_policy.rb b/app/policies/event_policy.rb index fc140bb42..d317c1b96 100644 --- a/app/policies/event_policy.rb +++ b/app/policies/event_policy.rb @@ -2,12 +2,12 @@ class EventPolicy < ApplicationPolicy class Scope < Scope def resolve if user.nil? or (user and !user.is_admin?) - scope.includes(:event_image, :event_files, :availability) + scope.includes(:event_image, :event_files, :availability, :categories) .where('availabilities.start_at >= ?', Time.now) .order('availabilities.start_at ASC') .references(:availabilities) else - scope.includes(:event_image, :event_files, :availability) + scope.includes(:event_image, :event_files, :availability, :categories) .order('availabilities.start_at DESC') .references(:availabilities) end diff --git a/app/views/api/events/index.json.jbuilder b/app/views/api/events/index.json.jbuilder index 4a24caedc..c95bf00a7 100644 --- a/app/views/api/events/index.json.jbuilder +++ b/app/views/api/events/index.json.jbuilder @@ -1,7 +1,8 @@ -json.array!(@events) do |event| - json.partial! 'api/events/event', event: event - json.event_image_small event.event_image.attachment.small.url if event.event_image - json.url event_url(event, format: :json) - json.nb_total_events @total +json.cache! [@events, @page] do + json.array!(@events) do |event| + json.partial! 'api/events/event', event: event + json.event_image_small event.event_image.attachment.small.url if event.event_image + json.url event_url(event, format: :json) + json.nb_total_events @total + end end -