From 4ca9a146282dbf46c914796d9838a373f1987f67 Mon Sep 17 00:00:00 2001 From: Larissa Reis Date: Wed, 17 Jun 2015 18:16:28 -0300 Subject: [PATCH] api: optionally lists parent and children when listing categories --- lib/noosfero/api/entities.rb | 14 ++++++++++++-- lib/noosfero/api/v1/categories.rb | 7 +++++-- test/unit/api/categories_test.rb | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/lib/noosfero/api/entities.rb b/lib/noosfero/api/entities.rb index e342a5d..506724e 100644 --- a/lib/noosfero/api/entities.rb +++ b/lib/noosfero/api/entities.rb @@ -47,9 +47,19 @@ module Noosfero expose :description end - class Category < Entity + class CategoryBase < Entity root 'categories', 'category' - expose :name, :id, :slug + expose :name, :id + end + + class Category < CategoryBase + root 'categories', 'category' + expose :slug + expose :full_name do |category, options| + category.full_name + end + expose :parent, :using => CategoryBase, if: { parent: true } + expose :children, :using => CategoryBase, if: { children: true } expose :image, :using => Image end diff --git a/lib/noosfero/api/v1/categories.rb b/lib/noosfero/api/v1/categories.rb index 4b65d8d..fa8d1f6 100644 --- a/lib/noosfero/api/v1/categories.rb +++ b/lib/noosfero/api/v1/categories.rb @@ -8,13 +8,16 @@ module Noosfero get do type = params[:category_type] + include_parent = params[:include_parent] == 'true' + include_children = params[:include_children] == 'true' + categories = type.nil? ? environment.categories : environment.categories.find(:all, :conditions => {:type => type}) - present categories, :with => Entities::Category + present categories, :with => Entities::Category, parent: include_parent, children: include_children end desc "Return the category by id" get ':id' do - present environment.categories.find(params[:id]), :with => Entities::Category + present environment.categories.find(params[:id]), :with => Entities::Category, parent: true, children: true end end diff --git a/test/unit/api/categories_test.rb b/test/unit/api/categories_test.rb index 9c5fedf..24b8244 100644 --- a/test/unit/api/categories_test.rb +++ b/test/unit/api/categories_test.rb @@ -20,4 +20,67 @@ class CategoriesTest < ActiveSupport::TestCase assert_equal category.name, json["category"]["name"] end + should 'list parent and children when get category by id' do + parent = fast_create(Category) + child_1 = fast_create(Category) + child_2 = fast_create(Category) + + category = fast_create(Category) + category.parent = parent + category.children << child_1 + category.children << child_2 + category.save + + get "/api/v1/categories/#{category.id}/?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal({'id' => parent.id, 'name' => parent.name}, json['category']['parent']) + assert_equivalent [child_1.id, child_2.id], json['category']['children'].map { |c| c['id'] } + end + + should 'include parent in categories list if params is true' do + parent_1 = fast_create(Category) # parent_1 has no parent category + child_1 = fast_create(Category) + child_2 = fast_create(Category) + + parent_2 = fast_create(Category) + parent_2.parent = parent_1 + parent_2.children << child_1 + parent_2.children << child_2 + parent_2.save + + get "/api/v1/categories/?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal [nil], json['categories'].map { |c| c['parent'] }.uniq + + params[:include_parent] = true + get "/api/v1/categories/?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equivalent [parent_1.parent, parent_2.parent.id, child_1.parent.id, child_2.parent.id], + json["categories"].map { |c| c['parent'] && c['parent']['id'] } + end + + should 'include children in categories list if params is true' do + category = fast_create(Category) + child_1 = fast_create(Category) + child_2 = fast_create(Category) + child_3 = fast_create(Category) + + category.children << child_1 + category.children << child_2 + category.save + + child_1.children << child_3 + child_1.save + + get "/api/v1/categories/?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal [nil], json['categories'].map { |c| c['children'] }.uniq + + params[:include_children] = true + get "/api/v1/categories/?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equivalent [category.children.map(&:id).sort, child_1.children.map(&:id).sort, child_2.children.map(&:id).sort, child_3.children.map(&:id).sort], + json["categories"].map{ |c| c['children'].map{ |child| child['id'] }.sort } + end + end -- libgit2 0.21.2