Skip to content
This repository was archived by the owner on Apr 17, 2018. It is now read-only.
Closed
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/dm-validations/validators/uniqueness_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ def valid?(target)
opts[subject] = target.__send__(subject)
}

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

return true if resource.nil?
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/corporate_world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Department

property :id, Serial
property :name, String, :unique_index => true
property :type, Discriminator

validates_uniqueness_of :name
end
Expand All @@ -34,6 +35,9 @@ class User
validates_uniqueness_of :user_name, :when => :signing_up_for_department_account, :scope => [:department]
validates_uniqueness_of :user_name, :when => :signing_up_for_organization_account, :scope => [:organisation]
end

# For STI uniqueness testing
class BigDepartment < Department; end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@

it_should_behave_like "invalid model"
end

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

it_should_behave_like "invalid model"
end
end

describe 'DataMapper::Validations::Fixtures::Organisation' do
Expand Down