Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support for using aliased field names in combination with validates_u…

…niqueness. (Non-aliased names continue to function as before.)

Specs cover:
- aliased field names in combination with validates_uniqueness
- the presence of aliased field key in the errors hash
- aliased field in embedded documents
  • Loading branch information...
commit e4838df7b448c1763124ab34f3a0402ab6dd9c1c 1 parent 4fa9762
@johnnyshields johnnyshields authored durran committed
View
1  .gitignore
@@ -20,3 +20,4 @@ doc/*
*.profile
*.profile.pdf
*.symbols
+.idea
View
2  lib/mongoid/validations/uniqueness.rb
@@ -124,6 +124,8 @@ def create_criteria(base, document, attribute, value)
#
# @since 2.3.0
def criterion(document, attribute, value)
+ attribute = document.class.aliased_fields[attribute.to_s] || attribute
+
if localized?(document, attribute)
conditions = value.inject([]) { |acc, (k,v)| acc << { "#{attribute}.#{k}" => filter(v) } }
selector = { "$or" => conditions }
View
2  spec/app/models/definition.rb
@@ -1,7 +1,7 @@
class Definition
include Mongoid::Document
field :description, type: String
- field :part, type: String
+ field :p, as: :part, type: String
field :regular, type: Boolean
embedded_in :word
end
View
98 spec/mongoid/validations/uniqueness_spec.rb
@@ -141,6 +141,55 @@
end
end
+ context "when the field name is aliased" do
+
+ before do
+ Dictionary.create!(language: "en")
+ end
+
+ let(:dictionary) do
+ Dictionary.new(language: "en")
+ end
+
+ after do
+ Dictionary.reset_callbacks(:validate)
+ end
+
+ context "when the validation uses the aliased name" do
+
+ before do
+ Dictionary.validates_uniqueness_of :language
+ end
+
+ it "correctly detects a uniqueness conflict" do
+ expect(dictionary).to_not be_valid
+ end
+
+ it "adds the uniqueness error to the aliased field name" do
+ dictionary.valid?
+ expect(dictionary.errors).to have_key(:language)
+ expect(dictionary.errors[:language]).to eq([ "is already taken" ])
+ end
+ end
+
+ context "when the validation uses the underlying field name" do
+
+ before do
+ Dictionary.validates_uniqueness_of :l
+ end
+
+ it "correctly detects a uniqueness conflict" do
+ expect(dictionary).to_not be_valid
+ end
+
+ it "adds the uniqueness error to the underlying field name" do
+ dictionary.valid?
+ expect(dictionary.errors).to have_key(:l)
+ expect(dictionary.errors[:l]).to eq([ "is already taken" ])
+ end
+ end
+ end
+
context "when the field is localized" do
context "when no scope is provided" do
@@ -2064,6 +2113,55 @@
end
end
end
+
+ context "when the field name is aliased" do
+
+ before do
+ word.definitions.build(part: "noun")
+ end
+
+ let(:definition) do
+ word.definitions.build(part: "noun")
+ end
+
+ after do
+ Definition.reset_callbacks(:validate)
+ end
+
+ context "when the validation uses the aliased name" do
+
+ before do
+ Definition.validates_uniqueness_of :part, case_sensitive: false
+ end
+
+ it "correctly detects a uniqueness conflict" do
+ expect(definition).to_not be_valid
+ end
+
+ it "adds the uniqueness error to the aliased field name" do
+ definition.valid?
+ expect(definition.errors).to have_key(:part)
+ expect(definition.errors[:part]).to eq([ "is already taken" ])
+ end
+ end
+
+ context "when the validation uses the underlying field name" do
+
+ before do
+ Definition.validates_uniqueness_of :p, case_sensitive: false
+ end
+
+ it "correctly detects a uniqueness conflict" do
+ expect(definition).to_not be_valid
+ end
+
+ it "adds the uniqueness error to the underlying field name" do
+ definition.valid?
+ expect(definition.errors).to have_key(:p)
+ expect(definition.errors[:p]).to eq([ "is already taken" ])
+ end
+ end
+ end
end
context "when the document uses composite keys" do
Please sign in to comment.
Something went wrong with that request. Please try again.