Commit 91951aa4068493fff78e960969c247f525f73f22
Committed by
Rodrigo Souto
1 parent
16d23214
Exists in
staging
and in
41 other branches
api: multiple articles hits
Showing
2 changed files
with
15 additions
and
0 deletions
Show diff stats
app/models/article.rb
@@ -634,6 +634,11 @@ class Article < ActiveRecord::Base | @@ -634,6 +634,11 @@ class Article < ActiveRecord::Base | ||
634 | self.hits += 1 | 634 | self.hits += 1 |
635 | end | 635 | end |
636 | 636 | ||
637 | + def self.hit(articles) | ||
638 | + Article.where(:id => articles.map(&:id)).update_all('hits = hits + 1') | ||
639 | + articles.each { |a| a.hits += 1 } | ||
640 | + end | ||
641 | + | ||
637 | def can_display_hits? | 642 | def can_display_hits? |
638 | true | 643 | true |
639 | end | 644 | end |
test/unit/article_test.rb
@@ -2154,4 +2154,14 @@ class ArticleTest < ActiveSupport::TestCase | @@ -2154,4 +2154,14 @@ class ArticleTest < ActiveSupport::TestCase | ||
2154 | assert_equivalent [a1,a2], Article.display_filter(nil, user) | 2154 | assert_equivalent [a1,a2], Article.display_filter(nil, user) |
2155 | end | 2155 | end |
2156 | 2156 | ||
2157 | + should 'update hit attribute of article array' do | ||
2158 | + a1 = fast_create(Article) | ||
2159 | + a2 = fast_create(Article) | ||
2160 | + a3 = fast_create(Article) | ||
2161 | + Article.hit([a1, a2, a3]) | ||
2162 | + Article.hit([a2, a3]) | ||
2163 | + assert_equal [1, 2, 2], [a1.hits, a2.hits, a3.hits] | ||
2164 | + assert_equal [1, 2, 2], [a1.reload.hits, a2.reload.hits, a3.reload.hits] | ||
2165 | + end | ||
2166 | + | ||
2157 | end | 2167 | end |