Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 38 lines (29 sloc) 2.632 kb
e1a484f @cainlevy importing untested concept code
authored
1 = NestedAssignment
ee80059 @cainlevy jotting down thoughts
authored
2
3 An attempt at solving the model (and controller) side of nested forms. Based on thoughts pulled from http://groups.google.com/group/rubyonrails-core/browse_thread/thread/4049b4b313fa8be2, http://groups.google.com/group/rubyonrails-core/browse_thread/thread/3c61e00916c365e5/f0a19fc01d0246fc, and work on ActiveScaffold.
4
e1a484f @cainlevy importing untested concept code
authored
5 == Goals:
ee80059 @cainlevy jotting down thoughts
authored
6
41f7a39 @cainlevy rdoc
authored
7 1. Assigning attributes should remain in-memory. Nothing should be permanent before #save. This means avoiding some of ActiveRecord's association assignment logic such as AssociationCollection#replace.
8 2. Deep and thorough validation of all new and changed associated records. In order for all possible errors to be available at once, no single invalid record may halt the process.
9 3. Support HTML-only forms. This means using a :_delete param to delete records, since that can be accomplished with a checkbox and does not require JavaScript to remove input fields from the form data. Credit to Josh Susser.
10 4. Use parameter hashes for collections instead of arrays, because params order can't be guaranteed through AJAX (credit to James Golick) and Rails' parameter parser has troubles with arrays of complex objects (e.g. params[:user][:photos][][:title]). The hash key should not be meaningful (such as the record id) or magical (such as new_1234), so that it may be generated by any means. Using a timestamp is particularly effective, since it also provides a maintainable order (credit to Eloy Duran).
11 5. Nested mass assignment should be enabled manually for security and performance reasons.
12 6. Nested mass assignment params should be keyed with a suffix (e.g. _params) to avoid complicating association assignment logic. This means params[:user][:photos_params] instead of params[:user][:photos], which assigns via User#photo_params= instead of User#photos=. The former can be tailored to the specific needs of form input, whereas the latter is best for in-memory association management. This constraint could conceivably be removed later, once mass assignment is more mature.
13 7. Skinny controllers. There's no footprint in the controller, actually. This is mass assignment.
ee80059 @cainlevy jotting down thoughts
authored
14
41f7a39 @cainlevy rdoc
authored
15 == Paradigm Params (see http://gist.github.com/10793)
e1a484f @cainlevy importing untested concept code
authored
16
17 {
41f7a39 @cainlevy rdoc
authored
18 # singular associations
19 :avatar_params => {
20 :id => '7',
21 :name => 'mugshot.jpg'
22 },
23
24 # plural associations
25 :tasks_params =>
e1a484f @cainlevy importing untested concept code
authored
26 {
41f7a39 @cainlevy rdoc
authored
27 # create
28 '1' => { :name => "Baz" },
29 # update
30 '2' => { :id => '3', :name => "Foo" },
31 # destroy
32 '3' => { :id => '5', :name => "Bar", :_delete => true}
e1a484f @cainlevy importing untested concept code
authored
33 }
34 }
35
36
ee80059 @cainlevy jotting down thoughts
authored
37 Copyright (c) 2008 Lance Ivy, released under the MIT license
Something went wrong with that request. Please try again.