mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 03:59:50 +08:00
Support for tracking the count of unique participants in a topic
This commit is contained in:
parent
41dfcd2774
commit
67041d1c6d
|
@ -14,13 +14,14 @@ class TopicFeaturedUsers
|
|||
topic.reload unless rails4?
|
||||
clear
|
||||
update keys(args)
|
||||
update_participant_count
|
||||
topic.save
|
||||
end
|
||||
|
||||
def user_ids
|
||||
[topic.featured_user1_id,
|
||||
topic.featured_user2_id,
|
||||
topic.featured_user3_id,
|
||||
[topic.featured_user1_id,
|
||||
topic.featured_user2_id,
|
||||
topic.featured_user3_id,
|
||||
topic.featured_user4_id].uniq.compact
|
||||
end
|
||||
|
||||
|
@ -48,4 +49,8 @@ class TopicFeaturedUsers
|
|||
topic.send("featured_user#{i+1}_id=", user_id)
|
||||
end
|
||||
end
|
||||
|
||||
def update_participant_count
|
||||
topic.participant_count = topic.posts.count('distinct user_id')
|
||||
end
|
||||
end
|
||||
|
|
13
db/migrate/20131114185225_add_participant_count_to_topics.rb
Normal file
13
db/migrate/20131114185225_add_participant_count_to_topics.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
class AddParticipantCountToTopics < ActiveRecord::Migration
|
||||
def up
|
||||
add_column :topics, :participant_count, :integer, default: 1
|
||||
|
||||
execute "UPDATE topics SET participant_count =
|
||||
(SELECT COUNT(DISTINCT p.user_id) FROM posts AS p WHERE p.topic_id = topics.id)"
|
||||
end
|
||||
|
||||
def down
|
||||
remove_column :topics, :participant_count
|
||||
end
|
||||
|
||||
end
|
|
@ -42,4 +42,33 @@ describe Jobs::FeatureTopicUsers do
|
|||
|
||||
end
|
||||
|
||||
context "participant count" do
|
||||
|
||||
let!(:post) { create_post }
|
||||
let(:topic) { post.topic }
|
||||
|
||||
|
||||
it "it works as expected" do
|
||||
|
||||
# It has 1 participant after creation
|
||||
topic.participant_count.should == 1
|
||||
|
||||
# It still has 1 after featuring
|
||||
Jobs::FeatureTopicUsers.new.execute(topic_id: topic.id)
|
||||
topic.reload.participant_count.should == 1
|
||||
|
||||
# If the OP makes another post, it's still 1.
|
||||
create_post(topic: topic, user: post.user)
|
||||
Jobs::FeatureTopicUsers.new.execute(topic_id: topic.id)
|
||||
topic.reload.participant_count.should == 1
|
||||
|
||||
# If another users posts, it's 2.
|
||||
create_post(topic: topic, user: Fabricate(:evil_trout))
|
||||
Jobs::FeatureTopicUsers.new.execute(topic_id: topic.id)
|
||||
topic.reload.participant_count.should == 2
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user