Rabl conforming to jsonapi.org - ruby-on-rails

I'm having a lot of trouble getting rabl to conform the the jsonapi.org spec. I have seen the wiki entry on github regarding this (https://github.com/nesquena/rabl/wiki/Conforming-to-jsonapi.org-format) but I dont understand how to get multiple root level nodes in rabl and still have collections work. The wiki entry on collections says this should work
collection [#post] => :posts
attributes :id, :title
child #post => :links do
node(:author) { #post.author_id }
node(:comments) { #post.comments_ids }
end
which for single root documents it does, but as soon as I try to add a meta or links to the root of the document as declared in the jsonapi.org spec, those nodes are appended to the existing collection node. Here is my rabl_init.rb
require 'rabl'
Rabl.configure do |config|
config.cache_sources = Rails.env != 'development'
config.include_child_root = false
config.include_json_root = false
end
I would like json that looks like this:
{
"links": {
"posts.comments": "http://example.com/posts/{posts.id}/comments"
},
"posts": [{
"id": "1",
"title": "Rails is Omakase"
}, {
"id": "2",
"title": "The Parley Letter"
}]
}
is Rabl capable of doing this?

Try something like this:
object false
node(:links) do
{"posts.comments" => "http://example.com/posts/{posts.id}/comments"}
end
child(#posts) do
attributes :id, :title
end

Related

Add "namespace" to serializer

I have User model existing in my db, however I would like to return json response with active_model_serializers gem in which user attributes are encapsulated/nested in player namespace which DOES NOT exist in db (say it is virtual and this is arbitrary expected response).
Instead of:
{
"email": "some#mail.com",
"first_name": "Hey",
"last_name": "Hoo",
"birthdate": "1540-05-05",
"phone_number": "856539571"
}
I would like to have:
{
"player":
{
"email": "some#mail.com",
"first_name": "Hey",
"last_name": "Hoo",
"birthdate": "1540-05-05",
"phone_number": "856539571"
}
}
When Nwocha's answer is correct I will add more details to it.
As for documentation says:
Overriding the resource root only applies when using the JSON adapter.
Normally, the resource root is derived from the class name of the resource being serialized. e.g. UserPostSerializer.new(UserPost.new) will be serialized with the root user_post or user_posts according the adapter collection pluralization rules.
When using the JSON adapter in your initializer (ActiveModelSerializers.config.adapter = :json), or passing in the adapter in your render call, you can specify the root by passing it as an argument to render. For example:
render json: #user_post, root: "admin_post", adapter: :json
This will be rendered as:
{
"admin_post": {
"title": "how to do open source"
}
}
Note: the Attributes adapter (default) does not include a resource root. You also will not be able to create a single top-level root if you are using the :json_api adapter.
Within the UserSerializer class, define the root attribute. E.g:
class UserSerializer < ActiveModel::Serializer
root :player
...
end
Answer provided before works as well, but in the end I used slightly different approach. Turned out that aside player there should be another json, let's say team, which makes final response look like this:
{
"player":
{
"email": "some#mail.com",
...
},
"team":
{
"name": "Fancy Team",
...
}
}
What I actually did was to define exact json that I wanted to use, like this:
class UserSerializer < ActiveRecord::Serializer
attributes :player
attributes :team
def player
{
email: object.email,
...
}
end
def team
{
name: object.name,
...
}
end
end
If I used root option in render json:, whole serializer would be encapsulated in this name. Sorry for not clearing it at the beginning.

Rails define custom json structure

I'm attempting to structure a json response to mimic an existing structure we have elsewhere in our application (using jbuilder templates). In this specific use case, we are unable to use the jbuilder template because we are preparing the json data for a live update job being fired from a model method, not responding to a server call in the controller.
Desired structure:
{"notes": {
"1": {
"id": "1",
"name": "Jane",
...
},
"2": {
"id": "2",
"name": "Joe",
...
}
}
}
Jbuilder Template (for reference):
json.notes do
for note in notes
json.set! note.id.to_s do
json.id note.id.to_s
json.name note.name
...
end
end
end
I've tried defining a to_builder method in the model class (below), per the jbuilder docs, and active model serializers but can't seem to get the hashes nested under the id attribute. Any direction would be appreciated!
to_builder method
def to_builder
Jbuilder.new do |note|
note.set! self.id do
note.(self, :id, :name, ...)
end
end
end
How about just plain Ruby?
notes.each_with_object({"notes": {}}) do |note, hash|
hash["notes"][note.id.to_s] = note.as_json
end
Or AMS:
adapter = ActiveModelSerializers::Adapter
notes.each_with_object({"notes": {}}) do |note, hash|
hash["notes"][note.id.to_s] = adapter.create(NoteSerializer.new(note))
end

Rails 4 render json with multiple objects and includes

I have a Rails 4 API. When a user search in the view for boats, this method is executed getting all the boats matching the search filters and return an array of boat models as json using render ActiveModel and the :include and :only like this:
render :json => #boats, :include => { :mainPhoto => {:only => [:name, :mime]},
:year => {:only => :name},
# other includes...}
This is working great.
But, additional to this information, in the view, I would like to show the total count of boats like "showing 1 - 20 of 80 boats" because there is a pagination funcionality. So, the point is I need to provide the 80 boats. I would like to avoid send two requests that execute almost the same logic, so the idea is to run the searchBoats method just once and in the result provide the list of boats and the total number of boats in a variable numTotalBoats. I understand numTotalBoats is not a boat model attribute. So, I think it should go in an independent variable in the render result.
Something like:
render :json => {boats: #boats with all the includes, numTotalBoats: #NumTotalBoats}
I tried thousands of combinations, but or I´m getting syntax errors or none of them is returning the expected result, something like
{boats: [boat1 with all the includes, boat2 with all the includes, ... boatN with all the includes], numTotalBoats: N}
Without adding any gems:
def index
boats = Boat.includes(:year)
render json: {
boats: boats.as_json(include: { year: { only: :name } }),
numTotalBoats: boats.count
}
end
At some point though, I believe you should use stand-alone serializers:
Note: Depending on whether you're using pagination gem or not, you might need to change .count calls below to .total_count (for Kaminari) or something else that will read the count correctly from paginated collection.
I recommend using ActiveModel Serializers and this is how it would be accomplished for your case.
Start by adding the gem to Gemfile:
gem 'active_model_serializers', '~-> 0.10'
Overwrite the adapter in config/initializers/active_model_serializer.rb:
ActiveModelSerializers.config.adapter = :json
Define serializers for your models,
# app/serializers/boat_serializer.rb
class BoatSerializer < ActiveModel::Serializer
attributes :name
has_one :year
end
# app/serializers/year_serializer.rb
class YearSerializer < ActiveModel::Serializer
attributes :name
end
And finally, in your controller:
boats = Boat.includes(:year)
render json: boats, meta: boats.count, meta_key: "numTotalBoats"
And you will achieve:
{
"boats": [
{
"name": "Boaty McBoatFace",
"year": {
"name": "2018"
}
},
{
"name": "Titanic",
"year": {
"name": "1911"
}
}
],
"numTotalBoats": 2
}
Adding that count in each index controller is a bit tedious, so I usually end up defining my own adapters or collection serializers in order to take care of that automatically (Tested with Rails 5, not 4).
# lib/active_model_serializers/adapter/json_extended.rb
module ActiveModelSerializers
module Adapter
class JsonExtended < Json
def meta
if serializer.object.is_a?(ActiveRecord::Relation)
{ total_count: serializer.object.count }
end.to_h.merge(instance_options.fetch(:meta, {})).presence
end
end
end
end
# config/initializers/active_model_serializer.rb
ActiveModelSerializers.config.adapter = ActiveModelSerializers::Adapter::JsonExtended
# make sure to add lib to eager load paths
# config/application.rb
config.eager_load_paths << Rails.root.join("lib")
And now your index action can look like this
def index
boats = Boat.includes(:year)
render json: boats
end
And output:
{
"boats": [
{
"name": "Boaty McBoatFace",
"year": {
"name": "2018"
}
},
{
"name": "Titanic",
"year": {
"name": "1911"
}
}
],
"meta": {
"total_count": 2
}
}
I think it's a little easier to parse this count for different endpoints and you will get it automatically while responding with a collection, so your controllers will be a little simpler.

Rails Active model serializer returns array instead of json

I am using Rails to create APIs containing basic todo information: name, list, and items. I want it to return json format, to look something like this:
{
"data": [
{
"type": "posts",
"id": "1",
"attributes": {
"title": "JSON API is awesome!",
"body": "You should be using JSON API",
"created": "2015-05-22T14:56:29.000Z",
"updated": "2015-05-22T14:56:28.000Z"
}
}
],
"links": {
"href": "http://example.com/api/posts",
"meta": {
"count": 10
}
}
}
^code from Active Serializer's Github.
When I look on my localhost http://localhost:3000/api/users/, it shows
[{"id":1,"username":"Iggy1","items":[{"id":1,"list_id":1,"name":"Wash dishes","completed":true},{"id":7,"list_id":1,"name":"Finish this assignment","completed":false}],"lists":[{"id":1,"name":"Important!","user_id":1,"permission":"private"},...
It is returning an array of hashes. I am sure I missed an important step when I was setting up my serializer. How can I reformat my array of hashes into JSON API format?
I've read getting started guide, rendering, and JSON API section but I still couldn't figure it out. I might have overlooked it.
Some of my codes:
app/serializers/user_serializer.rb
class UserSerializer < ActiveModel::Serializer
attributes :id, :username#, :email
has_many :items, through: :lists
has_many :lists
end
app/controllers/api/users_controller.rb
def index
#users = User.all
render json: #users, each_serializer: UserSerializer
end
routes
Rails.application.routes.draw do
namespace :api, defaults: { format: :json } do
resources :users do
resources :lists
end
end
end
Let me know if I can clarify it better. Thanks!!
(Answer from comments)
To use the JSON API adapter, you need to declare you want to use it.
ActiveModelSerializers.config.adapter = ActiveModelSerializers::Adapter::JsonApi
As per the AMS readme.
ActiveModelSerializers.config.adapter = :json_api # Default: `:attributes`
from 0-10-stable documentation
However, this did not solve my problem. It was due to the Hash being nested as discussed in this SO post

Rails 3: Wrapping as_json response with additional lines

I was very happy to learn of as_json to make my code DRY. And I've added the following to a model:
class ProductType < ActiveRecord::Base
has_many :component_types
def as_json(parameter)
{:name => self.name,
:description => self.description,
:children => self.componentTypes}
end
end
This is great. The only thing is that for my client side application, I need to wrap the response I get into this format, (where "items" contains what is created by as_json):
{
"identifier": "name",
"label": "name",
"items":
[
{
"name": "myName1",
"description": "myDesc1",
"children":[]
},
{
"name": "myName2",
"description": "myDesc2",
"children":[]
}
]
}
There are a lot of limitations to overriding as_json, and your issue is one of them. I'd suggest having a look at the RABL gem as I think it will help you reach your goal.

Resources