Commit 1fdf2205c67f1d79909ec9564d23a5d78fa6cedc

Authored by Victor Costa
1 parent 10c4dc4a

api: multiple articles hits

app/models/article.rb
... ... @@ -625,6 +625,11 @@ class Article < ActiveRecord::Base
625 625 self.hits += 1
626 626 end
627 627  
  628 + def self.hit(articles)
  629 + Article.where(:id => articles.map(&:id)).update_all('hits = hits + 1')
  630 + articles.each { |a| a.hits += 1 }
  631 + end
  632 +
628 633 def can_display_hits?
629 634 true
630 635 end
... ...
test/unit/article_test.rb
... ... @@ -2133,4 +2133,14 @@ class ArticleTest < ActiveSupport::TestCase
2133 2133 assert_equivalent [a1,a2], Article.display_filter(user, nil)
2134 2134 end
2135 2135  
  2136 + should 'update hit attribute of article array' do
  2137 + a1 = fast_create(Article)
  2138 + a2 = fast_create(Article)
  2139 + a3 = fast_create(Article)
  2140 + Article.hit([a1, a2, a3])
  2141 + Article.hit([a2, a3])
  2142 + assert_equal [1, 2, 2], [a1.hits, a2.hits, a3.hits]
  2143 + assert_equal [1, 2, 2], [a1.reload.hits, a2.reload.hits, a3.reload.hits]
  2144 + end
  2145 +
2136 2146 end
... ...