Merge pull request #1389 from einarj/extract_featured_users
Extracted featured_users functionality out of Topic.
This commit is contained in:
commit
90fef936dc
|
@ -15,11 +15,19 @@ class Topic < ActiveRecord::Base
|
|||
2**31 - 1
|
||||
end
|
||||
|
||||
def self.featured_users_count
|
||||
4
|
||||
versioned if: :new_version_required?
|
||||
|
||||
def featured_users
|
||||
@featured_users ||= TopicFeaturedUsers.new(self)
|
||||
end
|
||||
|
||||
versioned if: :new_version_required?
|
||||
def featured_user_ids
|
||||
featured_users.user_ids
|
||||
end
|
||||
|
||||
def feature_topic_users(args={})
|
||||
featured_users.choose(args)
|
||||
end
|
||||
|
||||
def trash!(trashed_by=nil)
|
||||
update_category_topic_count_by(-1) if deleted_at.nil?
|
||||
|
@ -368,9 +376,6 @@ class Topic < ActiveRecord::Base
|
|||
changed_to_category(cat)
|
||||
end
|
||||
|
||||
def featured_user_ids
|
||||
[featured_user1_id, featured_user2_id, featured_user3_id, featured_user4_id].uniq.compact
|
||||
end
|
||||
|
||||
def remove_allowed_user(username)
|
||||
user = User.where(username: username).first
|
||||
|
@ -466,13 +471,6 @@ class Topic < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
# Chooses which topic users to feature
|
||||
def feature_topic_users(args={})
|
||||
reload unless rails4?
|
||||
clear_featured_users
|
||||
update_featured_users featured_user_keys(args)
|
||||
save
|
||||
end
|
||||
|
||||
def posters_summary(options = {})
|
||||
@posters_summary ||= TopicPostersSummary.new(self, options).summary
|
||||
|
@ -624,30 +622,6 @@ class Topic < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def featured_user_keys(args)
|
||||
# Don't include the OP or the last poster
|
||||
to_feature = posts.where('user_id NOT IN (?, ?)', user_id, last_post_user_id)
|
||||
|
||||
# Exclude a given post if supplied (in the case of deletes)
|
||||
to_feature = to_feature.where("id <> ?", args[:except_post_id]) if args[:except_post_id].present?
|
||||
|
||||
|
||||
# Assign the featured_user{x} columns
|
||||
to_feature.group(:user_id).order('count_all desc').limit(Topic.featured_users_count).count.keys
|
||||
end
|
||||
|
||||
|
||||
def clear_featured_users
|
||||
Topic.featured_users_count.times do |i|
|
||||
send("featured_user#{i+1}_id=", nil)
|
||||
end
|
||||
end
|
||||
|
||||
def update_featured_users(user_keys)
|
||||
user_keys.each_with_index do |user_id, i|
|
||||
send("featured_user#{i+1}_id=", user_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
class TopicFeaturedUsers
|
||||
attr_reader :topic
|
||||
|
||||
def initialize(topic)
|
||||
@topic = topic
|
||||
end
|
||||
|
||||
def self.count
|
||||
4
|
||||
end
|
||||
|
||||
# Chooses which topic users to feature
|
||||
def choose(args={})
|
||||
topic.reload unless rails4?
|
||||
clear
|
||||
update keys(args)
|
||||
topic.save
|
||||
end
|
||||
|
||||
def user_ids
|
||||
[topic.featured_user1_id,
|
||||
topic.featured_user2_id,
|
||||
topic.featured_user3_id,
|
||||
topic.featured_user4_id].uniq.compact
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def keys(args)
|
||||
# Don't include the OP or the last poster
|
||||
to_feature = topic.posts.where('user_id NOT IN (?, ?)', topic.user_id, topic.last_post_user_id)
|
||||
|
||||
# Exclude a given post if supplied (in the case of deletes)
|
||||
to_feature = to_feature.where("id <> ?", args[:except_post_id]) if args[:except_post_id].present?
|
||||
|
||||
# Assign the featured_user{x} columns
|
||||
to_feature.group(:user_id).order('count_all desc').limit(TopicFeaturedUsers.count).count.keys
|
||||
end
|
||||
|
||||
def clear
|
||||
TopicFeaturedUsers.count.times do |i|
|
||||
topic.send("featured_user#{i+1}_id=", nil)
|
||||
end
|
||||
end
|
||||
|
||||
def update(user_keys)
|
||||
user_keys.each_with_index do |user_id, i|
|
||||
topic.send("featured_user#{i+1}_id=", user_id)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue