Commit e8398c7d4329070f4b1af2f6e253d1bde1789be5

Authored by Victor Costa
1 parent 1445c0a7
Exists in staging

api: accept parameters to build api content for blocks

app/api/v1/blocks.rb
... ... @@ -6,6 +6,7 @@ module Api
6 6 get ':id' do
7 7 block = Block.find(params["id"])
8 8 return forbidden! unless block.visible_to_user?(current_person) || block.allow_edit?(current_person)
  9 + block.api_content_params = params.except("id")
9 10 present block, :with => Entities::Block, display_api_content: true, current_person: current_person
10 11 end
11 12  
... ...
app/models/block.rb
... ... @@ -320,6 +320,8 @@ class Block < ApplicationRecord
320 320 false
321 321 end
322 322  
  323 + attr_accessor :api_content_params
  324 +
323 325 private
324 326  
325 327 def home_page_path
... ...
test/api/blocks_test.rb
... ... @@ -141,4 +141,18 @@ class BlocksTest < ActiveSupport::TestCase
141 141 json = JSON.parse(last_response.body)
142 142 assert_includes json["block"]["permissions"], 'allow_edit'
143 143 end
  144 +
  145 + should 'get a block with api content params' do
  146 + class MyTestBlock < Block
  147 + def api_content
  148 + api_content_params
  149 + end
  150 + end
  151 + box = fast_create(Box, :owner_id => environment.id, :owner_type => Environment.name)
  152 + block = fast_create(MyTestBlock, box_id: box.id)
  153 + params["custom_param"] = "custom_value"
  154 + get "/api/v1/blocks/#{block.id}?#{params.to_query}"
  155 + json = JSON.parse(last_response.body)
  156 + assert_equal "custom_value", json["block"]["api_content"]["custom_param"]
  157 + end
144 158 end
... ...