From dce9eb7ecb444e23ddab7c37df85624f361d2e58 Mon Sep 17 00:00:00 2001 From: Vital Ryabchinskiy Date: Wed, 28 Aug 2019 07:19:10 +0000 Subject: [PATCH] Support dependent collection delete_all --- .DS_Store | Bin 6148 -> 0 bytes .../associations/document_relation/dependent.rb | 10 ++++++++-- lib/active_mongoid/associations/metadata.rb | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 131b5bd742439d683e0e56ef83cc41faa2cc7f4d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%Wl&^6ur|JY7+!OLKW=1gGE)PN|4wQ(galT5ULSfPzZJ$5>v;H5+^|lk+S4b zegnb3@BtNTK7c<6XC9!~4qJrKTVeyOgKm59FjH0+qvg# zL)8?4O!P6K4z;LHF-1AHuFwi-1^zV!_}<+jm)ew2K&$(=i(MR$Pdn7buI|%_2Eg`y z7`MGJ_7KMpjXUt`OJT_IgQAT%-p>%dA~M7O%K31%TylDz?@dPo)&Dnd5C)^9Ts~zR z1>?-wP1CZ9R`J5c;xnfgPo2a~#-qABe9B9Y+@$BJ{Np$df9GBANzj?LN|*QIC~(5RzaRu-K^3tr0=NVR!?$bW)QIj5$gLDV0M<&8pzbcJMhKHiIoC0cfn*0pV zrWMc%Xa!ak;OB!4=U7v?kSLB0B;pAGG*B%JdH!3#V{C;rg$s#jfiW2hl%Y&LF_;WT zy{&jPg$s!?oS1riFf}q$FBB$5NB_2<6RSzosa8NMkXE1|izR;lH~;+pPY3CeRzNH8 zUn#(hhSR8HO6qRSO^)BSHv9#g8|N(~iV!5~Iu;pz6<>i1Lz~Srz?#B^MAX3akARRt KCt88Os=!Z7lg|YJ diff --git a/lib/active_mongoid/associations/document_relation/dependent.rb b/lib/active_mongoid/associations/document_relation/dependent.rb index 06bcfd5..548049e 100644 --- a/lib/active_mongoid/associations/document_relation/dependent.rb +++ b/lib/active_mongoid/associations/document_relation/dependent.rb @@ -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 diff --git a/lib/active_mongoid/associations/metadata.rb b/lib/active_mongoid/associations/metadata.rb index dc2104e..c5bad8d 100644 --- a/lib/active_mongoid/associations/metadata.rb +++ b/lib/active_mongoid/associations/metadata.rb @@ -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?