nathanvda/cocoon — pull request #64 by semanticart (2012-05-04 23:41:46+00:00)
Title: Use association build methods instead of assoc.klass.new

I had a model whose relationship had some conditions like the following example:

has_many :orphan_posts,
class_name: 'Post',
conditions: {parent_id: nil, parent_type: nil}

Within the Post model, the parent_id and parent_type are protected attributes and with `config.active_record.mass_assignment_sanitizer = :strict` set, cocoon's `create_object` method throws mass-assignment errors when it tries to do the `assoc.klass.new(*conditions)`

By using the association build methods, we can use the built-in Rails goodness for handling these attribute accessibility issues.

If you simply do the `attr_protected` on the author attribute of Comment without applying this patch, then you'll see a failure on the "should create correct association" test.

I added an additional test to keep the 100% code coverage, but there's probably room to improve testing this functionality. If you have ideas for better tests, let me know.