forked from datamapper/dm-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
61 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,11 @@ def load(value) | |
value | ||
end | ||
end | ||
|
||
# @api private | ||
def to_child_key | ||
self.class | ||
end | ||
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
49 changes: 49 additions & 0 deletions
49
spec/public/associations/many_to_one_with_custom_fk_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,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 |
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