diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2016-03-24 13:21:53 +0100 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2016-03-24 13:21:53 +0100 |
commit | 0f5bbb999c9770654613cb373070984dd785e855 (patch) | |
tree | 90c4dac78f605ea357868502da9607d6812a1a8b /app/models | |
parent | 7e58303a8da90b31f4179695283d3ac19dfb09d1 (diff) |
Pre-loading polymorphic associations for Atom feeds
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/concerns/paginable.rb | 7 | ||||
-rw-r--r-- | app/models/status.rb | 7 | ||||
-rw-r--r-- | app/models/stream_entry.rb | 5 |
3 files changed, 14 insertions, 5 deletions
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) |