From b385cdcc3477717a9d294bd841d84c30910872e9 Mon Sep 17 00:00:00 2001 From: Einar Jonsson Date: Mon, 26 Aug 2013 10:41:56 +0000 Subject: [PATCH] Extracted featured_users functionality out of Topic. * Created a TopicFeatureUsers model * Topic#featured_user_ids and Topic#feature_topic_users now delegate to * a TopicFeatureUsers instance to keep demeter happy. --- app/models/topic.rb | 48 +++++++--------------------- app/models/topic_featured_users.rb | 51 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 37 deletions(-) create mode 100644 app/models/topic_featured_users.rb diff --git a/app/models/topic.rb b/app/models/topic.rb index 75169d50dba..5fe6a17bd70 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -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 diff --git a/app/models/topic_featured_users.rb b/app/models/topic_featured_users.rb new file mode 100644 index 00000000000..6a0c0a491c8 --- /dev/null +++ b/app/models/topic_featured_users.rb @@ -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