Skip to content

Commit

Permalink
Use initialize_copy! to proper initialize now on clone.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Apr 19, 2011
1 parent 89ed9fb commit 2f549b8
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions actionpack/lib/action_dispatch/middleware/flash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Flash
KEY = 'action_dispatch.request.flash_hash'.freeze

class FlashNow #:nodoc:
attr_accessor :flash

def initialize(flash)
@flash = flash
end
Expand All @@ -66,14 +68,6 @@ def alert=(message)
def notice=(message)
self[:notice] = message
end

def close!(new_flash)
@flash = new_flash
end

def closed?
@flash.closed?
end
end

class FlashHash
Expand All @@ -86,6 +80,14 @@ def initialize #:nodoc:
@now = nil
end

def initialize_copy(other)
if other.now_is_loaded?
@now = other.now.dup
@now.flash = self
end
super
end

def []=(k, v) #:nodoc:
raise ClosedError, :flash if closed?
keep(k)
Expand Down Expand Up @@ -150,16 +152,12 @@ def replace(h) #:nodoc:
#
# Entries set via <tt>now</tt> are accessed the same way as standard entries: <tt>flash['my-key']</tt>.
def now
@now = (!@now || @now.closed?) ? FlashNow.new(self) : @now
@now ||= FlashNow.new(self)
end

attr_reader :closed
alias :closed? :closed

def close!
@closed = true
@now.close!(self) if @now
end
def close!; @closed = true; end

# Keeps either the entire current flash or a specific flash entry available for the next action:
#
Expand Down Expand Up @@ -214,7 +212,12 @@ def notice=(message)
self[:notice] = message
end

private
protected

def now_is_loaded?
!!@now
end

# Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
# use() # marks the entire flash as used
# use('msg') # marks the "msg" entry as used
Expand Down

1 comment on commit 2f549b8

@flop
Copy link
Contributor

@flop flop commented on 2f549b8 Apr 20, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx for this.. I didn't know that you can add behavior on dup with initialize_copy.

Please sign in to comment.