From 0f5bbb999c9770654613cb373070984dd785e855 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 24 Mar 2016 13:21:53 +0100 Subject: Pre-loading polymorphic associations for Atom feeds --- app/controllers/accounts_controller.rb | 9 ++++++++- app/models/concerns/paginable.rb | 7 +++++++ app/models/status.rb | 7 ++++--- app/models/stream_entry.rb | 5 +++-- 4 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 app/models/concerns/paginable.rb diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 53f665238..6794234e4 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -7,7 +7,14 @@ class AccountsController < ApplicationController def show respond_to do |format| format.html { @statuses = @account.statuses.order('id desc').with_includes.with_counters.paginate(page: params[:page], per_page: 10)} - format.atom { @entries = @account.stream_entries.order('id desc').with_includes.paginate_by_max_id(20, params[:max_id] || nil) } + + format.atom do + @entries = @account.stream_entries.order('id desc').with_includes.paginate_by_max_id(20, params[:max_id] || nil) + + ActiveRecord::Associations::Preloader.new.preload(@entries.select { |a| a.activity_type == 'Status' }, :mentioned_accounts, reblog: :account, thread: :account) + ActiveRecord::Associations::Preloader.new.preload(@entries.select { |a| a.activity_type == 'Favourite' }, status: [:account, :thread, :mentioned_accounts]) + ActiveRecord::Associations::Preloader.new.preload(@entries.select { |a| a.activity_type == 'Follow' }, :target_account) + end end end diff --git a/app/models/concerns/paginable.rb b/app/models/concerns/paginable.rb new file mode 100644 index 000000000..54552ae3d --- /dev/null +++ b/app/models/concerns/paginable.rb @@ -0,0 +1,7 @@ +module Paginable + extend ActiveSupport::Concern + + included do + scope :paginate_by_max_id, -> (limit, max_id) { order('id desc').limit(limit).where(max_id.nil? ? '1=1' : ['id < ?', max_id]) } + end +end diff --git a/app/models/status.rb b/app/models/status.rb index b1965f176..76218bea0 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -1,4 +1,6 @@ class Status < ActiveRecord::Base + include Paginable + belongs_to :account, inverse_of: :statuses belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies @@ -15,9 +17,8 @@ class Status < ActiveRecord::Base validates :uri, uniqueness: true, unless: 'local?' validates :text, presence: true, if: Proc.new { |s| s.local? && !s.reblog? } - scope :with_counters, -> { select('statuses.*, (select count(r.id) from statuses as r where r.reblog_of_id = statuses.id) as reblogs_count, (select count(f.id) from favourites as f where f.status_id = statuses.id) as favourites_count') } - scope :with_includes, -> { includes(:account, reblog: :account, thread: :account) } - scope :paginate_by_max_id, -> (limit, max_id) { order('id desc').limit(limit).where(max_id.nil? ? '1=1' : ['id < ?', max_id]) } + scope :with_counters, -> { select('statuses.*, (select count(r.id) from statuses as r where r.reblog_of_id = statuses.id) as reblogs_count, (select count(f.id) from favourites as f where f.status_id = statuses.id) as favourites_count') } + scope :with_includes, -> { includes(:account, :mentioned_accounts, reblog: [:account, :mentioned_accounts], thread: [:account, :mentioned_accounts]) } def local? self.uri.nil? diff --git a/app/models/stream_entry.rb b/app/models/stream_entry.rb index d64edd62f..35eab1df0 100644 --- a/app/models/stream_entry.rb +++ b/app/models/stream_entry.rb @@ -1,11 +1,12 @@ class StreamEntry < ActiveRecord::Base + include Paginable + belongs_to :account, inverse_of: :stream_entries belongs_to :activity, polymorphic: true validates :account, :activity, presence: true - scope :with_includes, -> { includes(:activity) } - scope :paginate_by_max_id, -> (limit, max_id) { order('id desc').limit(limit).where(max_id.nil? ? '1=1' : ['id < ?', max_id]) } + scope :with_includes, -> { includes(:activity) } def object_type orphaned? ? :activity : (targeted? ? :activity : self.activity.object_type) -- cgit