Commit 8e635082886e481c2a27287b4a1e53c88161b6f9
1 parent
ceaa8050
Exists in
article_hit_count
Count only once for unlogged users
Signed-off-by: Luciano Prestes Cavalcanti <lucianopcbr@gmail.com> Signed-off-by: Pedro de Lyra <pedrodelyra@gmail.com> Signed-off-by: Simiao Carvalho <simiaosimis@gmail.com>
Showing
2 changed files
with
25 additions
and
4 deletions
Show diff stats
app/controllers/public/content_viewer_controller.rb
@@ -276,12 +276,19 @@ class ContentViewerController < ApplicationController | @@ -276,12 +276,19 @@ class ContentViewerController < ApplicationController | ||
276 | private | 276 | private |
277 | 277 | ||
278 | def already_visited? | 278 | def already_visited? |
279 | - return true if current_user.nil? | ||
280 | - | ||
281 | - if cookies[:visited].to_i == current_user.id | 279 | + #-1 = -1 |
280 | + if current_user.nil? | ||
281 | + # For non logged in users, a cookie with id -1 is set | ||
282 | + if cookies.permanent.signed[:visited] == -1 | ||
283 | + return true | ||
284 | + else | ||
285 | + cookies.permanent.signed[:visited] = -1 | ||
286 | + return false | ||
287 | + end | ||
288 | + elsif cookies[:visited].to_i == current_user.id | ||
282 | return true | 289 | return true |
283 | else | 290 | else |
284 | - cookies[:visited] = { value: current_user.id, expires: 7.days.from_now } | 291 | + cookies.permanent.signed[:visited] = current_user.id |
285 | return false; | 292 | return false; |
286 | end | 293 | end |
287 | end | 294 | end |
test/functional/content_viewer_controller_test.rb
@@ -1587,4 +1587,18 @@ class ContentViewerControllerTest < ActionController::TestCase | @@ -1587,4 +1587,18 @@ class ContentViewerControllerTest < ActionController::TestCase | ||
1587 | assert_tag :tag => 'div', :attributes => { :class => 'article-compact-image' } | 1587 | assert_tag :tag => 'div', :attributes => { :class => 'article-compact-image' } |
1588 | assert_tag :tag => 'div', :attributes => { :class => 'article-compact-abstract-with-image' } | 1588 | assert_tag :tag => 'div', :attributes => { :class => 'article-compact-abstract-with-image' } |
1589 | end | 1589 | end |
1590 | + | ||
1591 | + should 'not count a visit twice for the same user' do | ||
1592 | + profile = create_user('someone').person | ||
1593 | + page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text') | ||
1594 | + page.save! | ||
1595 | + | ||
1596 | + get :view_page, :profile => page.identifier | ||
1597 | + assert_equal 1, page.hits | ||
1598 | + | ||
1599 | + end | ||
1600 | + | ||
1601 | + should 'not count a visit twice for unlogged users' do | ||
1602 | + assert true | ||
1603 | + end | ||
1590 | end | 1604 | end |