Skip to content

Commit 9842608

Browse files
authored
Merge pull request #2 from ProjectVegas/activerecord_independence
Activerecord independence
2 parents 55f7a30 + d147266 commit 9842608

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

jsonapi_errorable.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Gem::Specification.new do |spec|
1818
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
1919
spec.require_paths = ["lib"]
2020

21-
spec.add_dependency 'rails', [">= 4.1", "< 6"]
2221
spec.add_dependency 'jsonapi-serializable', '~> 0.1'
2322

2423
spec.add_development_dependency "bundler", "~> 1.11"

lib/jsonapi_errorable/serializers/validation.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Serializers
33
class Validation
44
attr_reader :object
55

6-
def initialize(object, relationship_params, relationship_message = {})
6+
def initialize(object, relationship_params = {}, relationship_message = {})
77
@object = object
88
@relationship_params = relationship_params || {}
99
@relationship_message = relationship_message
@@ -33,7 +33,7 @@ def errors
3333
end
3434

3535
def relationship?(name)
36-
return false unless activerecord?
36+
return false unless activemodel?
3737

3838
relation_names = object.class.reflect_on_all_associations.map(&:name)
3939
relation_names.include?(name)
@@ -56,8 +56,8 @@ def pointer_for(object, name)
5656
end
5757
end
5858

59-
def activerecord?
60-
object.is_a?(ActiveRecord::Base)
59+
def activemodel?
60+
object.class.respond_to?(:reflect_on_all_associations)
6161
end
6262

6363
def relationship_errors(relationship_params)

spec/serializers/serializable_validation_spec.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let(:instance) { described_class.new(object) }
88

99
before do
10-
allow(instance).to receive(:activerecord?) { true }
10+
allow(instance).to receive(:activemodel?) { true }
1111
allow(object.class)
1212
.to receive(:reflect_on_all_associations)
1313
.and_return([double(name: :pets)])
@@ -18,10 +18,14 @@
1818
subject { instance.errors }
1919

2020
before do
21-
allow(object).to receive(:respond_to?).with(:username) { true }
21+
allow(object).to receive(:respond_to?).with(:errors) { true }
2222
end
2323

2424
context 'when the error is on an attribute' do
25+
before do
26+
allow(object).to receive(:respond_to?).with(:username) { true }
27+
end
28+
2529
it 'renders valid JSONAPI error format' do
2630
expect(subject).to eq(
2731
[
@@ -61,7 +65,7 @@
6165

6266
context 'but the object is not activerecord' do
6367
before do
64-
allow(instance).to receive(:activerecord?) { false }
68+
allow(instance).to receive(:activemodel?) { false }
6569
allow(object).to receive(:respond_to?).with(:pets) { true }
6670
end
6771

@@ -146,7 +150,7 @@
146150

147151
context 'when not activerecord' do
148152
before do
149-
allow(instance).to receive(:activerecord?) { false }
153+
allow(instance).to receive(:activemodel?) { false }
150154
end
151155

152156
it { is_expected.to be(false) }

0 commit comments

Comments
 (0)