Skip to content
This repository
Browse code

[ WIP ] Delay removal to docs to post validation.

First part of solving #2367 although we still have broken other specs
now with the first part of the fix.
  • Loading branch information...
commit 0998e9097eae16c5b59014d958afa0ddff408f73 1 parent 57d3fa7
Durran Jordan authored
4  .travis.yml
@@ -24,6 +24,8 @@ matrix:
24 24
     - rvm: jruby-head
25 25
       jdk: openjdk7
26 26
       env: JRUBY_OPTS="-Xcompile.invokedynamic=false"
27  
-
  27
+branches:
  28
+  except:
  29
+    - nested-destroy-validation-fail
28 30
 services:
29 31
   - mongodb
14  lib/mongoid/atomic.rb
@@ -114,6 +114,7 @@ def atomic_array_add_to_sets
114 114
     #
115 115
     # @since 2.1.0
116 116
     def atomic_updates
  117
+      process_flagged_destroys
117 118
       mods = Modifiers.new
118 119
       generate_atomic_updates(mods, self)
119 120
       _children.each do |child|
@@ -302,6 +303,19 @@ def flag_as_destroyed
302 303
       atomic_path
303 304
     end
304 305
 
  306
+    def flagged_destroys
  307
+      @flagged_destroys ||= []
  308
+    end
  309
+
  310
+    def process_flagged_destroys
  311
+      _assigning do
  312
+        flagged_destroys.each do |block|
  313
+          block.call
  314
+        end
  315
+      end
  316
+      flagged_destroys.clear
  317
+    end
  318
+
305 319
     private
306 320
 
307 321
     # Get the atomic paths utility for this document.
1  lib/mongoid/persistence/modification.rb
@@ -20,6 +20,7 @@ module Modification
20 20
       # @since 2.1.0
21 21
       def prepare(&block)
22 22
         return false if validating? && document.invalid?(:update)
  23
+        document.process_flagged_destroys
23 24
         result = document.run_callbacks(:save) do
24 25
           document.run_callbacks(:update) do
25 26
             yield(document); true
7  lib/mongoid/relations/builders/nested_attributes/many.rb
@@ -103,8 +103,11 @@ def process_attributes(parent, attrs, options)
103 103
               converted = first ? convert_id(first.class, id) : id
104 104
               doc = existing.find(converted)
105 105
               if destroyable?(attrs)
106  
-                existing.delete(doc)
107  
-                doc.destroy unless doc.embedded? || doc.destroyed?
  106
+                doc.flagged_for_destroy = true
  107
+                parent.flagged_destroys.push(->{
  108
+                  existing.delete(doc)
  109
+                  doc.destroy unless doc.embedded? || doc.destroyed?
  110
+                })
108 111
               else
109 112
                 attrs.delete_id
110 113
                 if metadata.embedded?
44  spec/mongoid/nested_attributes_spec.rb
@@ -1258,6 +1258,50 @@
1258 1258
 
1259 1259
               context "when allow_destroy is true" do
1260 1260
 
  1261
+                context "when the parent validation failed" do
  1262
+
  1263
+                  before(:all) do
  1264
+                    Band.validates_presence_of :name
  1265
+                    Band.accepts_nested_attributes_for :records, :allow_destroy => true
  1266
+                  end
  1267
+
  1268
+                  after(:all) do
  1269
+                    Band.send(:undef_method, :records_attributes=)
  1270
+                    Band.reset_callbacks(:validate)
  1271
+                  end
  1272
+
  1273
+                  let!(:band) do
  1274
+                    Band.create(name: "Depeche Mode")
  1275
+                  end
  1276
+
  1277
+                  let!(:record) do
  1278
+                    band.records.create
  1279
+                  end
  1280
+
  1281
+                  let(:attributes) do
  1282
+                    {
  1283
+                      name: nil,
  1284
+                      records_attributes: { "foo" => { "id" => record.id, "_destroy" => true }}
  1285
+                    }
  1286
+                  end
  1287
+
  1288
+                  before do
  1289
+                    band.update_attributes(attributes)
  1290
+                  end
  1291
+
  1292
+                  it "does not remove the child document" do
  1293
+                    band.records.should_not be_empty
  1294
+                  end
  1295
+
  1296
+                  it "keeps the child flagged for destruction" do
  1297
+                    record.should be_flagged_for_destroy
  1298
+                  end
  1299
+
  1300
+                  it "does not persist any change" do
  1301
+                    band.reload.records.should eq([ record ])
  1302
+                  end
  1303
+                end
  1304
+
1261 1305
                 context "when the child accesses the parent after destroy" do
1262 1306
 
1263 1307
                   before(:all) do

0 notes on commit 0998e90

Please sign in to comment.
Something went wrong with that request. Please try again.