Active record result and transformed JSON - ruby-on-rails

I need to transform active record JSON to something like this:
{
cols: [{id: 'task', label: 'Task', type: 'string'},
{id: 'hours', label: 'Hours per Day', type: 'number'}],
rows: [{c:[{v: 'Work'}, {v: 11}]},
{c:[{v: 'Eat'}, {v: 2}]},
{c:[{v: 'Commute'}, {v: 2}]},
{c:[{v: 'Watch TV'}, {v:2}]},
{c:[{v: 'Sleep'}, {v:7, f:'7.000'}]}
]
}
That is totally different from what to_json returns from activerecord. What is the most ruby way to transform JSON?

Override the to_json method in your model
# your_model.rb, implement an instance method to_json
def to_json(options = {})
{
'cols' => [{'id' => 'whateveryoulike'}],
'rows' => [{'id' => 'whateveryoulike'}]
}.to_json(options)
end
Remember, it is important to accept options as parameter to this method and pass it on to to_json of the hash (or any other to_json call you make inside this method, for that matter). Otherwise, the method may not behave as expected on collection JSON serialization. And of course since you haven't given any details as to what your model is and how it maps to the desired JSON response, you will have to implement the representation of cols and rows as you like.
This also applies to to_xml.

Related

Rails - permit a param of unknown type (string, hash, array, or fixnum)

My model has a custom_fields column that serializes an array of hashes. Each of these hashes has a value attribute, which can be a hash, array, string, or fixnum. What could I do to permit this value attribute regardless of its type?
My current permitted params line looks something like:
params.require(:model_name).permit([
:field_one,
:field_two,
custom_fields: [:value]
])
Is there any way I can modify this to accept when value is an unknown type?
What you want can probably be done, but will take some work. Your best bet is this post: http://blog.trackets.com/2013/08/17/strong-parameters-by-example.html
This is not my work, but I have used the technique they outline in an app I wrote. The part you are looking for is at the end:
params = ActionController::Parameters.new(user: { username: "john", data: { foo: "bar" } })
# let's assume we can't do this because the data hash can contain any kind of data
params.require(:user).permit(:username, data: [ :foo ])
# we need to use the power of ruby to do this "by hand"
params.require(:user).permit(:username).tap do |whitelisted|
whitelisted[:data] = params[:user][:data]
end
# Unpermitted parameters: data
# => { "username" => "john", "data" => {"foo"=>"bar"} }
That blog post helped me understand params and I still refer to it when I need to brush up on the details.

Hash to Active Model Serializer

I have a hash #branches that is basically:
{1 => 5}, {2 => 6}
Is it possible for me to send this to a serializer and get output json like so:
{ branch_id: 1, branch_name: 'Hello', count_5}
I've made a custom serializer and calling it like so:
render json: #branches, serializer: AvilableStockBranchSerializer
How can I pass the hash data to the serializer?
Why do you need AMS?
You can use #branches.to_json instead

RABL - Render collection as object with the ids as keys

In Ruby on Rails 4, I'm trying to make an API for my website and instead of using an array like so:
[{id: 1, name: "John"}, {id: 2, name: "Foo"}, {id: 3, name: "Bar"}]
I want to render it like this, because it makes it much easier to search through in javascript (and for other reasons):
{"1": {id: 1, name: "John"}, "2": {id: 2, name: "Foo"}, "3": {id: 3, name: "Bar"}}
This works:
# users/index.rabl
#users.each do |user|
node(users.id.to_s) do
partial('api/v1/users/show', object: user)
end
end
But in the partial, I want another collection of elements (which belong to the user) and I can't get that working. Is there a more elegant way to do this?
To choose hash-map rather than array is definitely better option if have control on API backend codebase. From BigO notation hash lookup happens with constant O(1) time, not O(n) as for array.
Primary key lookup from the database is the most performant way to query data. Primary key is usually short and always indexed. In case of big data set use pagination.
Let's assume there is no RABL (you can always instantiate pure Ruby classes in RABL DSL code) but just an array:
array = [{id: 1, name: "John"}, {id: 2, name: "Foo"}, {id: 3, name: "Bar"}]
hash = {}
array.each{ |elem,i| hash[elem[:id].to_s] = elem }
# {"1"=>{:id=>1, :name=>"John"}, "2"=>{:id=>2, :name=>"Foo"}, "3"=>{:id=>3, :name=>"Bar"}}
To pass the Ruby hash to Javascript on client you probably want to encode it appropriately:
# hash.to_json
# {"1":{"id":1,"name":"John"},"2":{"id":2,"name":"Foo"},"3":{"id":3,"name":"Bar"}}
From Javascript you query hash by its key:
hash = {"1":{"id":1,"name":"John"},"2":{"id":2,"name":"Foo"},"3":{"id":3,"name":"Bar"}}
hash[1]
# Object { id=1, name="John"}

How to rename fields to get into json hash

I am trying to rename columns once they come out of a database query so I can get them into the correct format for a d3.js graph. This requires the fields to be named source,target,value.
def self.including_relationships
User.select(:follower_id,:followed_id,:value)
#How do I rename the fields from the line above to the fields below
obj['links'] << a.slice('source', 'target', 'value')
end
end
User.select('follower_id, followed_id, value').map(&:attributes)
will give you an array of Hash like bellow.
[{"follower_id" => 1, "followed_id" => 2, "value" => 1},
{"follower_id" => 1, "followed_id" => 2, "value" => 1},
..........
{"follower_id" => 1, "followed_id" => 2, "value" => 1},]
which can be stored as a json casting it.
> [{"follower_id" => 1, "followed_id" => 2, "value" => 1}].to_json
=> "[{\"follower_id\":1,\"followed_id\":2,\"value\":1}]"
You could use ActiveModel::Serializers or JBuilder to customise the JSON output but you should instead consider manipulating the data in javascript.
By doing so you avoid any strong couplings between your server side API and whatever graphing library you use.
Your API output should not be controlled by how you are displaying the data.
A simplified example using jQuery:
var promise = $.getJSON('/users.json');
promise.then(function(data){
return jQuery.map(data, function(user){
return {
target: user.follower_id,
source: user.followed_id,
value: user.value
}
}).toArray();
});
promise.done(function(data){
// draw the graph.
});

Rails model to hash, exclude attributes

I am trying to form a json response that looks like this:
{
"user": {
"birthday": "2013-03-13",
"email": "example#example",
"id": 1,
"name": null,
"username": "example"
},
"other_data": "foo"
}
Before, when I was just returning the user, I used
render :json => #user, :except => [:hashed_password, :created_at, :updated_at]
to keep the hashed_password, created_at, and updated_at attributes from being sent. Is there a way to do this, but also allow additional data to be sent along with the user? Right now I'm just adding the attributes I want to send to the hash one by one, but this is obviously not ideal.
Rendering json data first automagically calls 'as_json' on your model, which returns a ruby hash. After that, 'to_json' is called on that to get a string representation of your hash.
To achieve what you wanted, you can call something like this:
render :json => {
:user => #user.as_json(:except => [:hashed_password]),
:some_other_data => {}
}
In this case, there is no object which responds to 'as_json', so the controller just calls 'to_json' to turn your hash to a string.
I would recommend to use this gem: https://github.com/fabrik42/acts_as_api

Resources