Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Uniqueness validation scoping now workds with aliased fields. [ fix #…

…2375 ]
  • Loading branch information...
commit 77b1ab51e2757d00d1f27b032df5d06df6a07303 1 parent a824266
@durran durran authored
View
2  CHANGELOG.md
@@ -7,6 +7,8 @@ For instructions on upgrading to newer versions, visit
### Resolved Issues
+* \#2375 Uniqueness validation scoping now works with aliased fields.
+
* \#2372 Ensure that all atomic operations mongoize values before executing.
* \#2370 Paranoid documents now properly don't get deleted when using
View
4 lib/mongoid/validations/uniqueness.rb
@@ -167,7 +167,9 @@ def filter(value)
# @since 2.3.0
def scope(criteria, document, attribute)
Array.wrap(options[:scope]).each do |item|
- criteria = criteria.where(item => document.attributes[item.to_s])
+ normalized = item.to_s
+ name = document.aliased_fields[normalized] || normalized
+ criteria = criteria.where(item => document.attributes[name])
end
criteria = criteria.where(deleted_at: nil) if document.paranoid?
criteria
View
1  spec/app/models/dictionary.rb
@@ -5,5 +5,6 @@ class Dictionary
field :year, type: Integer
field :published, type: Time
field :description, type: String, localize: true
+ field :l, type: String, as: :language
has_many :words, validate: false
end
View
106 spec/mongoid/validations/uniqueness_spec.rb
@@ -304,6 +304,111 @@
end
end
+ context "when an aliased scope is provided" do
+
+ before do
+ Dictionary.validates_uniqueness_of :name, scope: :language
+ end
+
+ after do
+ Dictionary.reset_callbacks(:validate)
+ end
+
+ context "when the attribute is unique" do
+
+ before do
+ Dictionary.create(name: "Oxford", language: "English")
+ end
+
+ let(:dictionary) do
+ Dictionary.new(name: "Webster")
+ end
+
+ it "returns true" do
+ dictionary.should be_valid
+ end
+ end
+
+ context "when the attribute is unique in the scope" do
+
+ before do
+ Dictionary.create(name: "Oxford", language: "English")
+ end
+
+ let(:dictionary) do
+ Dictionary.new(name: "Webster", language: "English")
+ end
+
+ it "returns true" do
+ dictionary.should be_valid
+ end
+ end
+
+ context "when the attribute is not unique with no scope" do
+
+ before do
+ Dictionary.create(name: "Oxford", language: "English")
+ end
+
+ let(:dictionary) do
+ Dictionary.new(name: "Oxford")
+ end
+
+ it "returns true" do
+ dictionary.should be_valid
+ end
+ end
+
+ context "when the attribute is not unique in another scope" do
+
+ before do
+ Dictionary.create(name: "Oxford", language: "English")
+ end
+
+ let(:dictionary) do
+ Dictionary.new(name: "Oxford", language: "Deutsch")
+ end
+
+ it "returns true" do
+ dictionary.should be_valid
+ end
+ end
+
+ context "when the attribute is not unique in the same scope" do
+
+ context "when the document is not the match" do
+
+ before do
+ Dictionary.create(name: "Oxford", language: "English")
+ end
+
+ let(:dictionary) do
+ Dictionary.new(name: "Oxford", language: "English")
+ end
+
+ it "returns false" do
+ dictionary.should_not be_valid
+ end
+
+ it "adds the uniqueness errors" do
+ dictionary.valid?
+ dictionary.errors[:name].should eq([ "is already taken" ])
+ end
+ end
+
+ context "when the document is the match in the database" do
+
+ let!(:dictionary) do
+ Dictionary.create(name: "Oxford", language: "English")
+ end
+
+ it "returns true" do
+ dictionary.should be_valid
+ end
+ end
+ end
+ end
+
context "when a single scope is provided" do
before do
@@ -367,7 +472,6 @@
item.update_attributes(folder_id: personal_folder.id)
item.errors.messages[:name].first.should eq("is already taken")
end
-
end
context "when the attribute is not unique with no scope" do
Please sign in to comment.
Something went wrong with that request. Please try again.