Merge pull request #876 from chrishunt/chrishunt/observe-changes-for-topic-users
Trigger UserActionObserver when updating TopicUser
This commit is contained in:
commit
96f8d79e83
|
@ -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
|
||||
|
@ -98,6 +100,8 @@ class TopicUser < ActiveRecord::Base
|
|||
rows = TopicUser.update_all({last_visited_at: now}, {topic_id: topic.id, user_id: user.id})
|
||||
if rows == 0
|
||||
TopicUser.create(topic_id: topic.id, user_id: user.id, last_visited_at: now, first_visited_at: now)
|
||||
else
|
||||
observe_after_save_callbacks_for topic.id, user.id
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -173,6 +177,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!
|
||||
|
|
|
@ -107,6 +107,10 @@ describe TopicUser do
|
|||
topic_user.last_visited_at.to_i.should == today.to_i
|
||||
end
|
||||
|
||||
it 'triggers the observer callbacks when updating' do
|
||||
UserActionObserver.instance.expects(:after_save).twice
|
||||
2.times { TopicUser.track_visit!(topic, user) }
|
||||
end
|
||||
end
|
||||
|
||||
describe 'read tracking' do
|
||||
|
@ -186,6 +190,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)
|
||||
|
|
Loading…
Reference in New Issue