Skip to content

Upgrade to new jsonapi-deserializable version. #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion jsonapi-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Gem::Specification.new do |spec|
spec.files = Dir['README.md', 'lib/**/*']
spec.require_path = 'lib'

spec.add_dependency 'jsonapi-rb', '~> 0.2.1'
spec.add_dependency 'jsonapi-rb', '~> 0.3.0'
spec.add_dependency 'jsonapi-parser', '~> 0.1.0'

spec.add_development_dependency 'rails', '~> 5.0'
spec.add_development_dependency 'sqlite3'
Expand Down
35 changes: 22 additions & 13 deletions lib/jsonapi/rails/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,38 @@

module JSONAPI
module Rails
module Deserializable
class Resource < JSONAPI::Deserializable::Resource

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing top-level class documentation comment.

id
type
attributes
has_one do |_rel, id, type, key|
type = type.to_s.singularize.camelize
{ "#{key}_id".to_sym => id, "#{key}_type".to_sym => type }
end
has_many do |_rel, ids, types, key|
key = key.to_s.singularize
types = types.map { |t| t.to_s.singularize.camelize }
{ "#{key}_ids".to_sym => ids, "#{key}_types".to_sym => types }
end
end
end

module ActionController
extend ActiveSupport::Concern

JSONAPI_POINTERS_KEY = 'jsonapi_deserializable.jsonapi_pointers'.freeze

class_methods do
def deserializable_resource(key, options = {}, &block)
_deserializable(key, options,
JSONAPI::Deserializable::Resource, &block)
end

def deserializable_relationship(key, options = {}, &block)
_deserializable(key, options,
JSONAPI::Deserializable::Relationship, &block)
end

# @api private
def _deserializable(key, options, fallback, &block)
options = options.dup
klass = options.delete(:class) || Class.new(fallback, &block)
klass = options.delete(:class) ||
Class.new(JSONAPI::Rails::Deserializable::Resource, &block)

before_action(options) do |controller|
resource = klass.new(controller.params[:_jsonapi].to_unsafe_hash)
hash = controller.params[:_jsonapi].to_unsafe_hash
JSONAPI::Parser::Resource.parse!(hash)
resource = klass.new(hash[:data])
controller.request.env[JSONAPI_POINTERS_KEY] =
resource.reverse_mapping
controller.params[key.to_sym] = resource.to_hash
Expand Down
14 changes: 0 additions & 14 deletions lib/jsonapi/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,6 @@ class Railtie < ::Rails::Railtie

RENDERERS[:jsonapi_error].render(errors, options).to_json
end

JSONAPI::Deserializable::Resource.configure do |config|
config.default_has_one do |key, _rel, id, type|
key = key.to_s.singularize
type = type.to_s.singularize.camelize
{ "#{key}_id".to_sym => id, "#{key}_type".to_sym => type }
end

config.default_has_many do |key, _rel, ids, types|
key = key.to_s.singularize
types = types.map { |t| t.to_s.singularize.camelize }
{ "#{key}_ids".to_sym => ids, "#{key}_types".to_sym => types }
end
end
end
end
end
Expand Down
95 changes: 76 additions & 19 deletions spec/action_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,96 @@

describe ActionController::Base, type: :controller do
describe '.deserializable_resource' do
controller do
deserializable_resource :user

def create
render plain: 'ok'
end
end

let(:payload) do
{
_jsonapi: {
'data' => {
'type' => 'users',
'attributes' => { 'foo' => 'bar', 'bar' => 'baz' }
'attributes' => { 'name' => 'Lucas' }
}
}
}
end

it 'makes the deserialized resource available in params' do
post :create, params: payload
context 'when using default deserializer' do
controller do
deserializable_resource :user

def create
render plain: 'ok'
end
end

it 'makes the deserialized resource available in params' do
post :create, params: payload

expected = { 'type' => 'users', 'name' => 'Lucas' }
expect(controller.params[:user]).to eq(expected)
end

it 'makes the deserialization mapping available via #jsonapi_pointers' do
post :create, params: payload

expected = { name: '/data/attributes/name',
type: '/data/type' }
expect(controller.jsonapi_pointers).to eq(expected)
end
end

context 'when using a customized deserializer' do
controller do
deserializable_resource :user do
attribute(:name) do |val|
{ 'first_name'.to_sym => val }
end
end

def create
render plain: 'ok'
end
end

expected = { 'type' => 'users', 'foo' => 'bar', 'bar' => 'baz' }
expect(controller.params[:user]).to eq(expected)
it 'makes the deserialized resource available in params' do
post :create, params: payload

expected = { 'type' => 'users', 'first_name' => 'Lucas' }
expect(controller.params[:user]).to eq(expected)
end

it 'makes the deserialization mapping available via #jsonapi_pointers' do
post :create, params: payload

expected = { first_name: '/data/attributes/name',
type: '/data/type' }
expect(controller.jsonapi_pointers).to eq(expected)
end
end

it 'makes the deserialization mapping available via #jsonapi_pointers' do
post :create, params: payload
context 'when using a customized deserializer with key_format' do
controller do
deserializable_resource :user do
key_format(&:capitalize)
end

expected = { foo: '/data/attributes/foo',
bar: '/data/attributes/bar',
type: '/data/type' }
expect(controller.jsonapi_pointers).to eq(expected)
def create
render plain: 'ok'
end
end

it 'makes the deserialized resource available in params' do
post :create, params: payload

expected = { 'type' => 'users', 'Name' => 'Lucas' }
expect(controller.params[:user]).to eq(expected)
end

it 'makes the deserialization mapping available via #jsonapi_pointers' do
post :create, params: payload

expected = { Name: '/data/attributes/name',
type: '/data/type' }
expect(controller.jsonapi_pointers).to eq(expected)
end
end
end

Expand Down
2 changes: 0 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'jsonapi/parser'

# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
Expand Down