From 9305c767bb76d5154e8aa29b1811c6c28e40645c Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Thu, 16 Apr 2015 11:40:22 -0300 Subject: [PATCH] api: return partial data --- lib/noosfero/api/entities.rb | 20 ++++++++++---------- lib/noosfero/api/entity.rb | 17 +++++++++++++++++ lib/noosfero/api/v1/articles.rb | 2 +- test/unit/api/articles_test.rb | 8 ++++++++ 4 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 lib/noosfero/api/entity.rb diff --git a/lib/noosfero/api/entities.rb b/lib/noosfero/api/entities.rb index 991458b..85da32c 100644 --- a/lib/noosfero/api/entities.rb +++ b/lib/noosfero/api/entities.rb @@ -2,11 +2,11 @@ module Noosfero module API module Entities - Grape::Entity.format_with :timestamp do |date| + Entity.format_with :timestamp do |date| date.strftime('%Y/%m/%d %H:%M:%S') if date end - class Image < Grape::Entity + class Image < Entity root 'images', 'image' expose :icon_url do |image, options| @@ -26,7 +26,7 @@ module Noosfero end end - class Profile < Grape::Entity + class Profile < Entity expose :identifier, :name, :id expose :created_at, :format_with => :timestamp expose :image, :using => Image @@ -43,13 +43,13 @@ module Noosfero expose :description end - class Category < Grape::Entity + class Category < Entity root 'categories', 'category' expose :name, :id, :slug expose :image, :using => Image end - class ArticleChild < Grape::Entity + class ArticleChild < Entity root 'articles', 'article' expose :id, :body, :abstract expose :created_at, :format_with => :timestamp @@ -59,8 +59,8 @@ module Noosfero expose :categories, :using => Category expose :image, :using => Image end - - class Article < Grape::Entity + + class Article < Entity root 'articles', 'article' expose :id, :body, :abstract expose :created_at, :format_with => :timestamp @@ -69,12 +69,12 @@ module Noosfero expose :profile, :using => Profile expose :categories, :using => Category # FIXME: create a method that overrides expose and include conditions for return attributes - expose :parent, :using => Article, :if => lambda { |article, options| options[:fields].blank? || options[:fields].include?(:parent) } + expose :parent, :using => Article expose :children, :using => ArticleChild expose :image, :using => Image end - class Comment < Grape::Entity + class Comment < Entity root 'comments', 'comment' expose :body, :title, :id expose :created_at, :format_with => :timestamp @@ -82,7 +82,7 @@ module Noosfero end - class User < Grape::Entity + class User < Entity root 'users', 'user' expose :id expose :login diff --git a/lib/noosfero/api/entity.rb b/lib/noosfero/api/entity.rb new file mode 100644 index 0000000..6a0d869 --- /dev/null +++ b/lib/noosfero/api/entity.rb @@ -0,0 +1,17 @@ +class Noosfero::API::Entity < Grape::Entity + + def self.fields_condition(fields) + lambda do |object, options| + return true if options[:fields].blank? + fields.map { |field| options[:fields].include?(field.to_s)}.grep(true).present? + end + end + + def self.expose(*args, &block) + hash = args.last.is_a?(Hash) ? args.pop : {} + hash.merge!({:if => fields_condition(args)}) if hash[:if].blank? + args << hash + super + end + +end diff --git a/lib/noosfero/api/v1/articles.rb b/lib/noosfero/api/v1/articles.rb index 6ee4b3a..c854c46 100644 --- a/lib/noosfero/api/v1/articles.rb +++ b/lib/noosfero/api/v1/articles.rb @@ -33,7 +33,7 @@ module Noosfero article = find_article(environment.articles, params[:id]) articles = select_filtered_collection_of(article, 'children', params) articles = articles.display_filter(current_person, nil) - present articles, :with => Entities::Article, :fields => [:id, :name, :abstract, :author] + present articles, :with => Entities::Article, :fields => params[:fields] end get ':id/children/:child_id' do diff --git a/test/unit/api/articles_test.rb b/test/unit/api/articles_test.rb index 6d57c79..d856f0c 100644 --- a/test/unit/api/articles_test.rb +++ b/test/unit/api/articles_test.rb @@ -442,5 +442,13 @@ class ArticlesTest < ActiveSupport::TestCase assert_equal user.person, Article.last.last_changed_by end + should 'list article children with partial fields' do + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + child1 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing") + params[:fields] = [:title] + get "/api/v1/articles/#{article.id}/children?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal ['title'], json['articles'].first.keys + end end -- libgit2 0.21.2