Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed has_one association when it's nil. #44

Merged
merged 1 commit into from
Jun 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/flexirest/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def has_one(key, klass = nil)
@_associations ||= {}
@_associations[key] = klass
define_method(key) do
return nil if _attributes[key].nil?

if _attributes[key].is_a?(klass)
return _attributes[key]
end
Expand Down
5 changes: 5 additions & 0 deletions spec/lib/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class DeepNestedHasManyExample < Flexirest::Base
describe "Has One Associations" do
let(:subject) {AssociationExampleBase.new}

it "should return nil if it's nil" do
subject.child = nil
expect(subject.child).to be_nil
end

it "should return a list of the association class" do
subject.child = {test: "foo"}
expect(subject.child).to be_an(AssociationExampleOther)
Expand Down