From dc07563c0d2d7d7fce47e7a3564100126108c30c Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Sun, 28 Apr 2013 16:58:14 -0400 Subject: [PATCH] Add unstarred_at column to topic_users so we can permanently track when topics are starred --- app/models/topic.rb | 2 +- ...8194335_add_unstarred_at_to_topic_users.rb | 5 ++ spec/models/topic_spec.rb | 55 ++++++++++++------- 3 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 db/migrate/20130428194335_add_unstarred_at_to_topic_users.rb diff --git a/app/models/topic.rb b/app/models/topic.rb index 2f7bc2584a5..fa9e06e47bd 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -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 diff --git a/db/migrate/20130428194335_add_unstarred_at_to_topic_users.rb b/db/migrate/20130428194335_add_unstarred_at_to_topic_users.rb new file mode 100644 index 00000000000..1f74eca660e --- /dev/null +++ b/db/migrate/20130428194335_add_unstarred_at_to_topic_users.rb @@ -0,0 +1,5 @@ +class AddUnstarredAtToTopicUsers < ActiveRecord::Migration + def change + add_column :topic_users, :unstarred_at, :datetime + end +end diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 0d01c75cc81..4349ffc184e 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -574,30 +574,34 @@ describe Topic 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 @topic = Fabricate(:topic) @user = @topic.user end - 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) - @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 + it_should_behave_like "adding a star to a topic" describe 'removing a star' do before do @@ -611,8 +615,10 @@ 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) - @topic.toggle_star(@user, false) + 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 @@ -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