Commit fab945682c71f99df896369eb4fac887ae5c6041

Authored by Victor Costa
2 parents e189a8ad c3ab49f6

Merge branch 'api' into stable

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
... ...
lib/noosfero/api/entities.rb
... ... @@ -68,6 +68,7 @@ module Noosfero
68 68 expose :votes_against
69 69 expose :setting
70 70 expose :position
  71 + expose :hits
71 72 end
72 73  
73 74 class Article < ArticleBase
... ...
lib/noosfero/api/v1/articles.rb
... ... @@ -49,6 +49,7 @@ module Noosfero
49 49 articles = articles.joins('left join votes on articles.id=votes.voteable_id').group('articles.id').reorder('sum(coalesce(votes.vote, 0)) DESC')
50 50 end
51 51  
  52 + Article.hit(articles)
52 53 present articles, :with => Entities::Article, :fields => params[:fields]
53 54 end
54 55  
... ...
test/unit/api/articles_test.rb
... ... @@ -514,4 +514,14 @@ class ArticlesTest &lt; ActiveSupport::TestCase
514 514 assert_equal 'SuggestArticle', json['type']
515 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 527 end
... ...
test/unit/article_test.rb
... ... @@ -2133,4 +2133,14 @@ class ArticleTest &lt; 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
... ...