Skip to content

Commit a4bd278

Browse files
authored
Merge pull request #6 from richmolj/jsonapirb
Jsonapirb
2 parents 7faee00 + 30ada00 commit a4bd278

19 files changed

+170
-84
lines changed

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ 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'
65

76
group :test do
87
gem 'appraisal'

jsonapi_compliable.gemspec

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ Gem::Specification.new do |spec|
1818
spec.require_paths = ["lib"]
1919

2020
spec.add_dependency "rails", ['>= 4.1', '< 6']
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"
21+
22+
spec.add_dependency 'jsonapi-rails', '~> 0.1'
2423

2524
spec.add_development_dependency "kaminari"
2625
spec.add_development_dependency "active_model_serializers"

lib/jsonapi_compliable.rb

Lines changed: 3 additions & 5 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,8 +17,8 @@
1917
require "jsonapi_compliable/util/field_params"
2018
require "jsonapi_compliable/util/scoping"
2119
require "jsonapi_compliable/util/pagination"
22-
23-
require 'jsonapi_compliable/railtie' if defined?(::Rails)
20+
require "jsonapi_compliable/extensions/extra_attribute"
21+
require "jsonapi_compliable/extensions/boolean_attribute"
2422

2523
module JsonapiCompliable
2624
autoload :Base, 'jsonapi_compliable/base'

lib/jsonapi_compliable/base.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,26 @@ def parse_fieldsets!
5151
Util::FieldParams.parse!(params, :extra_fields)
5252
end
5353

54-
def render_ams(scope, opts = {})
54+
def render_jsonapi(scope, opts = {})
5555
scoped = Util::Scoping.apply?(self, scope, opts.delete(:scope)) ? jsonapi_scope(scope) : scope
56-
options = default_ams_options
56+
options = default_jsonapi_render_options
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
6769

68-
# render_ams(foo) equivalent to
69-
# render json: foo, ams_default_options
70-
def default_ams_options
70+
# render_jsonapi(foo) equivalent to
71+
# render jsonapi: foo, default_jsonapi_render_options
72+
def default_jsonapi_render_options
7173
{}.tap do |options|
72-
options[:adapter] = :json_api
7374
end
7475
end
7576

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module JsonapiCompliable
2+
module Extensions
3+
module BooleanAttribute
4+
def self.included(klass)
5+
klass.extend ClassMethods
6+
end
7+
8+
module ClassMethods
9+
def boolean_attribute(name, options = {}, &blk)
10+
blk ||= proc { @object.public_send(name) }
11+
field_name = :"is_#{name.to_s.gsub('?', '')}"
12+
attribute field_name, options, &blk
13+
end
14+
end
15+
end
16+
end
17+
end
18+
19+
JSONAPI::Serializable::Resource.class_eval do
20+
include JsonapiCompliable::Extensions::BooleanAttribute
21+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'jsonapi/serializable/resource/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::Resource::ConditionalFields
33+
include JsonapiCompliable::Extensions::ExtraAttribute
34+
end

lib/jsonapi_compliable/railtie.rb

Lines changed: 0 additions & 18 deletions
This file was deleted.

lib/jsonapi_compliable/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module JsonapiCompliable
2-
VERSION = "0.3.10"
2+
VERSION = "0.4.0"
33
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/create_update_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def create
1010
author = Author.new(author_params.except(:state_attributes))
1111
author.state = State.find_or_initialize_by(author_params[:state_attributes])
1212
author.save!(validate: false)
13-
render_ams(author, scope: false)
13+
render_jsonapi(author, scope: false)
1414
end
1515

1616
def update
@@ -21,7 +21,7 @@ def update
2121
end
2222
author.association(:books).loaded!
2323
author.save!
24-
render_ams(author, scope: false)
24+
render_jsonapi(author, scope: false)
2525
end
2626

2727
private

spec/extra_fields_spec.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
require 'spec_helper'
22

33
RSpec.describe 'extra_fields', type: :controller do
4-
class TestExtraFieldsSerializer < ActiveModel::Serializer
5-
include JsonapiAmsExtensions
4+
class SerializableTestExtraFields < JSONAPI::Serializable::Resource
5+
type 'authors'
66
attributes :first_name, :last_name
7-
extra_attribute :net_worth
8-
9-
def net_worth
7+
extra_attribute :net_worth, if: proc { @context.allow_net_worth? } do
108
100_000_000
119
end
1210
end
@@ -18,8 +16,12 @@ def net_worth
1816
end
1917
end
2018

19+
def allow_net_worth?
20+
true
21+
end
22+
2123
def index
22-
render_ams(Author.all, each_serializer: TestExtraFieldsSerializer)
24+
render_jsonapi(Author.all, class: SerializableTestExtraFields)
2325
end
2426
end
2527

@@ -57,8 +59,7 @@ def include_foo!
5759

5860
context 'when extra field is requested but guarded' do
5961
before do
60-
allow_any_instance_of(TestExtraFieldsSerializer)
61-
.to receive(:allow_net_worth?) { false }
62+
allow(controller).to receive(:allow_net_worth?) { false }
6263
end
6364

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

spec/fields_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
controller(ApplicationController) do
55
jsonapi {}
66

7-
class TestFieldsSerializer < ActiveModel::Serializer
8-
attributes :first_name, :last_name, :uuid
9-
attribute :salary, if: :admin?
7+
class SerializableTestFields < JSONAPI::Serializable::Resource
8+
type 'authors'
109

11-
def uuid
10+
attribute :first_name
11+
attribute :last_name
12+
attribute :uuid do
1213
SecureRandom.uuid
1314
end
15+
attribute :salary, if: proc { @context.current_user == 'admin' } do
16+
50_000
17+
end
1418

1519
def admin?
1620
scope == 'admin'
1721
end
18-
19-
def salary
20-
50_000
21-
end
2222
end
2323

2424
def index
25-
render_ams(Author.all, each_serializer: TestFieldsSerializer)
25+
render_jsonapi(Author.all, class: SerializableTestFields)
2626
end
2727

2828
def current_user

spec/filtering_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
end
1313

1414
def index
15-
render_ams(Author.all)
15+
render_jsonapi(Author.all)
1616
end
1717

1818
def can_filter_first_name?

spec/jsonapi_compliable_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def index
88
scope = Author.all
9-
render_ams(scope)
9+
render_jsonapi(scope)
1010
end
1111
end
1212

@@ -46,15 +46,15 @@ def index
4646
end
4747
end
4848

49-
describe '#render_ams' do
49+
describe '#render_jsonapi' do
5050
it 'is able to override options' do
5151
author = Author.create!(first_name: 'Stephen', last_name: 'King')
5252
author.books.create(title: "The Shining", genre: Genre.new(name: 'horror'))
5353

5454
controller.class_eval do
5555
def index
5656
scope = Author.all
57-
render_ams(scope, include: { books: :genre })
57+
render_jsonapi(scope, include: { books: :genre })
5858
end
5959
end
6060

@@ -67,7 +67,7 @@ def index
6767
controller.class_eval do
6868
def index
6969
people = jsonapi_scope(Author.all).to_a
70-
render_ams(people)
70+
render_jsonapi(people)
7171
end
7272
end
7373
end
@@ -97,7 +97,7 @@ def index
9797
controller.class_eval do
9898
def index
9999
people = Author.all
100-
render_ams(people, scope: false)
100+
render_jsonapi(people, scope: false)
101101
end
102102
end
103103
end

spec/pagination_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
jsonapi {}
66

77
def index
8-
render_ams(Author.all)
8+
render_jsonapi(Author.all)
99
end
1010
end
1111

spec/sideloading_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
end
88

99
def index
10-
render_ams(Author.all)
10+
render_jsonapi(Author.all)
1111
end
1212
end
1313

@@ -48,11 +48,11 @@ def index
4848
end
4949

5050
it 'uses the custom include function' do
51-
scope = double.as_null_object
51+
scope = Author.all
52+
allow(Author).to receive(:all) { scope }
53+
5254
expect(scope).to receive(:special_include)
53-
.and_return(scope)
54-
.with(books: { genre: {} })
55-
expect(Author).to receive(:all) { scope }
55+
.with(books: { genre: {} }).and_return(scope)
5656

5757
get :index, params: { include: 'books.genre' }
5858
end

spec/sorting_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
jsonapi {}
66

77
def index
8-
render_ams(Author.all)
8+
render_jsonapi(Author.all)
99
end
1010
end
1111

0 commit comments

Comments
 (0)