Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dependent collection delete_all #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
10 changes: 8 additions & 2 deletions lib/active_mongoid/associations/document_relation/dependent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ def dependent_documents(metadata)
if metadata.dependent
dependent_method = :"dependent_documents_for_#{metadata.name}"
define_method(dependent_method) do
relation = get_document_relation(metadata.name, metadata, nil, true)
Array(relation).each { |d| d.send(metadata.dependent) }
if metadata.dependent.to_sym == :delete_all
type = attributes[metadata.inverse_type]
target = metadata.builder(self, send(metadata.key)).build(type)
target.delete_all
else
relation = get_document_relation(metadata.name, metadata, nil, true)
Array(relation).each { |d| d.send(metadata.dependent) }
end
end

before_destroy dependent_method
Expand Down
2 changes: 1 addition & 1 deletion lib/active_mongoid/associations/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def dependent
end

def destructive?
@destructive ||= (dependent == :delete || dependent == :destroy)
@destructive ||= (dependent == :delete || dependent == :destroy || dependent == :delete_all)
end

def polymorphic?
Expand Down