Skip to content

jsonapi-rb/jsonapi-rails

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsonapi-rails

Rails integration for jsonapi-rb.

Status

Gem Version Build Status Gitter chat

Installation

Add the following to your application's Gemfile:

gem 'jsonapi-rails'

And then execute:

$ bundle

Usage

Serialization

Example:

# app/serializable/serializable_user.rb
class SerializableUser < JSONAPI::Serializable::Resource
  type 'users'

  attribute :name
  attribute :email do
    "#{@object.name}@foo.bar"
  end

  has_many :posts do
    link(:related) { @url_helpers.user_posts(@object) }
    meta foo: :bar
  end

  has_many :comments do
    resources do
      @user.comments.order(:desc)
    end
  end

  has_many :reviews, Foo::Bar::SerializableRev

  link(:self) { @url_helpers.user_url(@object.id) }
  meta do
    { foo: 'bar' }
  end
end

# app/controllers/users_controller.rb
# ...
user = User.find_by(id: id)
render jsonapi: user, include: { posts: [:comments] }, meta: { foo: 'bar' }
# ...

Deserialization

Example:

class PostsController < ActionController::Base
  deserializable_resource :post, only: [:create, :update] do
    attribute :title
    attribute :date
    has_one :author do |rel, id, type|
      field user_id: id
      field user_type: type
    end
    has_many :comments
  end

  def create_params
    params.require(:user).permit!
  end

  def create
    create_params[:title]
    create_params[:date]
    create_params[:comment_ids]
    create_params[:comment_types]
    create_params[:user_id]
    create_params[:user_type]
    # ...
  end
end

License

jsonapi-rails is released under the MIT License.

Packages

No packages published

Contributors 15