[Git][noosfero/noosfero][api] api: optionally lists parent and children when listing categories

Larissa Reis gitlab at gitlab.com
Wed Jun 17 18:19:53 BRT 2015


Larissa Reis pushed to branch api at Noosfero / noosfero


Commits:
4ca9a146 by Larissa Reis at 2015-06-17T18:16:28Z
api: optionally lists parent and children when listing categories

  Always show parent and children when getting a specific category with
  id

- - - - -


3 changed files:

- lib/noosfero/api/entities.rb
- lib/noosfero/api/v1/categories.rb
- test/unit/api/categories_test.rb


Changes:

=====================================
lib/noosfero/api/entities.rb
=====================================
--- 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
 


=====================================
lib/noosfero/api/v1/categories.rb
=====================================
--- 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


=====================================
test/unit/api/categories_test.rb
=====================================
--- 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



View it on GitLab: https://gitlab.com/noosfero/noosfero/commit/4ca9a146282dbf46c914796d9838a373f1987f67
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150617/0fcdc01d/attachment-0001.html>


More information about the Noosfero-dev mailing list