File tree Expand file tree Collapse file tree 12 files changed +166
-52
lines changed Expand file tree Collapse file tree 12 files changed +166
-52
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ source "https://rubygems.org"
2
2
3
3
# Specify your gem's dependencies in jsonapi_compliable.gemspec
4
4
gemspec
5
- gem 'active_model_serializers ' , git : 'https://github.com/richmolj/active_model_serializers.git '
5
+ gem 'jsonapi-serializable ' , path : '../serializable '
6
6
7
7
group :test do
8
8
gem 'appraisal'
Original file line number Diff line number Diff line change @@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
19
19
20
20
spec . add_dependency "rails" , [ '>= 4.1' , '< 6' ]
21
21
spec . add_dependency "jsonapi" , '~> 0.1.1.beta2'
22
- spec . add_dependency "active_model_serializers" , "~> 0.10"
23
- spec . add_dependency "jsonapi_ams_extensions" , "~> 0.1"
22
+
23
+ spec . add_dependency 'jsonapi-rails'
24
24
25
25
spec . add_development_dependency "kaminari"
26
26
spec . add_development_dependency "active_model_serializers"
Original file line number Diff line number Diff line change 1
- require 'active_model_serializers'
2
- require 'jsonapi'
3
- require 'jsonapi_ams_extensions'
1
+ require 'jsonapi/rails'
4
2
5
3
require "jsonapi_compliable/version"
6
4
require "jsonapi_compliable/errors"
19
17
require "jsonapi_compliable/util/field_params"
20
18
require "jsonapi_compliable/util/scoping"
21
19
require "jsonapi_compliable/util/pagination"
20
+ require "jsonapi_compliable/extensions/extra_attribute"
21
+ require "jsonapi_compliable/extensions/boolean_attribute"
22
22
23
23
require 'jsonapi_compliable/railtie' if defined? ( ::Rails )
24
24
Original file line number Diff line number Diff line change @@ -57,10 +57,12 @@ def render_ams(scope, opts = {})
57
57
options [ :include ] = forced_includes || Util ::IncludeParams . scrub ( self )
58
58
options [ :jsonapi ] = JsonapiCompliable ::Util ::Pagination . zero? ( params ) ? [ ] : scoped
59
59
options [ :fields ] = Util ::FieldParams . fieldset ( params , :fields ) if params [ :fields ]
60
- options [ :extra_fields ] = Util ::FieldParams . fieldset ( params , :extra_fields ) if params [ :extra_fields ]
61
60
options [ :meta ] ||= { }
62
61
options . merge! ( opts )
63
62
options [ :meta ] [ :stats ] = Stats ::Payload . new ( self , scoped ) . generate if params [ :stats ]
63
+ options [ :expose ] ||= { }
64
+ options [ :expose ] [ :context ] = self
65
+ options [ :expose ] [ :extra_fields ] = Util ::FieldParams . fieldset ( params , :extra_fields ) if params [ :extra_fields ]
64
66
65
67
render ( options )
66
68
end
@@ -69,7 +71,6 @@ def render_ams(scope, opts = {})
69
71
# render json: foo, ams_default_options
70
72
def default_ams_options
71
73
{ } . tap do |options |
72
- options [ :adapter ] = :json_api
73
74
end
74
75
end
75
76
Original file line number Diff line number Diff line change
1
+ require 'jsonapi/serializable/conditional_fields'
2
+
3
+ module JsonapiCompliable
4
+ module Extensions
5
+ module BooleanAttribute
6
+ def self . included ( klass )
7
+ klass . extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def boolean_attribute ( name , options = { } , &blk )
12
+ blk ||= proc { @object . public_send ( name ) }
13
+ field_name = :"is_#{ name . to_s . gsub ( '?' , '' ) } "
14
+ attribute field_name , options , &blk
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ JSONAPI ::Serializable ::Resource . class_eval do
22
+ include JsonapiCompliable ::Extensions ::BooleanAttribute
23
+ end
Original file line number Diff line number Diff line change
1
+ require 'jsonapi/serializable/conditional_fields'
2
+
3
+ module JsonapiCompliable
4
+ module Extensions
5
+ module ExtraAttribute
6
+ def self . included ( klass )
7
+ klass . extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def extra_attribute ( name , options = { } , &blk )
12
+ allow_field = proc {
13
+ if options [ :if ]
14
+ next false unless instance_eval ( &options [ :if ] )
15
+ end
16
+
17
+ if @extra_fields && @extra_fields [ jsonapi_type ]
18
+ @extra_fields [ jsonapi_type ] . include? ( name )
19
+ else
20
+ false
21
+ end
22
+ }
23
+
24
+ attribute name , if : allow_field , &blk
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ JSONAPI ::Serializable ::Resource . class_eval do
32
+ prepend JSONAPI ::Serializable ::ConditionalFields
33
+ include JsonapiCompliable ::Extensions ::ExtraAttribute
34
+ end
Original file line number Diff line number Diff line change 5
5
6
6
module ActiveModelSerializers
7
7
class Railtie < Rails ::Railtie
8
- initializer 'jsonapi_compliable.register_renderer' do
9
- require 'active_model_serializers/register_jsonapi_renderer'
10
- end
8
+ # initializer 'jsonapi_compliable.register_renderer' do
9
+ # require 'active_model_serializers/register_jsonapi_renderer'
10
+ # end
11
11
12
- initializer 'jsonapi_compliable.configure_ams' do
13
- if ActiveModelSerializers . config . respond_to? ( :include_data_default )
14
- ActiveModelSerializers . config . include_data_default = :if_sideloaded
15
- end
16
- end
12
+ # initializer 'jsonapi_compliable.configure_ams' do
13
+ # if ActiveModelSerializers.config.respond_to?(:include_data_default)
14
+ # ActiveModelSerializers.config.include_data_default = :if_sideloaded
15
+ # end
16
+ # end
17
17
end
18
18
end
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ RSpec . describe '.boolean_attribute' do
4
+ let ( :klass ) do
5
+ Class . new ( JSONAPI ::Serializable ::Resource ) do
6
+ type 'authors'
7
+
8
+ boolean_attribute :celebrity?
9
+ end
10
+ end
11
+
12
+ let ( :author ) { double ( id : 1 ) }
13
+ let ( :resource ) { klass . new ( object : author ) }
14
+
15
+ subject { resource . as_jsonapi [ :attributes ] }
16
+
17
+ before do
18
+ allow ( author ) . to receive ( :celebrity? ) { true }
19
+ end
20
+
21
+ it { is_expected . to eq ( is_celebrity : true ) }
22
+
23
+ context 'when supplied a block' do
24
+ before do
25
+ klass . class_eval do
26
+ boolean_attribute :alive? do
27
+ 'yesss'
28
+ end
29
+ end
30
+ end
31
+
32
+ it { is_expected . to include ( is_alive : 'yesss' ) }
33
+ end
34
+ end
Original file line number Diff line number Diff line change 1
1
require 'spec_helper'
2
+ require 'jsonapi/serializable/conditional_fields'
2
3
3
4
RSpec . describe 'extra_fields' , type : :controller do
4
- class TestExtraFieldsSerializer < ActiveModel ::Serializer
5
- include JsonapiAmsExtensions
5
+ class SerializableTestExtraFields < JSONAPI ::Serializable ::Resource
6
+ prepend JSONAPI ::Serializable ::ConditionalFields
7
+ type 'authors'
6
8
attributes :first_name , :last_name
7
- extra_attribute :net_worth
8
-
9
- def net_worth
9
+ extra_attribute :net_worth , if : proc { @context . allow_net_worth? } do
10
10
100_000_000
11
11
end
12
12
end
@@ -18,8 +18,12 @@ def net_worth
18
18
end
19
19
end
20
20
21
+ def allow_net_worth?
22
+ true
23
+ end
24
+
21
25
def index
22
- render_ams ( Author . all , each_serializer : TestExtraFieldsSerializer )
26
+ render_ams ( Author . all , class : SerializableTestExtraFields )
23
27
end
24
28
end
25
29
@@ -57,8 +61,7 @@ def include_foo!
57
61
58
62
context 'when extra field is requested but guarded' do
59
63
before do
60
- allow_any_instance_of ( TestExtraFieldsSerializer )
61
- . to receive ( :allow_net_worth? ) { false }
64
+ allow ( controller ) . to receive ( :allow_net_worth? ) { false }
62
65
end
63
66
64
67
it 'does not include the extra field in the response' do
Original file line number Diff line number Diff line change 1
1
require 'spec_helper'
2
+ require 'jsonapi/serializable/conditional_fields'
2
3
3
4
RSpec . describe 'fields' , type : :controller do
4
5
controller ( ApplicationController ) do
5
6
jsonapi { }
6
7
7
- class TestFieldsSerializer < ActiveModel :: Serializer
8
- attributes :first_name , :last_name , :uuid
9
- attribute :salary , if : :admin?
8
+ class SerializableTestFields < JSONAPI :: Serializable :: Resource
9
+ prepend JSONAPI :: Serializable :: ConditionalFields
10
+ type 'authors'
10
11
11
- def uuid
12
+ attribute :first_name
13
+ attribute :last_name
14
+ attribute :uuid do
12
15
SecureRandom . uuid
13
16
end
17
+ attribute :salary , if : proc { @context . current_user == 'admin' } do
18
+ 50_000
19
+ end
14
20
15
21
def admin?
16
22
scope == 'admin'
17
23
end
18
-
19
- def salary
20
- 50_000
21
- end
22
24
end
23
25
24
26
def index
25
- render_ams ( Author . all , each_serializer : TestFieldsSerializer )
27
+ render_ams ( Author . all , class : SerializableTestFields )
26
28
end
27
29
28
30
def current_user
You can’t perform that action at this time.
0 commit comments