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

Commit

Permalink
Added Property#to_child_key.
Browse files Browse the repository at this point in the history
The method by default returns the property class but there might be
some exceptions like Serial, which returns Integer. This is required
so that custom properties that are used as PKs can work correctly with
ManyToOne assocations - UUID from dm-types is a good example.

[#1348 status:resolved]
  • Loading branch information
solnic committed Aug 27, 2010
1 parent cd3340c commit 6315411
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lib/dm-core/associations/many_to_one.rb
Expand Up @@ -55,8 +55,7 @@ def child_key
properties[property_name] || begin
# create the property within the correct repository
DataMapper.repository(repository_name) do
type = parent_property.send(parent_property.type == DataMapper::Property::Boolean ? :type : :primitive)
model.property(property_name, type, child_key_options(parent_property))
model.property(property_name, parent_property.to_child_key, child_key_options(parent_property))
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/dm-core/property/object.rb
Expand Up @@ -27,6 +27,11 @@ def load(value)
value
end
end

# @api private
def to_child_key
self.class
end
end
end
end
5 changes: 5 additions & 0 deletions lib/dm-core/property/serial.rb
Expand Up @@ -3,6 +3,11 @@ class Property
class Serial < Integer
serial true
min 1

# @api private
def to_child_key
Property::Integer
end
end # class Text
end # module Property
end # module DataMapper
49 changes: 49 additions & 0 deletions spec/public/associations/many_to_one_with_custom_fk_spec.rb
@@ -0,0 +1,49 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))

# TODO: combine this into many_to_one_spec.rb

describe 'Many to One Associations when foreign key is a custom property' do
before :all do
class ::CustomPK < DataMapper::Property::String
key true
end

class ::Animal
include DataMapper::Resource

property :id, Serial
property :name, String

belongs_to :zoo
end

class ::Zoo
include DataMapper::Resource

property :id, ::CustomPK

has n, :animals
end

DataMapper.finalize
end

supported_by :all do
before :all do
@zoo = Zoo.create(:id => 'foo')
@animal = @zoo.animals.create(:name => 'marty')
end

it 'should have FK of the same property type as zoo PK' do
Animal.properties[:zoo_id].class.should be(Zoo.properties[:id].class)
end

it 'should be able to access parent' do
@animal.zoo.should == @zoo
end

it 'should be able to access the children' do
@zoo.animals.should == [ @animal ]
end
end
end
2 changes: 1 addition & 1 deletion spec/semipublic/resource/state/dirty_spec.rb
Expand Up @@ -51,7 +51,7 @@ class ::Author
it 'should set the child key if the parent key changes' do
original_id = @parent.id
@parent.update(:id => 42).should be(true)
method(:subject).should change(@resource, :parent_id).from(original_id.to_s).to('42')
method(:subject).should change(@resource, :parent_id).from(original_id).to(42)
end

it 'should update the resource' do
Expand Down

0 comments on commit 6315411

Please sign in to comment.