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

Commit

Permalink
Fixed property creation for repositories other than the default
Browse files Browse the repository at this point in the history
[#1506 state:resolved]
  • Loading branch information
solnic committed Apr 22, 2011
1 parent 808265d commit 5dd1bc9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
7 changes: 5 additions & 2 deletions lib/dm-core/model/property.rb
Expand Up @@ -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
Expand Down
58 changes: 49 additions & 9 deletions 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)
Expand All @@ -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
Expand Down

2 comments on commit 5dd1bc9

@unders
Copy link

@unders unders commented on 5dd1bc9 Jun 28, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a point release planned for this anytime soon? Would be excellent, since we're seeing this issue in an application.

@solnic
Copy link
Contributor Author

@solnic solnic commented on 5dd1bc9 Jun 28, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@unders yeah we'll be releasing a new version soon, maybe next week / cc @dkubb

Please sign in to comment.