Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Nested attributes plugin: remove _remove/_delete flags from attributes #455

Merged
merged 2 commits into from about 2 years ago

2 participants

r-stu31 Jeremy Evans
r-stu31

HTML forms with checkboxes for removing/deleting of associations also supply 'false' values. This leads to complications - one needs to have a "blank" _remove/_delete setters or set Model.strict_param_setting = false.

Provided fix solves the problem by removing the flags from attributes hash. I've made two decisions, which you may want to reconsider:

  • the removing is done on a dup of the original hash to preserve it
  • i've reordered the conditions to remove the flags only if the corresponding settings (reflection[:nested_attributes][:remove] resp. reflection[:nested_attributes][:destroy]) apply
added some commits March 24, 2012
r-stu31 Add tests for updating associated objects with _delete/_remove flags …
…set to false.
dbe1e41
r-stu31 Avoid complications with _delete/_remove flags being passed to nested…
…_attributes_update if they are set to false: remove the _delete/_remove flags from attributes if deleting/removing the associated objects is configured. Do not modify the original attributes hash.
945ef98
Jeremy Evans
Owner

This looks good. I should have time to merge and test it on Monday. Thanks for the help!

Jeremy Evans jeremyevans merged commit 945ef98 into from March 26, 2012
Jeremy Evans jeremyevans closed this March 26, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Mar 24, 2012
r-stu31 Add tests for updating associated objects with _delete/_remove flags …
…set to false.
dbe1e41
r-stu31 Avoid complications with _delete/_remove flags being passed to nested…
…_attributes_update if they are set to false: remove the _delete/_remove flags from attributes if deleting/removing the associated objects is configured. Do not modify the original attributes hash.
945ef98
This page is out of date. Refresh to see the latest.
5  lib/sequel/plugins/nested_attributes.rb
@@ -218,9 +218,10 @@ def nested_attributes_setter(reflection, attributes)
218 218
           modified!
219 219
           klass = reflection.associated_class
220 220
           if pk = attributes.delete(klass.primary_key) || attributes.delete(klass.primary_key.to_s)
221  
-            if klass.db.send(:typecast_value_boolean, attributes[:_delete] || attributes['_delete']) && reflection[:nested_attributes][:destroy]
  221
+            attributes = attributes.dup
  222
+            if reflection[:nested_attributes][:destroy] && klass.db.send(:typecast_value_boolean, attributes.delete(:_delete) || attributes.delete('_delete'))
222 223
               nested_attributes_remove(reflection, pk, :destroy=>true)
223  
-            elsif klass.db.send(:typecast_value_boolean, attributes[:_remove] || attributes['_remove']) && reflection[:nested_attributes][:remove]
  224
+            elsif reflection[:nested_attributes][:remove] && klass.db.send(:typecast_value_boolean, attributes.delete(:_remove) || attributes.delete('_remove'))
224 225
               nested_attributes_remove(reflection, pk)
225 226
             else
226 227
               nested_attributes_update(reflection, pk, attributes)
20  spec/extensions/nested_attributes_spec.rb
@@ -110,6 +110,16 @@ def check_sql_array(*shoulds)
110 110
     @db.sqls.should == ["UPDATE artists SET name = 'Ar' WHERE (id = 20)", "UPDATE albums SET name = 'Al2' WHERE (id = 10)"]
111 111
   end
112 112
   
  113
+  it "should support updating one_to_many objects with _delete/_remove flags set to false" do
  114
+    al = @Album.load(:id=>10, :name=>'Al')
  115
+    ar = @Artist.load(:id=>20, :name=>'Ar')
  116
+    ar.associations[:albums] = [al]
  117
+    ar.set(:albums_attributes=>[{:id=>10, :name=>'Al2', :_delete => 'f', :_remove => '0'}])
  118
+    @db.sqls.should == []
  119
+    ar.save
  120
+    @db.sqls.should == ["UPDATE artists SET name = 'Ar' WHERE (id = 20)", "UPDATE albums SET name = 'Al2' WHERE (id = 10)"]
  121
+  end
  122
+  
113 123
   it "should support updating many_to_many objects" do
114 124
     a = @Album.load(:id=>10, :name=>'Al')
115 125
     t = @Tag.load(:id=>20, :name=>'T')
@@ -120,6 +130,16 @@ def check_sql_array(*shoulds)
120 130
     @db.sqls.should == ["UPDATE albums SET name = 'Al' WHERE (id = 10)", "UPDATE tags SET name = 'T2' WHERE (id = 20)"]
121 131
   end
122 132
   
  133
+  it "should support updating many_to_many objects with _delete/_remove flags set to false" do
  134
+    a = @Album.load(:id=>10, :name=>'Al')
  135
+    t = @Tag.load(:id=>20, :name=>'T')
  136
+    a.associations[:tags] = [t]
  137
+    a.set(:tags_attributes=>[{:id=>20, :name=>'T2', '_delete' => false, '_remove' => 'F'}])
  138
+    @db.sqls.should == []
  139
+    a.save
  140
+    @db.sqls.should == ["UPDATE albums SET name = 'Al' WHERE (id = 10)", "UPDATE tags SET name = 'T2' WHERE (id = 20)"]
  141
+  end
  142
+  
123 143
   it "should support removing many_to_one objects" do
124 144
     al = @Album.load(:id=>10, :name=>'Al')
125 145
     ar = @Artist.load(:id=>20, :name=>'Ar')
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.