Skip to content

Commit

Permalink
Initialize relationships before checking for keys
Browse files Browse the repository at this point in the history
DataMapper.finalize now initializes all relationships before checking
for keys.  This allows a join model which consists entirely of
belongs_to relationships with `:key => true' to be considered valid.

[#1313 state:resolved]
  • Loading branch information
namelessjon committed Jun 10, 2010
1 parent a478891 commit 40ff12e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/dm-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,15 @@ def self.finalize_model(model)
raise IncompleteModelError, "#{name} must have at least one property or many to one relationship to be valid"
end

if model.key(repository_name).empty?
raise IncompleteModelError, "#{name} must have a key to be valid"
end

# initialize join models and target keys
relationships.each do |relationship|
relationship.child_key
relationship.through if relationship.respond_to?(:through)
relationship.via if relationship.respond_to?(:via)
end

if model.key(repository_name).empty?
raise IncompleteModelError, "#{name} must have a key to be valid"
end
end
end
19 changes: 19 additions & 0 deletions spec/public/finalize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ class ::ValidObject
end
end

it "should not raise on valid child model" do
class ::ValidChild
include DataMapper::Resource
belongs_to :valid_object, :key => true
end
class ::ValidObject
include DataMapper::Resource
property :id, Integer, :key => true
end
begin
method(:subject).should_not raise_error
ensure
DataMapper::Model.descendants.delete(ValidChild)
DataMapper::Model.descendants.delete(ValidObject)
Object.send(:remove_const, :ValidChild)
Object.send(:remove_const, :ValidObject)
end
end

it 'should raise on an anonymous model' do
model = Class.new do
include DataMapper::Resource
Expand Down

0 comments on commit 40ff12e

Please sign in to comment.