Skip to content

Commit

Permalink
Uniqueness validation scoping now workds with aliased fields. [ fix #…
Browse files Browse the repository at this point in the history
…2375 ]
  • Loading branch information
durran committed Sep 15, 2012
1 parent a824266 commit 77b1ab5
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/mongoid/validations/uniqueness.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/app/models/dictionary.rb
Expand Up @@ -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
106 changes: 105 additions & 1 deletion spec/mongoid/validations/uniqueness_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 77b1ab5

Please sign in to comment.