Skip to content

Commit

Permalink
Provide #delete_if for embedds many.
Browse files Browse the repository at this point in the history
Fixes #2324.
  • Loading branch information
hanshasselberg committed Sep 3, 2012
1 parent fec64f8 commit 666cea8
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 68 deletions.
2 changes: 1 addition & 1 deletion lib/mongoid/relations/embedded/many.rb
Expand Up @@ -217,7 +217,7 @@ def find(*args)
#
# @return [ Many ] The proxy.
def initialize(base, target, metadata)
init(base, target, metadata) do
init(base, Targets::Enumerable.new(target), metadata) do
target.each_with_index do |doc, index|
integrate(doc)
doc._index = index
Expand Down
249 changes: 182 additions & 67 deletions spec/mongoid/relations/targets/enumerable_spec.rb
Expand Up @@ -401,107 +401,222 @@
Person.create
end

context "when the document is loaded" do
context "when document is embedded" do

let!(:post) do
Post.create(person_id: person.id)
end
context "when the document is loaded" do

let!(:enumerable) do
described_class.new([ post ])
end
let(:address) do
Address.new
end

let!(:deleted) do
enumerable.delete_if { |doc| doc == post }
end
let!(:enumerable) do
described_class.new([ address ])
end

it "deletes the document from the enumerable" do
enumerable._loaded.should be_empty
end
let!(:deleted) do
enumerable.delete_if { |doc| doc == address }
end

it "returns the remaining docs" do
deleted.should be_empty
it "deletes the document from the enumerable" do
enumerable._loaded.should be_empty
end

it "returns the remaining docs" do
deleted.should be_empty
end
end
end

context "when the document is added" do
context "when the document is added" do

let!(:post) do
Post.new
end
let!(:person) do
Person.create(addresses: [ address ])
end

let(:criteria) do
Person.where(person_id: person.id)
end
let(:address) do
Address.new
end

let!(:enumerable) do
described_class.new(criteria)
end
let(:criteria) do
person.addresses
end

before do
enumerable << post
end
let!(:enumerable) do
described_class.new(criteria)
end

let!(:deleted) do
enumerable.delete_if { |doc| doc == post }
end
before do
enumerable << address
end

it "removes the document from the added docs" do
enumerable._added.should be_empty
end
let!(:deleted) do
enumerable.delete_if { |doc| doc == address }
end

it "returns the remaining docs" do
deleted.should be_empty
it "removes the document from the added docs" do
enumerable._added.should be_empty
end

it "returns the remaining docs" do
deleted.should be_empty
end
end
end

context "when the document is unloaded" do
context "when the document is unloaded" do

let!(:post) do
Post.create(person_id: person.id)
end
let!(:person) do
Person.create(addresses: [ address ])
end

let(:criteria) do
Post.where(person_id: person.id)
end
let(:address) do
Address.new
end

let!(:enumerable) do
described_class.new(criteria)
end
let(:criteria) do
person.addresses
end

let!(:deleted) do
enumerable.delete_if { |doc| doc == post }
end
let!(:enumerable) do
described_class.new(criteria)
end

it "does not load the document" do
enumerable._loaded.should be_empty
let!(:deleted) do
enumerable.delete_if { |doc| doc == address }
end

it "does not load the document" do
enumerable._loaded.should be_empty
end

it "returns the remaining docs" do
deleted.should be_empty
end
end

it "returns the remaining docs" do
deleted.should be_empty
context "when the block doesn't match" do

let(:address) do
Address.new(street: 'Baker Street 221b')
end

let(:enumerable) do
described_class.new([ address ])
end

let(:deleted) do
p enumerable.inspect
enumerable.delete_if { |doc| doc == Address.new }
end

it "returns the remaining docs" do
deleted.should eq([ address ])
end
end
end

context "when the block doesn't match" do
context "when document is not embedded" do

let!(:post) do
Post.create(person_id: person.id)
end
context "when the document is loaded" do

let(:criteria) do
Person.where(person_id: person.id)
let!(:post) do
Post.create(person_id: person.id)
end

let!(:enumerable) do
described_class.new([ post ])
end

let!(:deleted) do
enumerable.delete_if { |doc| doc == post }
end

it "deletes the document from the enumerable" do
enumerable._loaded.should be_empty
end

it "returns the remaining docs" do
deleted.should be_empty
end
end

let!(:enumerable) do
described_class.new([ post ])
context "when the document is added" do

let!(:post) do
Post.new
end

let(:criteria) do
Person.where(person_id: person.id)
end

let!(:enumerable) do
described_class.new(criteria)
end

before do
enumerable << post
end

let!(:deleted) do
enumerable.delete_if { |doc| doc == post }
end

it "removes the document from the added docs" do
enumerable._added.should be_empty
end

it "returns the remaining docs" do
deleted.should be_empty
end
end

let!(:deleted) do
enumerable.delete_if { |doc| doc == Post.new }
context "when the document is unloaded" do

let!(:post) do
Post.create(person_id: person.id)
end

let(:criteria) do
Post.where(person_id: person.id)
end

let!(:enumerable) do
described_class.new(criteria)
end

let!(:deleted) do
enumerable.delete_if { |doc| doc == post }
end

it "does not load the document" do
enumerable._loaded.should be_empty
end

it "returns the remaining docs" do
deleted.should be_empty
end
end

it "returns the remaining docs" do
deleted.should eq([ post ])
context "when the block doesn't match" do

let!(:post) do
Post.create(person_id: person.id)
end

let(:criteria) do
Person.where(person_id: person.id)
end

let!(:enumerable) do
described_class.new([ post ])
end

let!(:deleted) do
enumerable.delete_if { |doc| doc == Post.new }
end

it "returns the remaining docs" do
deleted.should eq([ post ])
end
end
end
end
Expand Down

0 comments on commit 666cea8

Please sign in to comment.