Trigger UserActionObserver when updating TopicUser

This commit is contained in:
Chris Hunt 2013-05-21 20:43:43 -07:00
parent 45361934b9
commit 685f4960f1
2 changed files with 12 additions and 0 deletions

View File

@ -87,6 +87,8 @@ class TopicUser < ActiveRecord::Base
end
TopicUser.create(attrs.merge!(user_id: user_id, topic_id: topic_id.to_i, first_visited_at: now ,last_visited_at: now))
else
observe_after_save_callbacks_for topic_id, user_id
end
end
rescue ActiveRecord::RecordNotUnique
@ -173,6 +175,11 @@ class TopicUser < ActiveRecord::Base
end
end
def observe_after_save_callbacks_for(topic_id, user_id)
TopicUser.where(topic_id: topic_id, user_id: user_id).each do |topic_user|
UserActionObserver.instance.after_save topic_user
end
end
end
def self.ensure_consistency!

View File

@ -186,6 +186,11 @@ describe TopicUser do
}.should change(TopicUser, :count).by(1)
end
it 'triggers the observer callbacks when updating' do
UserActionObserver.instance.expects(:after_save).twice
3.times { TopicUser.change(user, topic.id, starred: true) }
end
describe 'after creating a row' do
before do
TopicUser.change(user, topic.id, starred: true)