Commit a78018eb0e088b1b2e29d9161016291df61c084a
1 parent
8ff9e1ef
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
adding person fields in entities
Showing
2 changed files
with
16 additions
and
6 deletions
Show diff stats
lib/noosfero/api/entities.rb
| ... | ... | @@ -69,12 +69,16 @@ module Noosfero |
| 69 | 69 | class Person < Profile |
| 70 | 70 | root 'people', 'person' |
| 71 | 71 | expose :user, :using => UserBasic, documentation: {type: 'User', desc: 'The user data of a person' } |
| 72 | -# expose :vote_count, if: lambda { |object, options| options[:fields].include?('vote_count') == :full } | |
| 73 | - expose :vote_count | |
| 74 | - expose :comments_count do |person, options| | |
| 72 | + expose :vote_count, if: lambda { |object, options| options[:fields].present? ? options[:fields].include?('vote_count') : false} | |
| 73 | + expose :comments_count, if: lambda { |object, options| options[:fields].present? ? options[:fields].include?('comments_count') : false} do |person, options| | |
| 75 | 74 | person.comments.count |
| 76 | 75 | end |
| 77 | - | |
| 76 | + expose :following_articles_count, if: lambda { |object, options| options[:fields].present? ? options[:fields].include?('following_articles_count') : false} do |person, options| | |
| 77 | + person.following_articles.count | |
| 78 | + end | |
| 79 | + expose :articles_count, if: lambda { |object, options| options[:fields].present? ? options[:fields].include?('articles_count') : false} do |person, options| | |
| 80 | + person.articles.count | |
| 81 | + end | |
| 78 | 82 | |
| 79 | 83 | end |
| 80 | 84 | ... | ... |
test/unit/api/people_test.rb
| ... | ... | @@ -180,13 +180,19 @@ class PeopleTest < ActiveSupport::TestCase |
| 180 | 180 | assert_equal another_name, person.name |
| 181 | 181 | end |
| 182 | 182 | |
| 183 | - PERSON_ATTRIBUTES = %w(vote_count comments_count) | |
| 183 | + PERSON_ATTRIBUTES = %w(vote_count comments_count following_articles_count articles_count) | |
| 184 | 184 | |
| 185 | 185 | PERSON_ATTRIBUTES.map do |attribute| |
| 186 | 186 | |
| 187 | - define_method "test_should_expose_#{attribute}_attribute_in_person_enpoints" do | |
| 187 | + define_method "test_should_not_expose_#{attribute}_attribute_in_person_enpoint_if_field_parameter_is_not_passed" do | |
| 188 | 188 | get "/api/v1/people/me?#{params.to_query}" |
| 189 | 189 | json = JSON.parse(last_response.body) |
| 190 | + assert_nil json['person'][attribute] | |
| 191 | + end | |
| 192 | + | |
| 193 | + define_method "test_should_expose_#{attribute}_attribute_in_person_enpoints_only_if_field_parameter_is_passed" do | |
| 194 | + get "/api/v1/people/me?#{params.to_query}&fields=#{attribute}" | |
| 195 | + json = JSON.parse(last_response.body) | |
| 190 | 196 | assert_not_nil json['person'][attribute] |
| 191 | 197 | end |
| 192 | 198 | ... | ... |