Commit b65da5afe7aa265c1b22c0654ac85a7454d641aa

Authored by Victor Costa
Committed by Rodrigo Souto
1 parent 91951aa4

api: update hits for article children

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
... ... @@ -40,6 +40,7 @@ module Noosfero
40 40 articles = articles.joins('left join votes on articles.id=votes.voteable_id').group('articles.id').reorder('sum(coalesce(votes.vote, 0)) DESC')
41 41 end
42 42  
  43 + Article.hit(articles)
43 44 present articles, :with => Entities::Article, :fields => params[:fields]
44 45 end
45 46  
... ...
test/unit/api/articles_test.rb
... ... @@ -473,4 +473,14 @@ class ArticlesTest &lt; ActiveSupport::TestCase
473 473 assert_equal 'SuggestArticle', json['type']
474 474 end
475 475  
  476 + should 'update hit attribute of article children' do
  477 + a1 = fast_create(Article, :profile_id => user.person.id)
  478 + a2 = fast_create(Article, :parent_id => a1.id, :profile_id => user.person.id)
  479 + a3 = fast_create(Article, :parent_id => a1.id, :profile_id => user.person.id)
  480 + get "/api/v1/articles/#{a1.id}/children?#{params.to_query}"
  481 + json = JSON.parse(last_response.body)
  482 + assert_equal [1, 1], json['articles'].map { |a| a['hits']}
  483 + assert_equal [0, 1, 1], [a1.reload.hits, a2.reload.hits, a3.reload.hits]
  484 + end
  485 +
476 486 end
... ...