Skip to content

Commit 3410475

Browse files
author
Lee Richmond
committed
Switch to jsonapi-rb
1 parent 7faee00 commit 3410475

12 files changed

+166
-52
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ source "https://rubygems.org"
22

33
# Specify your gem's dependencies in jsonapi_compliable.gemspec
44
gemspec
5-
gem 'active_model_serializers', git: 'https://github.com/richmolj/active_model_serializers.git'
5+
gem 'jsonapi-serializable', path: '../serializable'
66

77
group :test do
88
gem 'appraisal'

jsonapi_compliable.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
1919

2020
spec.add_dependency "rails", ['>= 4.1', '< 6']
2121
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'
2424

2525
spec.add_development_dependency "kaminari"
2626
spec.add_development_dependency "active_model_serializers"

lib/jsonapi_compliable.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
require 'active_model_serializers'
2-
require 'jsonapi'
3-
require 'jsonapi_ams_extensions'
1+
require 'jsonapi/rails'
42

53
require "jsonapi_compliable/version"
64
require "jsonapi_compliable/errors"
@@ -19,6 +17,8 @@
1917
require "jsonapi_compliable/util/field_params"
2018
require "jsonapi_compliable/util/scoping"
2119
require "jsonapi_compliable/util/pagination"
20+
require "jsonapi_compliable/extensions/extra_attribute"
21+
require "jsonapi_compliable/extensions/boolean_attribute"
2222

2323
require 'jsonapi_compliable/railtie' if defined?(::Rails)
2424

lib/jsonapi_compliable/base.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ def render_ams(scope, opts = {})
5757
options[:include] = forced_includes || Util::IncludeParams.scrub(self)
5858
options[:jsonapi] = JsonapiCompliable::Util::Pagination.zero?(params) ? [] : scoped
5959
options[:fields] = Util::FieldParams.fieldset(params, :fields) if params[:fields]
60-
options[:extra_fields] = Util::FieldParams.fieldset(params, :extra_fields) if params[:extra_fields]
6160
options[:meta] ||= {}
6261
options.merge!(opts)
6362
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]
6466

6567
render(options)
6668
end
@@ -69,7 +71,6 @@ def render_ams(scope, opts = {})
6971
# render json: foo, ams_default_options
7072
def default_ams_options
7173
{}.tap do |options|
72-
options[:adapter] = :json_api
7374
end
7475
end
7576

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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

lib/jsonapi_compliable/railtie.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
module ActiveModelSerializers
77
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
1111

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
1717
end
1818
end

spec/boolean_attribute_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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

spec/extra_fields_spec.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
require 'spec_helper'
2+
require 'jsonapi/serializable/conditional_fields'
23

34
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'
68
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
1010
100_000_000
1111
end
1212
end
@@ -18,8 +18,12 @@ def net_worth
1818
end
1919
end
2020

21+
def allow_net_worth?
22+
true
23+
end
24+
2125
def index
22-
render_ams(Author.all, each_serializer: TestExtraFieldsSerializer)
26+
render_ams(Author.all, class: SerializableTestExtraFields)
2327
end
2428
end
2529

@@ -57,8 +61,7 @@ def include_foo!
5761

5862
context 'when extra field is requested but guarded' do
5963
before do
60-
allow_any_instance_of(TestExtraFieldsSerializer)
61-
.to receive(:allow_net_worth?) { false }
64+
allow(controller).to receive(:allow_net_worth?) { false }
6265
end
6366

6467
it 'does not include the extra field in the response' do

spec/fields_spec.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
require 'spec_helper'
2+
require 'jsonapi/serializable/conditional_fields'
23

34
RSpec.describe 'fields', type: :controller do
45
controller(ApplicationController) do
56
jsonapi {}
67

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'
1011

11-
def uuid
12+
attribute :first_name
13+
attribute :last_name
14+
attribute :uuid do
1215
SecureRandom.uuid
1316
end
17+
attribute :salary, if: proc { @context.current_user == 'admin' } do
18+
50_000
19+
end
1420

1521
def admin?
1622
scope == 'admin'
1723
end
18-
19-
def salary
20-
50_000
21-
end
2224
end
2325

2426
def index
25-
render_ams(Author.all, each_serializer: TestFieldsSerializer)
27+
render_ams(Author.all, class: SerializableTestFields)
2628
end
2729

2830
def current_user

0 commit comments

Comments
 (0)