Skip to content

Commit

Permalink
Fix uniqueness validation on localized nil fields
Browse files Browse the repository at this point in the history
- Fix #4052
  • Loading branch information
durran committed Jun 5, 2015
1 parent a3034e7 commit 948dd1a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ For instructions on upgrading to newer versions, visit

* \#4091 Use sublcass context when calling a scope defined in a superclass. (Edgars Beigarts)

* \#4052 Fixed uniqueness validation on localized fields with no value.

* \#4005 Fixed inclusion of mongoid with Rails components that don't have the Rails environment.

* \#3993 Fixes issue where `dup`/`clone` fails for embedded documents that use store_as without using Mongoid::Atributes::Dynamic
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/validatable/uniqueness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def create_criteria(base, document, attribute, value)
def criterion(document, attribute, value)
field = document.database_field_name(attribute)

if localized?(document, field)
conditions = value.inject([]) { |acc, (k,v)| acc << { "#{field}.#{k}" => filter(v) } }
if value && localized?(document, field)
conditions = (value || {}).inject([]) { |acc, (k,v)| acc << { "#{field}.#{k}" => filter(v) }}
selector = { "$or" => conditions }
else
selector = { field => filter(value) }
Expand Down
29 changes: 29 additions & 0 deletions spec/mongoid/validatable/uniqueness_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,35 @@
Dictionary.reset_callbacks(:validate)
end

context "when no attribute is set" do

context "when no document with no value exists in the database" do

let(:dictionary) do
Dictionary.new
end

it "returns true" do
expect(dictionary).to be_valid
end
end

context "when a document with no value exists in the database" do

before do
Dictionary.create
end

let(:dictionary) do
Dictionary.new
end

it "returns false" do
expect(dictionary).to_not be_valid
end
end
end

context "when the attribute is unique" do

context "when single localization" do
Expand Down

0 comments on commit 948dd1a

Please sign in to comment.