forked from ionia-corporation/active_force
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ionia-corporation#83 from DanOlson/fix-nil-belongs-to
Nil return value on belongs_to association should be handled gracefully
- Loading branch information
Showing
8 changed files
with
200 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
module ActiveForce | ||
module Association | ||
class RelationModelBuilder | ||
class << self | ||
def build(association, value) | ||
new(association, value).build_relation_model | ||
end | ||
end | ||
|
||
def initialize(association, value) | ||
@association = association | ||
@value = value | ||
end | ||
|
||
def build_relation_model | ||
klass = resolve_class | ||
klass.new(@association, @value).call | ||
end | ||
|
||
private | ||
|
||
def resolve_class | ||
association_builder = @value.class.name.gsub('::', '_') | ||
ActiveForce::Association.const_get "BuildFrom#{association_builder}" | ||
rescue NameError | ||
raise "Don't know how to build relation from #{@value.class.name}" | ||
end | ||
end | ||
|
||
class AbstractBuildFrom | ||
attr_reader :association, :value | ||
|
||
def initialize(association, value) | ||
@association = association | ||
@value = value | ||
end | ||
|
||
def call | ||
raise "Must implement #{self.class.name}#call" | ||
end | ||
end | ||
|
||
class BuildFromHash < AbstractBuildFrom | ||
def call | ||
association.build value | ||
end | ||
end | ||
|
||
class BuildFromArray < AbstractBuildFrom | ||
def call | ||
value.map { |mash| association.build mash } | ||
end | ||
end | ||
|
||
class BuildFromNilClass < AbstractBuildFrom | ||
def call | ||
association.is_a?(BelongsToAssociation) ? nil : [] | ||
end | ||
end | ||
|
||
class BuildFromRestforce_SObject < BuildFromHash | ||
end | ||
|
||
class BuildFromRestforce_Mash < BuildFromHash | ||
end | ||
|
||
class BuildFromRestforce_Collection < BuildFromArray | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
spec/active_force/association/relation_model_builder_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'spec_helper' | ||
|
||
module ActiveForce | ||
module Association | ||
describe RelationModelBuilder do | ||
let(:instance){ described_class.new association, value } | ||
|
||
describe '#build_relation_model' do | ||
context 'has_many' do | ||
let(:association){ HasManyAssociation.new Post, :comments } | ||
|
||
context 'with values' do | ||
let(:value) do | ||
build_restforce_collection([ | ||
Restforce::SObject.new({'Id' => '213', 'PostId' => '123'}), | ||
Restforce::SObject.new({'Id' => '214', 'PostId' => '123'}) | ||
]) | ||
end | ||
|
||
it 'returns an array of Comments' do | ||
comments = instance.build_relation_model | ||
expect(comments).to be_a Array | ||
expect(comments.all?{ |c| c.is_a? Comment }).to be true | ||
end | ||
end | ||
|
||
context 'without values' do | ||
let(:value){ nil } | ||
|
||
it 'returns an empty array' do | ||
comments = instance.build_relation_model | ||
expect(comments).to be_a Array | ||
expect(comments).to be_empty | ||
end | ||
end | ||
end | ||
|
||
context 'belongs_to' do | ||
let(:association){ BelongsToAssociation.new(Comment, :post) } | ||
|
||
context 'with a value' do | ||
let(:value) do | ||
build_restforce_sobject 'Id' => '213' | ||
end | ||
|
||
it 'returns a post' do | ||
expect(instance.build_relation_model).to be_a Post | ||
end | ||
end | ||
|
||
context 'without a value' do | ||
let(:value){ nil } | ||
|
||
it 'returns nil' do | ||
expect(instance.build_relation_model).to be_nil | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.