public
Description: A proof-of-concept for adding nested mass assignment to ActiveRecord.
Homepage: http://codelevy.com/
Clone URL: git://github.com/cainlevy/nested_assignment.git
commit  5828485d657c1a8f468f4ea64f3ba273fff532b0
tree    c258e98ae239821714c04e38a0e4765d3b612bd5
parent  ed595a7da8f40a894eab7cb542ab0c0b60f0c759
name age message
file .gitignore Fri Dec 12 14:38:52 -0800 2008 ignore the test log [cainlevy]
file MIT-LICENSE Thu Dec 11 14:19:52 -0800 2008 jotting down thoughts [cainlevy]
file README.rdoc Fri Dec 12 14:48:46 -0800 2008 rdoc [cainlevy]
file Rakefile Fri Dec 12 14:48:46 -0800 2008 rdoc [cainlevy]
file init.rb Thu Dec 11 14:43:43 -0800 2008 importing and adapting my database-driven test ... [cainlevy]
directory lib/ Loading commit data...
directory test/
README.rdoc

NestedAssignment

An attempt at solving the model (and controller) side of nested forms. Based on thoughts pulled from groups.google.com/group/rubyonrails-core/browse_thread/thread/4049b4b313fa8be2, groups.google.com/group/rubyonrails-core/browse_thread/thread/3c61e00916c365e5/f0a19fc01d0246fc, and work on ActiveScaffold.

Goals:

  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.
  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.
  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.
  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).
  5. Nested mass assignment should be enabled manually for security and performance reasons.
  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.
  7. Skinny controllers. There’s no footprint in the controller, actually. This is mass assignment.

Paradigm Params (see gist.github.com/10793)

  {
    # singular associations
    :avatar_params => {
      :id => '7',
      :name => 'mugshot.jpg'
    },

    # plural associations
    :tasks_params =>
      {
        # create
        '1' => { :name => "Baz" },
        # update
        '2' => { :id => '3', :name => "Foo" },
        # destroy
        '3' => { :id => '5', :name => "Bar", :_delete => true}
      }
  }

Copyright © 2008 Lance Ivy, released under the MIT license