Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Nested with_options merge hash values

#490

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 }

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

State: resolved
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Lawrence Pit
    Lawrence Pit
    • 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 }
    

    June 27th, 2008 @ 01:03 AM

  2. Pratik
    Pratik
    • Assigned user set to Pratik

    Hey Lawrence,

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

    July 12th, 2008 @ 04:50 PM

  3. Lawrence Pit
    Lawrence Pit

    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'.

    July 14th, 2008 @ 03:01 AM

  4. Lawrence Pit
  5. Lawrence Pit
    Lawrence Pit

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

       def is_a?(o)
         o == Hash
       end
    

    Last patch implements this. Simplifies deep_merge.

    July 14th, 2008 @ 03:17 AM

  6. Repository
    Repository
    • 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...

    July 17th, 2008 @ 02:02 AM

  7. Pratik
    Pratik
    • 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. )

    July 17th, 2008 @ 02:20 AM

  8. Pratik
    Pratik
    • State changed from new to resolved

    July 17th, 2008 @ 02:20 AM