From 6137bb46d31f0bb3ef3a4488c68a17a750ddb84e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Sat, 14 May 2016 10:06:29 +0200 Subject: [PATCH] FIX: a User is *not* a Topic --- app/controllers/topics_controller.rb | 4 +--- app/models/topic_user.rb | 8 +++----- spec/models/topic_user_spec.rb | 6 +++--- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 9a97662a6..27f360dc2 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -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 diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb index aab6d1b1c..5d0919de8 100644 --- a/app/models/topic_user.rb +++ b/app/models/topic_user.rb @@ -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 diff --git a/spec/models/topic_user_spec.rb b/spec/models/topic_user_spec.rb index f51b90b33..e08725480 100644 --- a/spec/models/topic_user_spec.rb +++ b/spec/models/topic_user_spec.rb @@ -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