Commit c9e090d97e8706e4681a1118464e05107b377425
Committed by
Thiago Ribeiro
1 parent
c7e9ad9e
Exists in
master
and in
21 other branches
Fixed post view hits for same visitor
- Refreshing page no longer counts visits - Unlogged in users counts only once - Added test to page hits - Refactor already_visited? method 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> Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
Showing
2 changed files
with
45 additions
and
1 deletions
Show diff stats
app/controllers/public/content_viewer_controller.rb
| ... | ... | @@ -8,6 +8,7 @@ class ContentViewerController < ApplicationController |
| 8 | 8 | helper TagsHelper |
| 9 | 9 | |
| 10 | 10 | def view_page |
| 11 | + | |
| 11 | 12 | path = get_path(params[:page], params[:format]) |
| 12 | 13 | |
| 13 | 14 | @version = params[:version].to_i |
| ... | ... | @@ -38,7 +39,7 @@ class ContentViewerController < ApplicationController |
| 38 | 39 | end |
| 39 | 40 | |
| 40 | 41 | # At this point the page will be showed |
| 41 | - @page.hit unless user_is_a_bot? | |
| 42 | + @page.hit unless user_is_a_bot? || already_visited?(@page) | |
| 42 | 43 | |
| 43 | 44 | @page = FilePresenter.for @page |
| 44 | 45 | |
| ... | ... | @@ -272,4 +273,18 @@ class ContentViewerController < ApplicationController |
| 272 | 273 | @comment_order = params[:comment_order].nil? ? 'oldest' : params[:comment_order] |
| 273 | 274 | end |
| 274 | 275 | |
| 276 | + private | |
| 277 | + | |
| 278 | + def already_visited?(element) | |
| 279 | + user_id = if user.nil? then -1 else current_user.id end | |
| 280 | + user_id = "#{user_id}_#{element.id}_#{element.class}" | |
| 281 | + | |
| 282 | + if cookies.signed[:visited] == user_id | |
| 283 | + return true | |
| 284 | + else | |
| 285 | + cookies.permanent.signed[:visited] = user_id | |
| 286 | + return false | |
| 287 | + end | |
| 288 | + end | |
| 289 | + | |
| 275 | 290 | end | ... | ... |
test/functional/content_viewer_controller_test.rb
| ... | ... | @@ -1587,4 +1587,33 @@ class ContentViewerControllerTest < ActionController::TestCase |
| 1587 | 1587 | assert_tag :tag => 'div', :attributes => { :class => 'article-compact-image' } |
| 1588 | 1588 | assert_tag :tag => 'div', :attributes => { :class => 'article-compact-abstract-with-image' } |
| 1589 | 1589 | end |
| 1590 | + | |
| 1591 | + should 'not count a visit twice for the same user' do | |
| 1592 | + profile = create_user('someone').person | |
| 1593 | + login_as(@profile.identifier) | |
| 1594 | + page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text') | |
| 1595 | + page.save! | |
| 1596 | + | |
| 1597 | + get :view_page, :profile => profile.identifier, :page => 'myarticle' | |
| 1598 | + page.reload | |
| 1599 | + assert_equal 1, page.hits | |
| 1600 | + | |
| 1601 | + get :view_page, :profile => profile.identifier, :page => 'myarticle' | |
| 1602 | + page.reload | |
| 1603 | + assert_equal 1, page.hits | |
| 1604 | + end | |
| 1605 | + | |
| 1606 | + should 'not count a visit twice for unlogged users' do | |
| 1607 | + profile = create_user('someone').person | |
| 1608 | + page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text') | |
| 1609 | + page.save! | |
| 1610 | + | |
| 1611 | + get :view_page, :profile => profile.identifier, :page => 'myarticle' | |
| 1612 | + page.reload | |
| 1613 | + assert_equal 1, page.hits | |
| 1614 | + | |
| 1615 | + get :view_page, :profile => profile.identifier, :page => 'myarticle' | |
| 1616 | + page.reload | |
| 1617 | + assert_equal 1, page.hits | |
| 1618 | + end | |
| 1590 | 1619 | end | ... | ... |