Ticket #481 (closed defect: duplicate)

Opened 10 months ago

Last modified 10 months ago

[PATCH] Hash#environmentalize_keys! ate my data!

Reported by: jamie.devja..@tracefunc.com Assigned to:
Priority: medium Milestone: The Future
Component: Merb Keywords:
Cc:

Description

In a spec, I was calling a few home-grown helpers with this code:

POST '/customers/create', :customer => {:name => 'foo'}

which translated to

post '/customers/create', :query_string => 'customer[name]=foo'

I love those helpers.

Anyway, when the FakeRequest? is created, it merges in :QUERY_STRING to the options hash. It then calls environmentalize_keys! on the options hash, which due to the indeterminate order of hash keys *sometimes* overwrites the :QUERY_STRING value (in my example, nil) with the :query_string value, and everything's happy. But sometimes, the :QUERY_STRING value is the one that gets put in place, and my tests start failing for no good reason.

So, I've changed the method to ignore keys with a nil value, which solves my current problem.

  def environmentize_keys!
    keys.each do |key|
      val = delete(key)
      next if val.nil?
      self[key.to_s.upcase] = val
    end
    self
  end

My other option was to | |= val in, so it would preserve a key with an explicit nil value, but that's somewhat redundant since a missing key query returns nil as well.

Change History

02/11/08 13:44:44 changed by shayarne..@gmail.com

  • status changed from new to closed.
  • resolution set to duplicate.