Commit 8e635082886e481c2a27287b4a1e53c88161b6f9

Authored by Pedro de Lyra Pereira
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>
app/controllers/public/content_viewer_controller.rb
... ... @@ -276,12 +276,19 @@ class ContentViewerController &lt; ApplicationController
276 276 private
277 277  
278 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 289 return true
283 290 else
284   - cookies[:visited] = { value: current_user.id, expires: 7.days.from_now }
  291 + cookies.permanent.signed[:visited] = current_user.id
285 292 return false;
286 293 end
287 294 end
... ...
test/functional/content_viewer_controller_test.rb
... ... @@ -1587,4 +1587,18 @@ class ContentViewerControllerTest &lt; 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 + 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 1604 end
... ...