2004-11-23 20:04:44 -05:00
|
|
|
module ActionController #:nodoc:
|
|
|
|
module Flash
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-05-20 19:38:48 -04:00
|
|
|
|
2009-12-17 19:37:11 -05:00
|
|
|
included do
|
2012-07-07 08:16:48 -04:00
|
|
|
class_attribute :_flash_types, instance_accessor: false
|
2012-07-06 14:34:56 -04:00
|
|
|
self._flash_types = []
|
|
|
|
|
2012-07-07 08:16:48 -04:00
|
|
|
delegate :flash, to: :request
|
2012-07-06 14:34:56 -04:00
|
|
|
add_flash_types(:alert, :notice)
|
2009-12-17 19:37:11 -05:00
|
|
|
end
|
|
|
|
|
2012-07-06 14:34:56 -04:00
|
|
|
module ClassMethods
|
2013-07-10 11:17:46 -04:00
|
|
|
# Creates new flash types. You can pass as many types as you want to create
|
|
|
|
# flash types other than the default <tt>alert</tt> and <tt>notice</tt> in
|
|
|
|
# your controllers and views. For instance:
|
|
|
|
#
|
|
|
|
# # in application_controller.rb
|
|
|
|
# class ApplicationController < ActionController::Base
|
|
|
|
# add_flash_types :warning
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # in your controller
|
|
|
|
# redirect_to user_path(@user), warning: "Incomplete profile"
|
|
|
|
#
|
|
|
|
# # in your view
|
|
|
|
# <%= warning %>
|
|
|
|
#
|
|
|
|
# This method will automatically define a new method for each of the given
|
|
|
|
# names, and it will be available in your views.
|
2012-07-06 14:34:56 -04:00
|
|
|
def add_flash_types(*types)
|
|
|
|
types.each do |type|
|
|
|
|
next if _flash_types.include?(type)
|
2012-07-07 08:16:48 -04:00
|
|
|
|
2012-07-06 14:34:56 -04:00
|
|
|
define_method(type) do
|
|
|
|
request.flash[type]
|
|
|
|
end
|
|
|
|
helper_method type
|
|
|
|
|
2013-09-13 09:03:38 -04:00
|
|
|
self._flash_types += [type]
|
2009-12-28 19:28:26 -05:00
|
|
|
end
|
2012-07-06 14:34:56 -04:00
|
|
|
end
|
|
|
|
end
|
2009-12-20 21:00:04 -05:00
|
|
|
|
2012-07-06 14:34:56 -04:00
|
|
|
protected
|
|
|
|
def redirect_to(options = {}, response_status_and_flash = {}) #:doc:
|
|
|
|
self.class._flash_types.each do |flash_type|
|
|
|
|
if type = response_status_and_flash.delete(flash_type)
|
|
|
|
flash[flash_type] = type
|
|
|
|
end
|
2009-12-28 19:28:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if other_flashes = response_status_and_flash.delete(:flash)
|
|
|
|
flash.update(other_flashes)
|
|
|
|
end
|
|
|
|
|
|
|
|
super(options, response_status_and_flash)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
end
|