Skip to content

Commit

Permalink
Scope uniqueness validation for paranoid documents. [fix #2144]
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Jun 27, 2012
1 parent f2394df commit f93ae54
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -899,6 +899,8 @@ For instructions on upgrading to newer versions, visit

### Resolved Issues

* \#2144 Uniqueness validation on paranoid documents now properly scopes.

* \#2127 Don't unbind parents of embedded documents mid nested
attributes deletion.

Expand Down
1 change: 1 addition & 0 deletions lib/mongoid/validations/uniqueness.rb
Expand Up @@ -165,6 +165,7 @@ def scope(criteria, document, attribute)
Array.wrap(options[:scope]).each do |item|
criteria = criteria.where(item => document.attributes[item.to_s])
end
criteria = criteria.where(deleted_at: nil) if document.paranoid?
criteria
end

Expand Down
52 changes: 52 additions & 0 deletions spec/mongoid/validations/uniqueness_spec.rb
Expand Up @@ -6,6 +6,58 @@

context "when the document is a root document" do

context "when the document is paranoid" do

before do
ParanoidPost.validates_uniqueness_of :title
end

after do
ParanoidPost.reset_callbacks(:validate)
end

let!(:post) do
ParanoidPost.create(title: "testing")
end

context "when the field is unique" do

let(:new_post) do
ParanoidPost.new(title: "test")
end

it "returns true" do
new_post.should be_valid
end
end

context "when the field is unique for non soft deleted docs" do

before do
post.delete
end

let(:new_post) do
ParanoidPost.new(title: "testing")
end

it "returns true" do
new_post.should be_valid
end
end

context "when the field is not unique" do

let(:new_post) do
ParanoidPost.new(title: "testing")
end

it "returns false" do
new_post.should_not be_valid
end
end
end

context "when the document contains no compound key" do

context "when validating a relation" do
Expand Down

0 comments on commit f93ae54

Please sign in to comment.