Skip to content
This repository was archived by the owner on Apr 17, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/data_mapper/validation/rule/uniqueness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ def valid?(resource)
opts[subject] = resource.__send__(subject)
}

property = resource.model.properties[attribute_name]
other_resource = DataMapper.repository(resource.repository.name) do
resource.model.first(opts)
property.model.first(opts)
end

return true if other_resource.nil?
Expand Down
13 changes: 13 additions & 0 deletions spec/fixtures/corporate_world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,24 @@ class Department
include DataMapper::Resource

property :id, Serial

property :type, Discriminator

property :name, String, :unique_index => true

validates_uniqueness_of :name
end

class SpecialDepartment < Department
property :special_id, String
validates_uniqueness_of :special_id
end

class VerySpecialDepartment < Department
property :special_id, String
validates_uniqueness_of :special_id
end

class User
include DataMapper::Resource

Expand Down
48 changes: 48 additions & 0 deletions spec/integration/uniqueness_validator/uniqueness_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,54 @@
end
end

describe 'DataMapper::Validations::Fixtures::SpecialDepartment' do
before :all do
DataMapper::Validations::Fixtures::Department.destroy!

DataMapper::Validations::Fixtures::Department.create(:name => "HR").should be_saved
end

describe "with a duplicate name" do
before do
@model = DataMapper::Validations::Fixtures::SpecialDepartment.new(:name => "HR")
end

it_should_behave_like "invalid model"
end
end

describe 'DataMapper::Validations::Fixtures::SpecialDepartment' do
before :all do
DataMapper::Validations::Fixtures::Department.destroy!

DataMapper::Validations::Fixtures::SpecialDepartment.create(:name => "HR", :special_id => "1234").should be_saved
end

describe "with a duplicate special_id" do
before do
@model = DataMapper::Validations::Fixtures::SpecialDepartment.new(:name => "Other Dept.", :special_id => "1234")
end

it_should_behave_like "invalid model"
end
end

describe 'DataMapper::Validations::Fixtures::VerySpecialDepartment' do
before :all do
DataMapper::Validations::Fixtures::Department.destroy!

DataMapper::Validations::Fixtures::SpecialDepartment.create(:name => "HR", :special_id => "1234").should be_saved
end

describe "with a special_id also used by a SpecialDepartment" do
before do
@model = DataMapper::Validations::Fixtures::VerySpecialDepartment.new(:name => "Some Very Special Other Dept.", :special_id => "1234")
end

it_should_behave_like "valid model"
end
end

describe 'DataMapper::Validations::Fixtures::Organisation' do
before :all do
DataMapper::Validations::Fixtures::Organisation.destroy!
Expand Down