FIX: a User is *not* a Topic

This commit is contained in:
Régis Hanol 2016-05-14 10:06:29 +02:00
parent ece8083ab1
commit 6137bb46d3
3 changed files with 7 additions and 11 deletions

View File

@ -576,9 +576,7 @@ class TopicsController < ApplicationController
Scheduler::Defer.later "Track Visit" do
TopicViewItem.add(topic_id, ip, user_id)
if track_visit
TopicUser.track_visit! topic_id, user_id
end
TopicUser.track_visit!(topic_id, user_id) if track_visit
end
end

View File

@ -125,16 +125,14 @@ class TopicUser < ActiveRecord::Base
# In case of a race condition to insert, do nothing
end
def track_visit!(topic,user)
topic_id = topic.is_a?(Topic) ? topic.id : topic
user_id = user.is_a?(User) ? user.id : topic
def track_visit!(topic_id, user_id)
now = DateTime.now
rows = TopicUser.where(topic_id: topic_id, user_id: user_id).update_all(last_visited_at: now)
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
observe_after_save_callbacks_for(topic_id, user_id)
end
end

View File

@ -125,7 +125,7 @@ describe TopicUser do
describe 'visited at' do
before do
TopicUser.track_visit!(topic, user)
TopicUser.track_visit!(topic.id, user.id)
end
it 'set upon initial visit' do
@ -139,7 +139,7 @@ describe TopicUser do
today = yesterday.tomorrow
freeze_time today do
TopicUser.track_visit!(topic,user)
TopicUser.track_visit!(topic.id, user.id)
# reload is a no go
topic_user = TopicUser.get(topic,user)
expect(topic_user.first_visited_at.to_i).to eq(yesterday.to_i)
@ -149,7 +149,7 @@ describe TopicUser do
it 'triggers the observer callbacks when updating' do
UserActionObserver.instance.expects(:after_save).twice
2.times { TopicUser.track_visit!(topic, user) }
2.times { TopicUser.track_visit!(topic.id, user.id) }
end
end