Commit 9305c767bb76d5154e8aa29b1811c6c28e40645c

Authored by Victor Costa
Committed by Rodrigo Souto
1 parent 37ec8a33

api: return partial data

lib/noosfero/api/entities.rb
... ... @@ -2,11 +2,11 @@ module Noosfero
2 2 module API
3 3 module Entities
4 4  
5   - Grape::Entity.format_with :timestamp do |date|
  5 + Entity.format_with :timestamp do |date|
6 6 date.strftime('%Y/%m/%d %H:%M:%S') if date
7 7 end
8 8  
9   - class Image < Grape::Entity
  9 + class Image < Entity
10 10 root 'images', 'image'
11 11  
12 12 expose :icon_url do |image, options|
... ... @@ -26,7 +26,7 @@ module Noosfero
26 26 end
27 27 end
28 28  
29   - class Profile < Grape::Entity
  29 + class Profile < Entity
30 30 expose :identifier, :name, :id
31 31 expose :created_at, :format_with => :timestamp
32 32 expose :image, :using => Image
... ... @@ -43,13 +43,13 @@ module Noosfero
43 43 expose :description
44 44 end
45 45  
46   - class Category < Grape::Entity
  46 + class Category < Entity
47 47 root 'categories', 'category'
48 48 expose :name, :id, :slug
49 49 expose :image, :using => Image
50 50 end
51 51  
52   - class ArticleChild < Grape::Entity
  52 + class ArticleChild < Entity
53 53 root 'articles', 'article'
54 54 expose :id, :body, :abstract
55 55 expose :created_at, :format_with => :timestamp
... ... @@ -59,8 +59,8 @@ module Noosfero
59 59 expose :categories, :using => Category
60 60 expose :image, :using => Image
61 61 end
62   -
63   - class Article < Grape::Entity
  62 +
  63 + class Article < Entity
64 64 root 'articles', 'article'
65 65 expose :id, :body, :abstract
66 66 expose :created_at, :format_with => :timestamp
... ... @@ -69,12 +69,12 @@ module Noosfero
69 69 expose :profile, :using => Profile
70 70 expose :categories, :using => Category
71 71 # FIXME: create a method that overrides expose and include conditions for return attributes
72   - expose :parent, :using => Article, :if => lambda { |article, options| options[:fields].blank? || options[:fields].include?(:parent) }
  72 + expose :parent, :using => Article
73 73 expose :children, :using => ArticleChild
74 74 expose :image, :using => Image
75 75 end
76 76  
77   - class Comment < Grape::Entity
  77 + class Comment < Entity
78 78 root 'comments', 'comment'
79 79 expose :body, :title, :id
80 80 expose :created_at, :format_with => :timestamp
... ... @@ -82,7 +82,7 @@ module Noosfero
82 82 end
83 83  
84 84  
85   - class User < Grape::Entity
  85 + class User < Entity
86 86 root 'users', 'user'
87 87 expose :id
88 88 expose :login
... ...
lib/noosfero/api/entity.rb 0 → 100644
... ... @@ -0,0 +1,17 @@
  1 +class Noosfero::API::Entity < Grape::Entity
  2 +
  3 + def self.fields_condition(fields)
  4 + lambda do |object, options|
  5 + return true if options[:fields].blank?
  6 + fields.map { |field| options[:fields].include?(field.to_s)}.grep(true).present?
  7 + end
  8 + end
  9 +
  10 + def self.expose(*args, &block)
  11 + hash = args.last.is_a?(Hash) ? args.pop : {}
  12 + hash.merge!({:if => fields_condition(args)}) if hash[:if].blank?
  13 + args << hash
  14 + super
  15 + end
  16 +
  17 +end
... ...
lib/noosfero/api/v1/articles.rb
... ... @@ -33,7 +33,7 @@ module Noosfero
33 33 article = find_article(environment.articles, params[:id])
34 34 articles = select_filtered_collection_of(article, 'children', params)
35 35 articles = articles.display_filter(current_person, nil)
36   - present articles, :with => Entities::Article, :fields => [:id, :name, :abstract, :author]
  36 + present articles, :with => Entities::Article, :fields => params[:fields]
37 37 end
38 38  
39 39 get ':id/children/:child_id' do
... ...
test/unit/api/articles_test.rb
... ... @@ -442,5 +442,13 @@ class ArticlesTest &lt; ActiveSupport::TestCase
442 442 assert_equal user.person, Article.last.last_changed_by
443 443 end
444 444  
  445 + should 'list article children with partial fields' do
  446 + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
  447 + child1 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing")
  448 + params[:fields] = [:title]
  449 + get "/api/v1/articles/#{article.id}/children?#{params.to_query}"
  450 + json = JSON.parse(last_response.body)
  451 + assert_equal ['title'], json['articles'].first.keys
  452 + end
445 453  
446 454 end
... ...