diff --git a/lib/noosfero/api/entities.rb b/lib/noosfero/api/entities.rb index 5166410..d04194e 100644 --- a/lib/noosfero/api/entities.rb +++ b/lib/noosfero/api/entities.rb @@ -69,12 +69,16 @@ module Noosfero class Person < Profile root 'people', 'person' expose :user, :using => UserBasic, documentation: {type: 'User', desc: 'The user data of a person' } -# expose :vote_count, if: lambda { |object, options| options[:fields].include?('vote_count') == :full } - expose :vote_count - expose :comments_count do |person, options| + expose :vote_count, if: lambda { |object, options| options[:fields].present? ? options[:fields].include?('vote_count') : false} + expose :comments_count, if: lambda { |object, options| options[:fields].present? ? options[:fields].include?('comments_count') : false} do |person, options| person.comments.count end - + expose :following_articles_count, if: lambda { |object, options| options[:fields].present? ? options[:fields].include?('following_articles_count') : false} do |person, options| + person.following_articles.count + end + expose :articles_count, if: lambda { |object, options| options[:fields].present? ? options[:fields].include?('articles_count') : false} do |person, options| + person.articles.count + end end diff --git a/test/unit/api/people_test.rb b/test/unit/api/people_test.rb index 1540921..dbfe3db 100644 --- a/test/unit/api/people_test.rb +++ b/test/unit/api/people_test.rb @@ -180,13 +180,19 @@ class PeopleTest < ActiveSupport::TestCase assert_equal another_name, person.name end - PERSON_ATTRIBUTES = %w(vote_count comments_count) + PERSON_ATTRIBUTES = %w(vote_count comments_count following_articles_count articles_count) PERSON_ATTRIBUTES.map do |attribute| - define_method "test_should_expose_#{attribute}_attribute_in_person_enpoints" do + define_method "test_should_not_expose_#{attribute}_attribute_in_person_enpoint_if_field_parameter_is_not_passed" do get "/api/v1/people/me?#{params.to_query}" json = JSON.parse(last_response.body) + assert_nil json['person'][attribute] + end + + define_method "test_should_expose_#{attribute}_attribute_in_person_enpoints_only_if_field_parameter_is_passed" do + get "/api/v1/people/me?#{params.to_query}&fields=#{attribute}" + json = JSON.parse(last_response.body) assert_not_nil json['person'][attribute] end -- libgit2 0.21.2