diff --git a/lib/noosfero/api/entities.rb b/lib/noosfero/api/entities.rb index 9839263..af6e5f9 100644 --- a/lib/noosfero/api/entities.rb +++ b/lib/noosfero/api/entities.rb @@ -68,6 +68,7 @@ module Noosfero expose :votes_against expose :setting expose :position + expose :hits end class Article < ArticleBase diff --git a/lib/noosfero/api/v1/articles.rb b/lib/noosfero/api/v1/articles.rb index 6ca0e36..0e20aa4 100644 --- a/lib/noosfero/api/v1/articles.rb +++ b/lib/noosfero/api/v1/articles.rb @@ -40,6 +40,7 @@ module Noosfero articles = articles.joins('left join votes on articles.id=votes.voteable_id').group('articles.id').reorder('sum(coalesce(votes.vote, 0)) DESC') end + Article.hit(articles) present articles, :with => Entities::Article, :fields => params[:fields] end diff --git a/test/unit/api/articles_test.rb b/test/unit/api/articles_test.rb index 9edaa46..5469f43 100644 --- a/test/unit/api/articles_test.rb +++ b/test/unit/api/articles_test.rb @@ -473,4 +473,14 @@ class ArticlesTest < ActiveSupport::TestCase assert_equal 'SuggestArticle', json['type'] end + should 'update hit attribute of article children' do + a1 = fast_create(Article, :profile_id => user.person.id) + a2 = fast_create(Article, :parent_id => a1.id, :profile_id => user.person.id) + a3 = fast_create(Article, :parent_id => a1.id, :profile_id => user.person.id) + get "/api/v1/articles/#{a1.id}/children?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal [1, 1], json['articles'].map { |a| a['hits']} + assert_equal [0, 1, 1], [a1.reload.hits, a2.reload.hits, a3.reload.hits] + end + end -- libgit2 0.21.2