Skip to content

Commit

Permalink
Support for using aliased field names in combination with validates_u…
Browse files Browse the repository at this point in the history
…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
johnnyshields authored and durran committed May 6, 2013
1 parent 9ffdf89 commit 00251f4
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ doc/*
*.profile
*.profile.pdf
*.symbols
.idea
2 changes: 2 additions & 0 deletions lib/mongoid/validatable/uniqueness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion spec/app/models/definition.rb
Original file line number Diff line number Diff line change
@@ -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
98 changes: 98 additions & 0 deletions spec/mongoid/validatable/uniqueness_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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
Expand Down Expand Up @@ -2012,6 +2061,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
Expand Down

0 comments on commit 00251f4

Please sign in to comment.