Skip to content

Commit

Permalink
pull the flash methods in to their own module
Browse files Browse the repository at this point in the history
We only want to activate flash when the user has enabled it.  Api
servers don't use flash, so add an empty implementation to the base
Request object.
  • Loading branch information
tenderlove committed Sep 25, 2015
1 parent 46cd257 commit add4648
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
3 changes: 3 additions & 0 deletions actionpack/lib/action_dispatch/http/request.rb
Expand Up @@ -384,6 +384,9 @@ def logger
get_header("action_dispatch.logger".freeze)
end

def commit_flash
end

private
def check_method(name)
HTTP_METHOD_LOOKUP[name] || raise(ActionController::UnknownHttpMethod, "#{name}, accepted HTTP methods are #{HTTP_METHODS[0...-1].join(', ')}, and #{HTTP_METHODS[-1]}")
Expand Down
72 changes: 38 additions & 34 deletions actionpack/lib/action_dispatch/middleware/flash.rb
@@ -1,40 +1,6 @@
require 'active_support/core_ext/hash/keys'

module ActionDispatch
class Request
# Access the contents of the flash. Use <tt>flash["notice"]</tt> to
# read a notice you put there or <tt>flash["notice"] = "hello"</tt>
# to put a new one.
def flash
flash = flash_hash
return flash if flash
self.flash = Flash::FlashHash.from_session_value(session["flash"])
end

def flash=(flash)
set_header Flash::KEY, flash
end

def flash_hash # :nodoc:
get_header Flash::KEY
end

def commit_flash # :nodoc:
session = self.session || {}
flash_hash = self.flash_hash

if flash_hash && (flash_hash.present? || session.key?('flash'))
session["flash"] = flash_hash.to_session_value
self.flash = flash_hash.dup
end

if (!session.respond_to?(:loaded?) || session.loaded?) && # (reset_session uses {}, which doesn't implement #loaded?)
session.key?('flash') && session['flash'].nil?
session.delete('flash')
end
end
end

# The flash provides a way to pass temporary primitive-types (String, Array, Hash) between actions. Anything you place in the flash will be exposed
# to the very next action and then cleared out. This is a great way of doing notices and alerts, such as a create
# action that sets <tt>flash[:notice] = "Post successfully created"</tt> before redirecting to a display action that can
Expand Down Expand Up @@ -72,6 +38,40 @@ def commit_flash # :nodoc:
class Flash
KEY = 'action_dispatch.request.flash_hash'.freeze

module RequestMethods
# Access the contents of the flash. Use <tt>flash["notice"]</tt> to
# read a notice you put there or <tt>flash["notice"] = "hello"</tt>
# to put a new one.
def flash
flash = flash_hash
return flash if flash
self.flash = Flash::FlashHash.from_session_value(session["flash"])
end

def flash=(flash)
set_header Flash::KEY, flash
end

def flash_hash # :nodoc:
get_header Flash::KEY
end

def commit_flash # :nodoc:
session = self.session || {}
flash_hash = self.flash_hash

if flash_hash && (flash_hash.present? || session.key?('flash'))
session["flash"] = flash_hash.to_session_value
self.flash = flash_hash.dup
end

if (!session.respond_to?(:loaded?) || session.loaded?) && # (reset_session uses {}, which doesn't implement #loaded?)
session.key?('flash') && session['flash'].nil?
session.delete('flash')
end
end
end

class FlashNow #:nodoc:
attr_accessor :flash

Expand Down Expand Up @@ -285,4 +285,8 @@ def stringify_array(array)

def self.new(app) app; end
end

class Request
prepend Flash::RequestMethods
end
end

0 comments on commit add4648

Please sign in to comment.