clemens / delocalize

Date/time and number parsing for Rails.

Home Page:http://www.railway.at

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Different structure of params in Rails 5

daniel-rikowski opened this issue · comments

It's that time again: A new Rails release is around the corner, bearing the usual breaking changes.

In Rails 5.0.0.rc1 nested parameter hashes are no longer of type Hash but ActionController::Parameters.

Consider this typical params hash:

{ id: 1, action: 'update', controller: 'articles', article: { title: 'New release' } }

In Rails 4.2.6:

params.class == ActionController::Parameters
params[:article].class == Hash

In Rails 5.0.0.rc1

params.class == ActionController::Parameters
params[:article].class == ActionController::Parameters

There are two locations where delocalize assumes the nested params are instances of Hash which now also have to check for ActionController::Parameters. Unfortunately ActionController::Parameters is not defined in earlier versions of Rails, so the code is a little more involved.

Thoughts, ideas, suggestions?

@clemens Would you accept a pull request for this or do you already have something different in mind?

Any thoughts on this, now that Rails 5 is released?

I have managed to get it for rails 5 working by doing this:
https://github.com/clemens/delocalize/compare/master...zilverline:d264d6fd1f627e47fef317cdeea78ca5d273cf88?expand=1

It's backward compatible, but it ain't pretty.

Still, not bad!

Mine isn't pretty either:

def delocalize_hash(hash, options, base_key_stack = [])
  hash.each do |key, value|
    key_stack = [*base_key_stack, key] # don't modify original key stack!

    hash[key] = if value.is_a?(Hash) or
                  (defined?(ActionController::Parameters) and value.is_a?(ActionController::Parameters))
                  delocalize_hash(value, options, key_stack)
                elsif value.is_a?(Array)
                  key_stack += [:[]] # pseudo-key to denote arrays
                  value.map { |item| delocalize_parse(options, key_stack, item) }
                else
                  delocalize_parse(options, key_stack, value)
                end
  end
end

I'll have to have a closer look. I'll schedule some time tomorrow for it.

If you want, check out the rails5 branch and let me know if this works for you. each_pair is implemented for Hash as well as ActionController::Parameters so this should be fine. It's also implemented for a small number of other classes (e.g. Struct) but since the method in question is private and not intended to be given anything but Rails params, I'm kind of OK with that.

Nice. But doesn't delocalize_parser_for need the same fix, too?

I had failing tests for Rails 5 before the fixes but now everything's green (except for JRuby – I'll investigate that). So either this isn't a problem or the tests aren't covering a scenario that has issues.

If I read the code right, here's what happens in Rails 5's ActionController::Parameters: The params hash gets injected into the Parameters object and stored in an instance variable as a ActiveSupport::HashWithIndifferentAccess (https://github.com/rails/rails/blob/5-0-0/actionpack/lib/action_controller/metal/strong_parameters.rb#L142). This means that while the shell doesn't subclass Hash anymore, the core still does. So when I'm calling delocalize_hash with the outer shell, it receives an instance of ActionController::Parameters (which is not a Hash) but then iterates on its attributes (which still are a Hash or Array or whatever but not a ActionController::Parameters instance).

Long story short: To me it looks like we don't need to adapt delocalize_parser_for as well since it's always called with the individual elements of ActionController::Parameters which are never an instance of that class (unless something goes very wrong or someone tries to do some wonky stuff).

If anyone's still having trouble with it, I'm happy to accept a failing test case and fix it accordingly.

Ah, thank you for sorting this out.

I also gave rails5 a test drive and works.

I've released 1.1.0 with Rails 5 support. Thanks for the initial fix and helping with discussion and testing!