Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update changelog, implementation
  • Loading branch information
durran committed Feb 5, 2013
1 parent 26bfd14 commit 63da4aa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,13 @@ For instructions on upgrading to newer versions, visit

### Resolved Issues

* \#2784 Fixed uniqueness validation properly getting added to subclasses.
(Takeshi Akima)

## 3.0.21

### Resolved Issues

* \#2781 / * \#2777 - Fixed issue with serialization of `DateTime` that was
only present in Rails environments.

Expand Down
19 changes: 16 additions & 3 deletions lib/mongoid/validations/uniqueness.rb
Expand Up @@ -16,6 +16,20 @@ module Validations
class UniquenessValidator < ActiveModel::EachValidator
include Queryable

attr_reader :klass

# Unfortunately, we have to tie Uniqueness validators to a class.
#
# @example Setup the validator.
# UniquenessValidator.new.setup(Person)
#
# @param [ Class ] klass The class getting validated.
#
# @since 1.0.0
def setup(klass)
@klass = klass
end

# Validate the document for uniqueness violations.
#
# @example Validate the document.
Expand Down Expand Up @@ -91,8 +105,7 @@ def case_sensitive?
# @since 2.4.10
def create_criteria(base, document, attribute, value)
field = document.fields[attribute.to_s]
klass = base.embedded? ? base : field.options[:klass] || base
criteria = scope(klass.unscoped, document, attribute)
criteria = scope(base.unscoped, document, attribute)
if field.try(:localized?)
criteria.selector.update(criterion(document, attribute, value))
else
Expand Down Expand Up @@ -253,7 +266,7 @@ def validate_embedded(document, attribute, value)
#
# @since 2.4.10
def validate_root(document, attribute, value)
criteria = create_criteria(document.class, document, attribute, value)
criteria = create_criteria(klass, document, attribute, value)
if criteria.with(consistency: :strong).exists?
add_error(document, attribute, value)
end
Expand Down
6 changes: 1 addition & 5 deletions spec/mongoid/validations/uniqueness_spec.rb
Expand Up @@ -2013,15 +2013,12 @@
context "when validation works with inheritance" do

before do
Actor.delete_all
Actor.validates_uniqueness_of :name
@johnny_depp = Actor.create!(name: "Johnny Depp")
Actor.create!(name: "Johnny Depp")
end

after do
# @johnny_depp.delete
Actor.reset_callbacks(:validate)
Actor.delete_all
end

let!(:subclass_document_with_duplicated_name) do
Expand All @@ -2034,6 +2031,5 @@
d.errors[:name].should eq([ "is already taken" ])
end
end

end
end

0 comments on commit 63da4aa

Please sign in to comment.