Commit eae70c57f9f6f16fe64ed9d33d2d89ee556e7f58

Authored by Victor Costa
1 parent c49c0f4d

api: display content when get a specific block

app/models/block.rb
@@ -314,6 +314,14 @@ class Block < ApplicationRecord @@ -314,6 +314,14 @@ class Block < ApplicationRecord
314 self.observers << block 314 self.observers << block
315 end 315 end
316 316
  317 + def api_content
  318 + nil
  319 + end
  320 +
  321 + def display_api_content_by_default?
  322 + false
  323 + end
  324 +
317 private 325 private
318 326
319 def home_page_path 327 def home_page_path
lib/noosfero/api/entities.rb
@@ -88,6 +88,7 @@ module Noosfero @@ -88,6 +88,7 @@ module Noosfero
88 root 'blocks', 'block' 88 root 'blocks', 'block'
89 expose :id, :type, :settings, :position, :enabled 89 expose :id, :type, :settings, :position, :enabled
90 expose :mirror, :mirror_block_id, :title 90 expose :mirror, :mirror_block_id, :title
  91 + expose :api_content, if: lambda { |object, options| options[:display_api_content] || object.display_api_content_by_default? }
91 end 92 end
92 93
93 class Box < Entity 94 class Box < Entity
lib/noosfero/api/v1/blocks.rb
@@ -9,7 +9,7 @@ module Noosfero @@ -9,7 +9,7 @@ module Noosfero
9 if block.owner.kind_of?(Profile) 9 if block.owner.kind_of?(Profile)
10 return forbidden! unless block.owner.display_info_to?(current_person) 10 return forbidden! unless block.owner.display_info_to?(current_person)
11 end 11 end
12 - present block, :with => Entities::Block 12 + present block, :with => Entities::Block, display_api_content: true
13 end 13 end
14 end 14 end
15 end 15 end
test/api/blocks_test.rb
@@ -62,4 +62,25 @@ class BlocksTest &lt; ActiveSupport::TestCase @@ -62,4 +62,25 @@ class BlocksTest &lt; ActiveSupport::TestCase
62 json = JSON.parse(last_response.body) 62 json = JSON.parse(last_response.body)
63 assert_equal block.id, json["block"]["id"] 63 assert_equal block.id, json["block"]["id"]
64 end 64 end
  65 +
  66 + should 'display api content by default' do
  67 + box = fast_create(Box, :owner_id => environment.id, :owner_type => Environment.name)
  68 + block = fast_create(Block, box_id: box.id)
  69 + get "/api/v1/blocks/#{block.id}?#{params.to_query}"
  70 + json = JSON.parse(last_response.body)
  71 + assert json["block"].key?('api_content')
  72 + end
  73 +
  74 + should 'display api content of a specific block' do
  75 + class SomeBlock < Block
  76 + def api_content
  77 + {some_content: { name: 'test'} }
  78 + end
  79 + end
  80 + box = fast_create(Box, :owner_id => environment.id, :owner_type => Environment.name)
  81 + block = fast_create(SomeBlock, box_id: box.id)
  82 + get "/api/v1/blocks/#{block.id}?#{params.to_query}"
  83 + json = JSON.parse(last_response.body)
  84 + assert_equal "test", json["block"]["api_content"]["some_content"]["name"]
  85 + end
65 end 86 end
test/api/boxes_test.rb
@@ -38,4 +38,13 @@ class BoxesTest &lt; ActiveSupport::TestCase @@ -38,4 +38,13 @@ class BoxesTest &lt; ActiveSupport::TestCase
38 assert_equal box.id, json["boxes"].first["id"] 38 assert_equal box.id, json["boxes"].first["id"]
39 end 39 end
40 40
  41 + should 'not display block api_content by default' do
  42 + Environment.delete_all
  43 + environment = fast_create(Environment, :is_default => true)
  44 + box = fast_create(Box, :owner_id => environment.id, :owner_type => 'Environment')
  45 + block = fast_create(Block, box_id: box.id)
  46 + get "/api/v1/environments/default/boxes?#{params.to_query}"
  47 + json = JSON.parse(last_response.body)
  48 + assert !json["boxes"].first["blocks"].first.key?('api_content')
  49 + end
41 end 50 end