I want to add some meta data about the response in my JSON output, but RABL insert them inside the handled collection. Here is my template:
views/clients/show.rabl
node(:status) { response.status }
node(:time) { Time.now }
collection @client
attributes :name, :base_url, :city
attribute c_at: :created_at
node(:items_count) { |c| c.items.count }
node(:users_count) { |c| c.users.count }
This gives me the following JSON response:
{"client":{"status":200,"time":"2015-04-16 13:45:46 +0200","name":"This is a test","base_url":"http://test.com","city":"Paris","created_at":"2015-04-15 10:38:44 UTC","items_count":3,"users_count":0}}
Here, status
and time
information are inside the client
collection. How can I extract them, to have the following output:
{"time":"...","status":200,"client":{...}}
Found it. The magic comes with object false
. Here is the answer.
object false
node(:status) { response.status }
node(:time) { Time.now }
child @client do
attributes :name, :base_url, :city
attribute c_at: :created_at
node(:items_count) { |c| c.items.count }
node(:users_count) { |c| c.users.count }
end
Hope it will help.