Skip to content

Commit

Permalink
Don't force Hash::update to call to_hash unconditionally (fixes break…
Browse files Browse the repository at this point in the history
…age with Devise 1.1.5)

Devise has a MonitoredHash class which implements #update and #to_hash
entirely reasonably as follows:

    def update(other_hash)
      other_hash.each_pair { |key, value| regular_writer(convert_key(key), value) }
      self
    end

    def to_hash; Hash.new.update(self) end

This causes an infinite recursion and subsequent stack overflow in the
presence of slim_scrooge's override of Hash#update.

We fix this by modifying slim_scrooge's override of that method to
only force #to_hash on instances of MonitoredHash

(See https://github.com/plataformatec/devise/blob/master/lib/devise/rails/warden_compat.rb#L60)
  • Loading branch information
purcell committed Dec 10, 2010
1 parent 2e973d7 commit 28c9ed6
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/slim_scrooge/monitored_hash.rb
Expand Up @@ -87,17 +87,19 @@ def self._load(str)
end
end

# We need to change the update method of Hash so that it *always* calls
# to_hash. This is because it normally checks if other_hash is a kind of
# Hash, and doesn't bother calling to_hash if so. But we need it to call
# to_hash, because otherwise update will not get the complete columns
# from a MonitoredHash
# We need to change the update method of Hash so that it *always*
# calls to_hash on MonitoredHash instances. This is because it
# normally checks if other_hash is a kind of Hash, and doesn't bother
# calling to_hash if so. But we need it to call to_hash, because
# otherwise update will not get the complete columns from a
# MonitoredHash
#
# This is not harmful - to_hash in a regular Hash just returns self.
#
class Hash
alias_method :c_update, :update
def update(other_hash, &block)
c_update(other_hash.to_hash, &block)
c_update(other_hash.is_a?(::SlimScrooge::MonitoredHash) ? other_hash.to_hash : other_hash,
&block)
end
end

0 comments on commit 28c9ed6

Please sign in to comment.