Skip to content

Commit b385cdc

Browse files
committed
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.
1 parent 7787770 commit b385cdc

2 files changed

Lines changed: 62 additions & 37 deletions

File tree

app/models/topic.rb

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ def self.max_sort_order
1515
2**31 - 1
1616
end
1717

18-
def self.featured_users_count
19-
4
18+
versioned if: :new_version_required?
19+
20+
def featured_users
21+
@featured_users ||= TopicFeaturedUsers.new(self)
2022
end
2123

22-
versioned if: :new_version_required?
24+
def featured_user_ids
25+
featured_users.user_ids
26+
end
27+
28+
def feature_topic_users(args={})
29+
featured_users.choose(args)
30+
end
2331

2432
def trash!(trashed_by=nil)
2533
update_category_topic_count_by(-1) if deleted_at.nil?
@@ -368,9 +376,6 @@ def change_category(name)
368376
changed_to_category(cat)
369377
end
370378

371-
def featured_user_ids
372-
[featured_user1_id, featured_user2_id, featured_user3_id, featured_user4_id].uniq.compact
373-
end
374379

375380
def remove_allowed_user(username)
376381
user = User.where(username: username).first
@@ -466,13 +471,6 @@ def update_action_counts
466471
end
467472
end
468473

469-
# Chooses which topic users to feature
470-
def feature_topic_users(args={})
471-
reload unless rails4?
472-
clear_featured_users
473-
update_featured_users featured_user_keys(args)
474-
save
475-
end
476474

477475
def posters_summary(options = {})
478476
@posters_summary ||= TopicPostersSummary.new(self, options).summary
@@ -624,30 +622,6 @@ def update_category_topic_count_by(num)
624622
end
625623
end
626624

627-
def featured_user_keys(args)
628-
# Don't include the OP or the last poster
629-
to_feature = posts.where('user_id NOT IN (?, ?)', user_id, last_post_user_id)
630-
631-
# Exclude a given post if supplied (in the case of deletes)
632-
to_feature = to_feature.where("id <> ?", args[:except_post_id]) if args[:except_post_id].present?
633-
634-
635-
# Assign the featured_user{x} columns
636-
to_feature.group(:user_id).order('count_all desc').limit(Topic.featured_users_count).count.keys
637-
end
638-
639-
640-
def clear_featured_users
641-
Topic.featured_users_count.times do |i|
642-
send("featured_user#{i+1}_id=", nil)
643-
end
644-
end
645-
646-
def update_featured_users(user_keys)
647-
user_keys.each_with_index do |user_id, i|
648-
send("featured_user#{i+1}_id=", user_id)
649-
end
650-
end
651625
end
652626

653627
# == Schema Information

app/models/topic_featured_users.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class TopicFeaturedUsers
2+
attr_reader :topic
3+
4+
def initialize(topic)
5+
@topic = topic
6+
end
7+
8+
def self.count
9+
4
10+
end
11+
12+
# Chooses which topic users to feature
13+
def choose(args={})
14+
topic.reload unless rails4?
15+
clear
16+
update keys(args)
17+
topic.save
18+
end
19+
20+
def user_ids
21+
[topic.featured_user1_id,
22+
topic.featured_user2_id,
23+
topic.featured_user3_id,
24+
topic.featured_user4_id].uniq.compact
25+
end
26+
27+
private
28+
29+
def keys(args)
30+
# Don't include the OP or the last poster
31+
to_feature = topic.posts.where('user_id NOT IN (?, ?)', topic.user_id, topic.last_post_user_id)
32+
33+
# Exclude a given post if supplied (in the case of deletes)
34+
to_feature = to_feature.where("id <> ?", args[:except_post_id]) if args[:except_post_id].present?
35+
36+
# Assign the featured_user{x} columns
37+
to_feature.group(:user_id).order('count_all desc').limit(TopicFeaturedUsers.count).count.keys
38+
end
39+
40+
def clear
41+
TopicFeaturedUsers.count.times do |i|
42+
topic.send("featured_user#{i+1}_id=", nil)
43+
end
44+
end
45+
46+
def update(user_keys)
47+
user_keys.each_with_index do |user_id, i|
48+
topic.send("featured_user#{i+1}_id=", user_id)
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)