tpkg / client

tpkg client code
MIT License
5 stars 7 forks source link

metadata HashWithIndifferentAccess incompatible with rails4 #44

Open jonnymacs opened 10 years ago

jonnymacs commented 10 years ago

The issue I came across has to do with the way rails4 casts parameters to ActionController::Parameters objects. There are some methods rails expects to exist on these objects, but the are missing if the tpkg gem is loaded

Easiest way to repro this would be to run a rails4 app that has tpkg in the Gemfile default group, then execute these steps from the rails console

$raw_parameters = { :teams => { :number => "37" } }
$parameters = ActionController::Parameters.new(raw_parameters)
$parameters.require(:teams).permit!

NoMethodError: undefined method `permitted=' for {"number"=>"37"}:ActiveSupport::HashWithIndifferentAccess
    from /Users/jmcallister/.rvm/gems/ruby-2.1.2@ypleads-sales-tool/gems/actionpack-4.1.4/lib/action_controller/metal/strong_parameters.rb:325:in `block in dup'
    from /Users/jmcallister/.rvm/gems/ruby-2.1.2@ypleads-sales-tool/gems/actionpack-4.1.4/lib/action_controller/metal/strong_parameters.rb:324:in `tap'
    from /Users/jmcallister/.rvm/gems/ruby-2.1.2@ypleads-sales-tool/gems/actionpack-4.1.4/lib/action_controller/metal/strong_parameters.rb:324:in `dup'
    from /Users/jmcallister/.rvm/gems/ruby-2.1.2@ypleads-sales-tool/gems/activesupport-4.1.4/lib/active_support/hash_with_indifferent_access.rb:50:in `with_indifferent_access'

from /Users/jmcallister/.rvm/gems/ruby-2.1.2@ypleads-sales-tool/gems/tpkg-2.3.3/lib/tpkg/metadata.rb:135:in `convert_value'
    from /Users/jmcallister/.rvm/gems/ruby-2.1.2@ypleads-sales-tool/gems/tpkg-2.3.3/lib/tpkg/metadata.rb:51:in `[]='

    from /Users/jmcallister/.rvm/gems/ruby-2.1.2@ypleads-sales-tool/gems/actionpack-4.1.4/lib/action_controller/metal/strong_parameters.rb:337:in `convert_hashes_to_parameters'
    from /Users/jmcallister/.rvm/gems/ruby-2.1.2@ypleads-sales-tool/gems/actionpack-4.1.4/lib/action_controller/metal/strong_parameters.rb:282:in `[]'
    from /Users/jmcallister/.rvm/gems/ruby-2.1.2@ypleads-sales-tool/gems/tpkg-2.3.3/lib/tpkg/metadata.rb:36:in `default'

The output should be

=> {"number"=>"37"}

Now the tpkg gem should not get loaded with rails. It is / was getting loaded b/c it is / was part of the default group in bundler in our application. We no longer include tpkg in the default bundler group, which resolves the issue.

But it still seems risky to define HashWithIndifferentAccess class in tpkg in the event that it's already defined by someone else.

Perhaps there is a way to conditionally create HashWithIndifferentAccess if it's not already defined by someone else (like ActiveSupport)?

some references https://github.com/rails/strong_parameters https://github.com/rails/strong_parameters/issues/140

tdombrowski commented 10 years ago

Conditional definition of HashWithIndifferentAccess can also be risky, if the active_support version does exist, but is or becomes incompatible with the way tpkg uses it.

Might be safer to rename the class entirely, or put it into a module to namespace it (along with other classes that may be borrowed from active_support)