#490 √ resolved
Lawrence Pit

Nested with_options merge hash values

Reported by Lawrence Pit | June 26th, 2008 @ 01:07 AM

Consider this:

map.with_options :conditions => { :subdomain => /^www$/ } do |m|

m.with_options :conditions => { :method => :get } do |home|

home.main_home '/'

  1. etc.

end

  1. etc.

end

I expected that hitting http://foo.mydomain.com would not match home.main_home, but it does. This is because the with_options method overwrites the value for :conditions, losing the condition for subdomain.

Attached is a patch including tests that will merge hash values within options.

In the above example this would effectively result in:

map.main_home "/", :conditions => { :subdomain => /^www$/, :method => :get }

Comments and changes to this ticket

  • Lawrence Pit

    Lawrence Pit June 27th, 2008 @ 01:03 AM

    • → Tag changed from “activesupport” to “activesupport core_ext patch tests”

    Again, hopefully nicely formatted this time:

    map.with_options :conditions => { :subdomain => /^www$/ } do |m|
      m.with_options :conditions => { :method => :get } do |home|
    
        home.root '/'
      end
    end 
    

    should equal:

    map.root '/', :conditions => { :subdomain => /^www$/, :method => :get }
    
  • Pratik

    Pratik July 12th, 2008 @ 04:50 PM

    • → Assigned user changed from “” to “Pratik”

    Hey Lawrence,

    Looks nice. I wonder if we should rather add Hash#deep_merge method and simply use it here. Thoughts ?

  • Lawrence Pit

    Lawrence Pit July 14th, 2008 @ 03:01 AM

    Attached version with deep_merge.

    The code of deep_merge is a bit ugly though... You need to test for :to_hash for it to work with with_options, because OrderedHash is an Array, not a Hash. Nicer would of course be if you only needed to test for is_a?(Hash).

    Secondly, calling is_a?(Hash) doesn't work within deep_merge. hash/conversions.rb shows the same issue, it comments:

    1. something weird with classes not matching here. maybe singleton methods breaking is_a?

    hence the use of: val.class.to_s == 'Hash'.

  • Lawrence Pit
  • Lawrence Pit

    Lawrence Pit July 14th, 2008 @ 03:17 AM

    just thinking, maybe we should add this method to OrderedHash? :

       def is_a?(o)
         o == Hash
       end
    

    Last patch implements this. Simplifies deep_merge.

  • Repository

    Repository July 17th, 2008 @ 02:02 AM

    • → State changed from “new” to “resolved”

    (from [40dbebba28bfa1c55737da7354542c3bdca4e1a1]) Allow deep merging of hash values for nested with_options. [#490 state:resolved]

    Signed-off-by: Pratik Naik

    http://github.com/rails/rails/co...

  • Pratik

    Pratik July 17th, 2008 @ 02:20 AM

    • → State changed from “resolved” to “new”

    Intentionally committed version without OrderedHash#is_a? as OrderedHash doesn't really behave like a hash ( lots of missing features like merge/update/etc. )

  • Pratik

    Pratik July 17th, 2008 @ 02:20 AM

    • → State changed from “new” to “resolved”

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

Repository is at http://github.com/rails/rails

Check out the development master (Edge Rails):

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..

Shared Ticket Bins

People watching this ticket