Skip to content

Commit 93e38ed

Browse files
committed
Upgrade to upcoming jsonapi-deserializable version.
1 parent a8d47c2 commit 93e38ed

File tree

5 files changed

+96
-49
lines changed

5 files changed

+96
-49
lines changed

jsonapi-rails.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
1414
spec.files = Dir['README.md', 'lib/**/*']
1515
spec.require_path = 'lib'
1616

17-
spec.add_dependency 'jsonapi-rb', '~> 0.2.1'
17+
spec.add_dependency 'jsonapi-rb', '~> 0.3.0'
1818

1919
spec.add_development_dependency 'rails', '~> 5.0'
2020
spec.add_development_dependency 'sqlite3'

lib/jsonapi/rails/action_controller.rb

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
require 'jsonapi/deserializable'
2-
require 'jsonapi/parser'
32

43
module JSONAPI
54
module Rails
5+
module Deserializable
6+
class Resource < JSONAPI::Deserializable::Resource
7+
id
8+
type
9+
attributes
10+
has_one do |_rel, id, type, key|
11+
type = type.to_s.singularize.camelize
12+
{ "#{key}_id".to_sym => id, "#{key}_type".to_sym => type }
13+
end
14+
has_many do |_rel, ids, types, key|
15+
key = key.to_s.singularize
16+
types = types.map { |t| t.to_s.singularize.camelize }
17+
{ "#{key}_ids".to_sym => ids, "#{key}_types".to_sym => types }
18+
end
19+
end
20+
end
21+
622
module ActionController
723
extend ActiveSupport::Concern
824

925
JSONAPI_POINTERS_KEY = 'jsonapi_deserializable.jsonapi_pointers'.freeze
1026

1127
class_methods do
1228
def deserializable_resource(key, options = {}, &block)
13-
_deserializable(key, options,
14-
JSONAPI::Deserializable::Resource, &block)
15-
end
16-
17-
def deserializable_relationship(key, options = {}, &block)
18-
_deserializable(key, options,
19-
JSONAPI::Deserializable::Relationship, &block)
20-
end
21-
22-
# @api private
23-
def _deserializable(key, options, fallback, &block)
2429
options = options.dup
25-
klass = options.delete(:class) || Class.new(fallback, &block)
30+
klass = options.delete(:class) ||
31+
Class.new(JSONAPI::Rails::Deserializable::Resource, &block)
2632

2733
before_action(options) do |controller|
2834
resource = klass.new(controller.params[:_jsonapi].to_unsafe_hash)

lib/jsonapi/rails/railtie.rb

-14
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,6 @@ class Railtie < ::Rails::Railtie
4141

4242
RENDERERS[:jsonapi_error].render(errors, options).to_json
4343
end
44-
45-
JSONAPI::Deserializable::Resource.configure do |config|
46-
config.default_has_one do |key, _rel, id, type|
47-
key = key.to_s.singularize
48-
type = type.to_s.singularize.camelize
49-
{ "#{key}_id".to_sym => id, "#{key}_type".to_sym => type }
50-
end
51-
52-
config.default_has_many do |key, _rel, ids, types|
53-
key = key.to_s.singularize
54-
types = types.map { |t| t.to_s.singularize.camelize }
55-
{ "#{key}_ids".to_sym => ids, "#{key}_types".to_sym => types }
56-
end
57-
end
5844
end
5945
end
6046
end

spec/action_controller_spec.rb

+76-19
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,96 @@
22

33
describe ActionController::Base, type: :controller do
44
describe '.deserializable_resource' do
5-
controller do
6-
deserializable_resource :user
7-
8-
def create
9-
render plain: 'ok'
10-
end
11-
end
12-
135
let(:payload) do
146
{
157
_jsonapi: {
168
'data' => {
179
'type' => 'users',
18-
'attributes' => { 'foo' => 'bar', 'bar' => 'baz' }
10+
'attributes' => { 'name' => 'Lucas' }
1911
}
2012
}
2113
}
2214
end
2315

24-
it 'makes the deserialized resource available in params' do
25-
post :create, params: payload
16+
context 'when using default deserializer' do
17+
controller do
18+
deserializable_resource :user
19+
20+
def create
21+
render plain: 'ok'
22+
end
23+
end
24+
25+
it 'makes the deserialized resource available in params' do
26+
post :create, params: payload
27+
28+
expected = { 'type' => 'users', 'name' => 'Lucas' }
29+
expect(controller.params[:user]).to eq(expected)
30+
end
31+
32+
it 'makes the deserialization mapping available via #jsonapi_pointers' do
33+
post :create, params: payload
34+
35+
expected = { name: '/data/attributes/name',
36+
type: '/data/type' }
37+
expect(controller.jsonapi_pointers).to eq(expected)
38+
end
39+
end
40+
41+
context 'when using a customized deserializer' do
42+
controller do
43+
deserializable_resource :user do
44+
attribute(:name) do |val|
45+
{ 'first_name'.to_sym => val }
46+
end
47+
end
48+
49+
def create
50+
render plain: 'ok'
51+
end
52+
end
2653

27-
expected = { 'type' => 'users', 'foo' => 'bar', 'bar' => 'baz' }
28-
expect(controller.params[:user]).to eq(expected)
54+
it 'makes the deserialized resource available in params' do
55+
post :create, params: payload
56+
57+
expected = { 'type' => 'users', 'first_name' => 'Lucas' }
58+
expect(controller.params[:user]).to eq(expected)
59+
end
60+
61+
it 'makes the deserialization mapping available via #jsonapi_pointers' do
62+
post :create, params: payload
63+
64+
expected = { first_name: '/data/attributes/name',
65+
type: '/data/type' }
66+
expect(controller.jsonapi_pointers).to eq(expected)
67+
end
2968
end
3069

31-
it 'makes the deserialization mapping available via #jsonapi_pointers' do
32-
post :create, params: payload
70+
context 'when using a customized deserializer with key_format' do
71+
controller do
72+
deserializable_resource :user do
73+
key_format(&:capitalize)
74+
end
3375

34-
expected = { foo: '/data/attributes/foo',
35-
bar: '/data/attributes/bar',
36-
type: '/data/type' }
37-
expect(controller.jsonapi_pointers).to eq(expected)
76+
def create
77+
render plain: 'ok'
78+
end
79+
end
80+
81+
it 'makes the deserialized resource available in params' do
82+
post :create, params: payload
83+
84+
expected = { 'type' => 'users', 'Name' => 'Lucas' }
85+
expect(controller.params[:user]).to eq(expected)
86+
end
87+
88+
it 'makes the deserialization mapping available via #jsonapi_pointers' do
89+
post :create, params: payload
90+
91+
expected = { Name: '/data/attributes/name',
92+
type: '/data/type' }
93+
expect(controller.jsonapi_pointers).to eq(expected)
94+
end
3895
end
3996
end
4097

spec/spec_helper.rb

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'jsonapi/parser'
2-
31
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
42
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
53
# The generated `.rspec` file contains `--require spec_helper` which will cause

0 commit comments

Comments
 (0)