mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
Add unstarred_at column to topic_users so we can permanently track when topics are starred
This commit is contained in:
parent
60208e3ded
commit
dc07563c0d
3 changed files with 40 additions and 22 deletions
|
@ -569,7 +569,7 @@ class Topic < ActiveRecord::Base
|
||||||
# Enable/disable the star on the topic
|
# Enable/disable the star on the topic
|
||||||
def toggle_star(user, starred)
|
def toggle_star(user, starred)
|
||||||
Topic.transaction do
|
Topic.transaction do
|
||||||
TopicUser.change(user, id, starred: starred, starred_at: starred ? DateTime.now : nil)
|
TopicUser.change(user, id, {starred: starred}.merge( starred ? {starred_at: DateTime.now, unstarred_at: nil} : {unstarred_at: DateTime.now}))
|
||||||
|
|
||||||
# Update the star count
|
# Update the star count
|
||||||
exec_sql "UPDATE topics
|
exec_sql "UPDATE topics
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
class AddUnstarredAtToTopicUsers < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column :topic_users, :unstarred_at, :datetime
|
||||||
|
end
|
||||||
|
end
|
|
@ -574,30 +574,34 @@ describe Topic do
|
||||||
|
|
||||||
describe 'toggle_star' do
|
describe 'toggle_star' do
|
||||||
|
|
||||||
|
shared_examples_for "adding a star to a topic" do
|
||||||
|
it 'triggers a forum topic user change with true' do
|
||||||
|
# otherwise no chance the mock will work
|
||||||
|
freeze_time do
|
||||||
|
TopicUser.expects(:change).with(@user, @topic.id, starred: true, starred_at: DateTime.now, unstarred_at: nil)
|
||||||
|
@topic.toggle_star(@user, true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'increases the star_count of the forum topic' do
|
||||||
|
lambda {
|
||||||
|
@topic.toggle_star(@user, true)
|
||||||
|
@topic.reload
|
||||||
|
}.should change(@topic, :star_count).by(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'triggers the rate limiter' do
|
||||||
|
Topic::FavoriteLimiter.any_instance.expects(:performed!)
|
||||||
|
@topic.toggle_star(@user, true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
before do
|
before do
|
||||||
@topic = Fabricate(:topic)
|
@topic = Fabricate(:topic)
|
||||||
@user = @topic.user
|
@user = @topic.user
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'triggers a forum topic user change with true' do
|
it_should_behave_like "adding a star to a topic"
|
||||||
# otherwise no chance the mock will work
|
|
||||||
freeze_time do
|
|
||||||
TopicUser.expects(:change).with(@user, @topic.id, starred: true, starred_at: DateTime.now)
|
|
||||||
@topic.toggle_star(@user, true)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'increases the star_count of the forum topic' do
|
|
||||||
lambda {
|
|
||||||
@topic.toggle_star(@user, true)
|
|
||||||
@topic.reload
|
|
||||||
}.should change(@topic, :star_count).by(1)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'triggers the rate limiter' do
|
|
||||||
Topic::FavoriteLimiter.any_instance.expects(:performed!)
|
|
||||||
@topic.toggle_star(@user, true)
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'removing a star' do
|
describe 'removing a star' do
|
||||||
before do
|
before do
|
||||||
|
@ -611,8 +615,10 @@ describe Topic do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'triggers a forum topic user change with false' do
|
it 'triggers a forum topic user change with false' do
|
||||||
TopicUser.expects(:change).with(@user, @topic.id, starred: false, starred_at: nil)
|
freeze_time do
|
||||||
@topic.toggle_star(@user, false)
|
TopicUser.expects(:change).with(@user, @topic.id, starred: false, unstarred_at: DateTime.now)
|
||||||
|
@topic.toggle_star(@user, false)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'reduces the star_count' do
|
it 'reduces the star_count' do
|
||||||
|
@ -622,6 +628,13 @@ describe Topic do
|
||||||
}.should change(@topic, :star_count).by(-1)
|
}.should change(@topic, :star_count).by(-1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'and adding a star again' do
|
||||||
|
before do
|
||||||
|
@topic.toggle_star(@user, false)
|
||||||
|
@topic.reload
|
||||||
|
end
|
||||||
|
it_should_behave_like "adding a star to a topic"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue