Commit 4ca9a146282dbf46c914796d9838a373f1987f67

Authored by Larissa Reis
1 parent 0a4f86dd

api: optionally lists parent and children when listing categories

  Always show parent and children when getting a specific category with
  id
lib/noosfero/api/entities.rb
... ... @@ -47,9 +47,19 @@ module Noosfero
47 47 expose :description
48 48 end
49 49  
50   - class Category < Entity
  50 + class CategoryBase < Entity
51 51 root 'categories', 'category'
52   - expose :name, :id, :slug
  52 + expose :name, :id
  53 + end
  54 +
  55 + class Category < CategoryBase
  56 + root 'categories', 'category'
  57 + expose :slug
  58 + expose :full_name do |category, options|
  59 + category.full_name
  60 + end
  61 + expose :parent, :using => CategoryBase, if: { parent: true }
  62 + expose :children, :using => CategoryBase, if: { children: true }
53 63 expose :image, :using => Image
54 64 end
55 65  
... ...
lib/noosfero/api/v1/categories.rb
... ... @@ -8,13 +8,16 @@ module Noosfero
8 8  
9 9 get do
10 10 type = params[:category_type]
  11 + include_parent = params[:include_parent] == 'true'
  12 + include_children = params[:include_children] == 'true'
  13 +
11 14 categories = type.nil? ? environment.categories : environment.categories.find(:all, :conditions => {:type => type})
12   - present categories, :with => Entities::Category
  15 + present categories, :with => Entities::Category, parent: include_parent, children: include_children
13 16 end
14 17  
15 18 desc "Return the category by id"
16 19 get ':id' do
17   - present environment.categories.find(params[:id]), :with => Entities::Category
  20 + present environment.categories.find(params[:id]), :with => Entities::Category, parent: true, children: true
18 21 end
19 22  
20 23 end
... ...
test/unit/api/categories_test.rb
... ... @@ -20,4 +20,67 @@ class CategoriesTest &lt; ActiveSupport::TestCase
20 20 assert_equal category.name, json["category"]["name"]
21 21 end
22 22  
  23 + should 'list parent and children when get category by id' do
  24 + parent = fast_create(Category)
  25 + child_1 = fast_create(Category)
  26 + child_2 = fast_create(Category)
  27 +
  28 + category = fast_create(Category)
  29 + category.parent = parent
  30 + category.children << child_1
  31 + category.children << child_2
  32 + category.save
  33 +
  34 + get "/api/v1/categories/#{category.id}/?#{params.to_query}"
  35 + json = JSON.parse(last_response.body)
  36 + assert_equal({'id' => parent.id, 'name' => parent.name}, json['category']['parent'])
  37 + assert_equivalent [child_1.id, child_2.id], json['category']['children'].map { |c| c['id'] }
  38 + end
  39 +
  40 + should 'include parent in categories list if params is true' do
  41 + parent_1 = fast_create(Category) # parent_1 has no parent category
  42 + child_1 = fast_create(Category)
  43 + child_2 = fast_create(Category)
  44 +
  45 + parent_2 = fast_create(Category)
  46 + parent_2.parent = parent_1
  47 + parent_2.children << child_1
  48 + parent_2.children << child_2
  49 + parent_2.save
  50 +
  51 + get "/api/v1/categories/?#{params.to_query}"
  52 + json = JSON.parse(last_response.body)
  53 + assert_equal [nil], json['categories'].map { |c| c['parent'] }.uniq
  54 +
  55 + params[:include_parent] = true
  56 + get "/api/v1/categories/?#{params.to_query}"
  57 + json = JSON.parse(last_response.body)
  58 + assert_equivalent [parent_1.parent, parent_2.parent.id, child_1.parent.id, child_2.parent.id],
  59 + json["categories"].map { |c| c['parent'] && c['parent']['id'] }
  60 + end
  61 +
  62 + should 'include children in categories list if params is true' do
  63 + category = fast_create(Category)
  64 + child_1 = fast_create(Category)
  65 + child_2 = fast_create(Category)
  66 + child_3 = fast_create(Category)
  67 +
  68 + category.children << child_1
  69 + category.children << child_2
  70 + category.save
  71 +
  72 + child_1.children << child_3
  73 + child_1.save
  74 +
  75 + get "/api/v1/categories/?#{params.to_query}"
  76 + json = JSON.parse(last_response.body)
  77 + assert_equal [nil], json['categories'].map { |c| c['children'] }.uniq
  78 +
  79 + params[:include_children] = true
  80 + get "/api/v1/categories/?#{params.to_query}"
  81 + json = JSON.parse(last_response.body)
  82 + 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],
  83 + json["categories"].map{ |c| c['children'].map{ |child| child['id'] }.sort }
  84 + end
  85 +
23 86 end
... ...