From 685f4960f12c7d36494b0012a52626214a148390 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Tue, 21 May 2013 20:43:43 -0700 Subject: [PATCH] Trigger UserActionObserver when updating TopicUser --- app/models/topic_user.rb | 7 +++++++ spec/models/topic_user_spec.rb | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb index 4ff10541f90..80e2745e95c 100644 --- a/app/models/topic_user.rb +++ b/app/models/topic_user.rb @@ -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! diff --git a/spec/models/topic_user_spec.rb b/spec/models/topic_user_spec.rb index eeb7f65395b..a8cf85e0f4a 100644 --- a/spec/models/topic_user_spec.rb +++ b/spec/models/topic_user_spec.rb @@ -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)