diff --git a/lib/dm-core/model/property.rb b/lib/dm-core/model/property.rb index 76044c7f..08ae41f7 100644 --- a/lib/dm-core/model/property.rb +++ b/lib/dm-core/model/property.rb @@ -64,13 +64,16 @@ def property(name, type, options = {}) # Add property to the other mappings as well if this is for the default # repository. + if repository_name == default_repository_name - DataMapper::Ext::Hash.except(@properties, default_repository_name).each do |other_repository_name, properties| + other_repository_properties = DataMapper::Ext::Hash.except(@properties, default_repository_name) + + other_repository_properties.each do |other_repository_name, properties| next if properties.named?(name) # make sure the property is created within the correct repository scope DataMapper.repository(other_repository_name) do - properties << klass.new(self, name, options, type) + properties << klass.new(self, name, options) end end end diff --git a/spec/public/model/property_spec.rb b/spec/public/model/property_spec.rb index ba35d8ac..26ccf7c5 100644 --- a/spec/public/model/property_spec.rb +++ b/spec/public/model/property_spec.rb @@ -1,5 +1,19 @@ require 'spec_helper' +shared_examples_for "a correct property declaration" do + it 'should define a name accessor' do + @model.should_not be_method_defined(@property_name) + subject + @model.should be_method_defined(@property_name) + end + + it 'should define a name= mutator' do + @model.should_not be_method_defined(:"#{@property_name}=") + subject + @model.should be_method_defined(:"#{@property_name}=") + end +end + describe DataMapper::Model::Property do before do Object.send(:remove_const, :ModelPropertySpecs) if defined?(ModelPropertySpecs) @@ -12,19 +26,45 @@ class ::ModelPropertySpecs end describe '#property' do + context "using default repository" do + before do + Object.send(:remove_const, :UserDefault) if defined?(::UserDefault) + + class ::UserDefault + include DataMapper::Resource + property :id, Serial + end - subject { ModelPropertySpecs.property(:name, String) } + @model = ::UserDefault + @property_name = :name + end - it 'should define a name accessor' do - ModelPropertySpecs.should_not be_method_defined(:name) - subject - ModelPropertySpecs.should be_method_defined(:name) + subject do + ::UserDefault.property(:name, String) + end + + it_should_behave_like "a correct property declaration" end - it 'should define a name= mutator' do - ModelPropertySpecs.should_not be_method_defined(:name=) - subject - ModelPropertySpecs.should be_method_defined(:name=) + context "using alternate repository" do + before do + Object.send(:remove_const, :UserAlternate) if defined?(::UserAlternate) + + class ::UserAlternate + include DataMapper::Resource + property :id, Serial + repository(:alternate) { property :age, Integer } + end + + @model = UserAlternate + @property_name = :alt_name + end + + subject do + ::UserAlternate.property(:alt_name, String) + end + + it_should_behave_like "a correct property declaration" end it 'should raise an exception if the method exists' do