Permalink
Browse files

Handle nil-data relationships (closes #14)

  • Loading branch information...
1 parent f18374a commit 781803275f222d4b01d2d3e4b09300ea346fb155 @remiprev committed May 29, 2012
Showing with 7 additions and 1 deletion.
  1. +1 −1 lib/her/model/relationships.rb
  2. +6 −0 spec/model/relationships_spec.rb
@@ -16,7 +16,7 @@ def parse_relationships(data) # {{{
relationships.each do |relationship|
name = relationship[:name]
class_name = relationship[:class_name]
- next unless data.include?(name)
+ next if !data.include?(name) or data[name].nil?
data[name] = case type
when :has_many
Her::Model::ORM.initialize_collection(class_name, data[name])
@@ -137,6 +137,7 @@
builder.adapter :test do |stub|
stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke", :organization => { :id => 1, :name => "Bluth Company" }, :organization_id => 1 }.to_json] }
stub.get("/users/2") { |env| [200, {}, { :id => 2, :name => "Lindsay Fünke", :organization_id => 1 }.to_json] }
+ stub.get("/users/3") { |env| [200, {}, { :id => 2, :name => "Lindsay Fünke", :organization => nil }.to_json] }
stub.get("/organizations/1") { |env| [200, {}, { :id => 1, :name => "Bluth Company" }.to_json] }
end
end
@@ -152,6 +153,7 @@
@user_with_included_data = User.find(1)
@user_without_included_data = User.find(2)
+ @user_with_included_nil_data = User.find(3)
end # }}}
it "maps an array of included data through belongs_to" do # {{{
@@ -160,6 +162,10 @@
@user_with_included_data.organization.name.should == "Bluth Company"
end # }}}
+ it "does not map included data if it’s nil" do # {{{
+ @user_with_included_nil_data.organization.should be_nil
+ end # }}}
+
it "fetches data that was not included through belongs_to" do # {{{
@user_without_included_data.organization.class.should == Business
@user_without_included_data.organization.id.should == 1

0 comments on commit 7818032

Please sign in to comment.