Code Monkey home page Code Monkey logo

Comments (9)

daniel-rikowski avatar daniel-rikowski commented on August 28, 2024

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

from delocalize.

stephanvd avatar stephanvd commented on August 28, 2024

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.

from delocalize.

daniel-rikowski avatar daniel-rikowski commented on August 28, 2024

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

from delocalize.

clemens avatar clemens commented on August 28, 2024

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

from delocalize.

clemens avatar clemens commented on August 28, 2024

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.

from delocalize.

daniel-rikowski avatar daniel-rikowski commented on August 28, 2024

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

from delocalize.

clemens avatar clemens commented on August 28, 2024

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.

from delocalize.

daniel-rikowski avatar daniel-rikowski commented on August 28, 2024

Ah, thank you for sorting this out.

I also gave rails5 a test drive and works.

from delocalize.

clemens avatar clemens commented on August 28, 2024

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

from delocalize.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.