rails/strong_parameters — pull request #87 by HurricaneJames (2013-01-22 17:16:55+00:00)
Title: Modify strong_parameters to be more compliant with accepts_nested_attributes_for

This is a follow up to https://github.com/rails/strong_parameters/pull/86

I wanted to make strong_parameters work with `accepts_nested_attributes_for` when `fields_for` was not the method used to generate fields. In the previous discussion there was some argument about having to dynamically define the permitted fields. However, that is not necessary because the keys do not matter, they do not mean anything, `accepts_nested_attributes_for ignores` them. In fact, the current strong_parameters code takes this into account by looking for any hash with all numeric keys, assuming that hash is going to be used with `accepts_nested_attributes_for`, and throwing out the keys.

So, I spent a couple hours learning how strong_parameters works and realized that my previous solution was not particularly good (read pretty bad). It didn't cause any problems with my code, but it did cause some with the gem's tests. Anyway, I went back and did some thinking. I realized that what we really need to look for isn't a regex on every value in the hash, but the name of the hash. If its name ends in `_attributes` then it should be treated as an attributes hash where the keys are ignored and the values are processed.

This pull request addresses changes the `each_element` method to do a single regex check on the original key name from `hash_filter` method. I also tossed in a couple of happy pass tests to make sure that strong_parameters was processing _attributes hashes with random keys and no keys (which are actually arrays not hashes) in addition to the numeric keys `fields_for` generates.