Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Fix 1:1 with CPK containing a boolean property
Browse files Browse the repository at this point in the history
[#994 state:resolved]

Signed-off-by: Dan Kubb <dan.kubb@gmail.com>
  • Loading branch information
angelic authored and dkubb committed Oct 16, 2009
1 parent abe512d commit f6b6852
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions Manifest.txt
Expand Up @@ -79,6 +79,7 @@ spec/public/associations/many_to_one_spec.rb
spec/public/associations/many_to_one_with_boolean_cpk_spec.rb
spec/public/associations/one_to_many_spec.rb
spec/public/associations/one_to_one_spec.rb
spec/public/associations/one_to_one_with_boolean_cpk_spec.rb
spec/public/collection_spec.rb
spec/public/migrations_spec.rb
spec/public/model/relationship_spec.rb
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-core/associations/many_to_one.rb
Expand Up @@ -145,7 +145,7 @@ def initialize(name, source_model, target_model, options = {})
#
# @api private
def lazy_load(source)
return unless source_key.get(source).all?
return unless source_key.get(source).all? { |value| !value.nil? }

# SEL: load all related resources in the source collection
if source.saved? && source.collection.size > 1
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-core/associations/one_to_one.rb
Expand Up @@ -15,7 +15,7 @@ class Relationship < Associations::Relationship
def get(source, other_query = nil)
assert_kind_of 'source', source, source_model

return unless loaded?(source) || source_key.get(source).all?
return unless loaded?(source) || source_key.get(source).all? { |value| !value.nil? }

relationship.get(source, other_query).first
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-core/resource.rb
Expand Up @@ -90,7 +90,7 @@ def key
original_attributes[property] || (property.loaded?(self) ? property.get!(self) : nil)
end

return unless key.all?
return unless key.all? { |value| !value.nil? }

# memoize the key if the Resource is not frozen
@key = key unless frozen?
Expand Down
45 changes: 45 additions & 0 deletions spec/public/associations/one_to_one_with_boolean_cpk_spec.rb
@@ -0,0 +1,45 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))

# TODO: combine this into one_to_one_spec.rb

describe 'One to One Associations when foreign key is part of a composite key and contains a boolean, with an integer and a boolean making up the composite key' do
before :all do
class ::ParentModel
include DataMapper::Resource

property :integer_key, Integer, :key => true
property :boolean_key, Boolean, :key => true

has 1, :child_model, :child_key => [ :integer_key, :boolean_key ]
end

class ::ChildModel
include DataMapper::Resource

property :integer_key, Integer, :key => true
property :other_integer_key, Integer, :key => true
property :boolean_key, Boolean, :key => true

belongs_to :parent_model, :child_key => [ :integer_key, :boolean_key ]
end
end

supported_by :all do
before :all do
@parent = ParentModel.create(:integer_key => 1, :boolean_key => false)
@child = ChildModel.create(:integer_key => 1, :other_integer_key => 1, :boolean_key => false)
end

it 'should be able to access the child' do
@parent.child_model.should == @child
end

it 'should be able to access the parent' do
@child.parent_model.should == @parent
end

it 'should be able to access the parent_key' do
@child.parent_model.key.should_not be_nil
end
end
end

0 comments on commit f6b6852

Please sign in to comment.