Commit fab945682c71f99df896369eb4fac887ae5c6041
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Merge branch 'api' into stable
Showing
5 changed files
with
27 additions
and
0 deletions
Show diff stats
app/models/article.rb
@@ -625,6 +625,11 @@ class Article < ActiveRecord::Base | @@ -625,6 +625,11 @@ class Article < ActiveRecord::Base | ||
625 | self.hits += 1 | 625 | self.hits += 1 |
626 | end | 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 | def can_display_hits? | 633 | def can_display_hits? |
629 | true | 634 | true |
630 | end | 635 | end |
lib/noosfero/api/entities.rb
lib/noosfero/api/v1/articles.rb
@@ -49,6 +49,7 @@ module Noosfero | @@ -49,6 +49,7 @@ module Noosfero | ||
49 | articles = articles.joins('left join votes on articles.id=votes.voteable_id').group('articles.id').reorder('sum(coalesce(votes.vote, 0)) DESC') | 49 | articles = articles.joins('left join votes on articles.id=votes.voteable_id').group('articles.id').reorder('sum(coalesce(votes.vote, 0)) DESC') |
50 | end | 50 | end |
51 | 51 | ||
52 | + Article.hit(articles) | ||
52 | present articles, :with => Entities::Article, :fields => params[:fields] | 53 | present articles, :with => Entities::Article, :fields => params[:fields] |
53 | end | 54 | end |
54 | 55 |
test/unit/api/articles_test.rb
@@ -514,4 +514,14 @@ class ArticlesTest < ActiveSupport::TestCase | @@ -514,4 +514,14 @@ class ArticlesTest < ActiveSupport::TestCase | ||
514 | assert_equal 'SuggestArticle', json['type'] | 514 | assert_equal 'SuggestArticle', json['type'] |
515 | end | 515 | end |
516 | 516 | ||
517 | + should 'update hit attribute of article children' do | ||
518 | + a1 = fast_create(Article, :profile_id => user.person.id) | ||
519 | + a2 = fast_create(Article, :parent_id => a1.id, :profile_id => user.person.id) | ||
520 | + a3 = fast_create(Article, :parent_id => a1.id, :profile_id => user.person.id) | ||
521 | + get "/api/v1/articles/#{a1.id}/children?#{params.to_query}" | ||
522 | + json = JSON.parse(last_response.body) | ||
523 | + assert_equal [1, 1], json['articles'].map { |a| a['hits']} | ||
524 | + assert_equal [0, 1, 1], [a1.reload.hits, a2.reload.hits, a3.reload.hits] | ||
525 | + end | ||
526 | + | ||
517 | end | 527 | end |
test/unit/article_test.rb
@@ -2133,4 +2133,14 @@ class ArticleTest < ActiveSupport::TestCase | @@ -2133,4 +2133,14 @@ class ArticleTest < ActiveSupport::TestCase | ||
2133 | assert_equivalent [a1,a2], Article.display_filter(user, nil) | 2133 | assert_equivalent [a1,a2], Article.display_filter(user, nil) |
2134 | end | 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 | end | 2146 | end |