Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.
/ nested_assignment Public archive

A proof-of-concept for adding nested mass assignment to ActiveRecord.

License

Notifications You must be signed in to change notification settings

cainlevy/nested_assignment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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[:photos][]). 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[:photos_params] instead of params[: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

About

A proof-of-concept for adding nested mass assignment to ActiveRecord.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages