From 8e635082886e481c2a27287b4a1e53c88161b6f9 Mon Sep 17 00:00:00 2001 From: pedrodelyra Date: Wed, 26 Aug 2015 16:47:49 -0300 Subject: [PATCH] Count only once for unlogged users --- app/controllers/public/content_viewer_controller.rb | 15 +++++++++++---- test/functional/content_viewer_controller_test.rb | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index b0708f9..1c6358a 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -276,12 +276,19 @@ class ContentViewerController < ApplicationController private def already_visited? - return true if current_user.nil? - - if cookies[:visited].to_i == current_user.id + #-1 = -1 + if current_user.nil? + # For non logged in users, a cookie with id -1 is set + if cookies.permanent.signed[:visited] == -1 + return true + else + cookies.permanent.signed[:visited] = -1 + return false + end + elsif cookies[:visited].to_i == current_user.id return true else - cookies[:visited] = { value: current_user.id, expires: 7.days.from_now } + cookies.permanent.signed[:visited] = current_user.id return false; end end diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 3006e97..528d91e 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -1587,4 +1587,18 @@ class ContentViewerControllerTest < ActionController::TestCase assert_tag :tag => 'div', :attributes => { :class => 'article-compact-image' } assert_tag :tag => 'div', :attributes => { :class => 'article-compact-abstract-with-image' } end + + should 'not count a visit twice for the same user' do + profile = create_user('someone').person + page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text') + page.save! + + get :view_page, :profile => page.identifier + assert_equal 1, page.hits + + end + + should 'not count a visit twice for unlogged users' do + assert true + end end -- libgit2 0.21.2