Add unstarred_at column to topic_users so we can permanently track when topics are starred

This commit is contained in:
Neil Lalonde 2013-04-28 16:58:14 -04:00
parent 60208e3ded
commit dc07563c0d
3 changed files with 40 additions and 22 deletions

View file

@ -569,7 +569,7 @@ class Topic < ActiveRecord::Base
# Enable/disable the star on the topic
def toggle_star(user, starred)
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
exec_sql "UPDATE topics

View file

@ -0,0 +1,5 @@
class AddUnstarredAtToTopicUsers < ActiveRecord::Migration
def change
add_column :topic_users, :unstarred_at, :datetime
end
end

View file

@ -574,15 +574,11 @@ describe Topic do
describe 'toggle_star' do
before do
@topic = Fabricate(:topic)
@user = @topic.user
end
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)
TopicUser.expects(:change).with(@user, @topic.id, starred: true, starred_at: DateTime.now, unstarred_at: nil)
@topic.toggle_star(@user, true)
end
end
@ -598,6 +594,14 @@ describe Topic do
Topic::FavoriteLimiter.any_instance.expects(:performed!)
@topic.toggle_star(@user, true)
end
end
before do
@topic = Fabricate(:topic)
@user = @topic.user
end
it_should_behave_like "adding a star to a topic"
describe 'removing a star' do
before do
@ -611,9 +615,11 @@ describe Topic do
end
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
TopicUser.expects(:change).with(@user, @topic.id, starred: false, unstarred_at: DateTime.now)
@topic.toggle_star(@user, false)
end
end
it 'reduces the star_count' do
lambda {
@ -622,6 +628,13 @@ describe Topic do
}.should change(@topic, :star_count).by(-1)
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