Permalink
Browse files

Added support add_flash_type

  • Loading branch information...
kennyj committed Jul 6, 2012
1 parent 48fe382 commit 0cb11d26213983eacaecb665818a519bd4ff4c7e
@@ -3,19 +3,44 @@ module Flash
extend ActiveSupport::Concern
included do
+ class_attribute :_flash_types
@josevalim

josevalim Jul 6, 2012

We probably want to pass :instance_methods => false too

+ self._flash_types = []
+
delegate :flash, :to => :request
- delegate :alert, :notice, :to => "request.flash"
- helper_method :alert, :notice
+ add_flash_type :alert, :notice
end
+ module ClassMethods
+ def add_flash_type(*types)
+ types.each do |type|
+ delegate type, :to => "request.flash"
+ helper_method type
+
+ ActionDispatch::Flash::FlashNow.class_eval <<-EOS
@josevalim

josevalim Jul 6, 2012

I don't think we should add these ones to AD::Flash::FlashNow and friends.

@kennyj

kennyj Jul 6, 2012

Owner

How about AD::Flash::FlashHash ? The below implementation is required by delegate type, :to => "request.flash".

@josevalim

josevalim Jul 6, 2012

Neither for AD::Flash::FlashHash. We should rewrite the delegate as a flash[] access.

+ def #{type}=(message)
+ self[:#{type}] = message
+ end
+ EOS
+
+ ActionDispatch::Flash::FlashHash.class_eval <<-EOS
+ def #{type}
+ self[:#{type}]
+ end
+ def #{type}=(message)
+ self[:#{type}] = message
+ end
+ EOS
+
+ _flash_types << type
+ end
+ end
+ end
protected
def redirect_to(options = {}, response_status_and_flash = {}) #:doc:
- if alert = response_status_and_flash.delete(:alert)
- flash[:alert] = alert
- end
-
- if notice = response_status_and_flash.delete(:notice)
- flash[:notice] = notice
+ self.class._flash_types.each do |flash_type|
+ if type = response_status_and_flash.delete(flash_type)
+ flash[flash_type] = type
+ end
end
if other_flashes = response_status_and_flash.delete(:flash)
@@ -59,15 +59,6 @@ def [](k)
@flash[k]
end
- # Convenience accessor for flash.now[:alert]=
- def alert=(message)
- self[:alert] = message
- end
-
- # Convenience accessor for flash.now[:notice]=
- def notice=(message)
- self[:notice] = message
- end
end
# Implementation detail: please do not change the signature of the
@@ -185,26 +176,6 @@ def sweep #:nodoc:
@discard.replace @flashes.keys
end
- # Convenience accessor for flash[:alert]
- def alert
- self[:alert]
- end
-
- # Convenience accessor for flash[:alert]=
- def alert=(message)
- self[:alert] = message
- end
-
- # Convenience accessor for flash[:notice]
- def notice
- self[:notice]
- end
-
- # Convenience accessor for flash[:notice]=
- def notice=(message)
- self[:notice] = message
- end
-
protected
def now_is_loaded?
@now
@@ -90,6 +90,10 @@ def render_with_flash_now_notice
def redirect_with_other_flashes
redirect_to '/wonderland', :flash => { :joyride => "Horses!" }
end
+
+ def redirect_with_foo_flash
+ redirect_to "/wonderland", :foo => 'for great justice'
+ end
end
tests TestController
@@ -203,13 +207,22 @@ def test_redirect_to_with_other_flashes
get :redirect_with_other_flashes
assert_equal "Horses!", @controller.send(:flash)[:joyride]
end
+
+ def test_redirect_to_with_adding_flash_type
+ @controller.class.add_flash_type :foo
+ get :redirect_with_foo_flash
+ assert_equal "for great justice", @controller.send(:flash)[:foo]
+ end
end
class FlashIntegrationTest < ActionDispatch::IntegrationTest
SessionKey = '_myapp_session'
SessionSecret = 'b3c631c314c0bbca50c1b2843150fe33'
class TestController < ActionController::Base
+
+ add_flash_type :foobar
+
def dont_set_flash
head :ok
end
@@ -227,6 +240,11 @@ def set_flash_now
def use_flash
render :inline => "flash: #{flash["that"]}"
end
+
+ def set_foobar
+ flash.foobar = "for great justice"
+ head :ok
+ end
end
def test_flash
@@ -266,6 +284,14 @@ def test_setting_flash_now_does_not_raise_in_following_requests
end
end
+ def test_added_flash_type_method
+ with_test_route_set do
+ get '/set_foobar'
+ assert_response :success
+ assert_equal 'for great justice', @controller.foobar
+ end
+ end
+
private
# Overwrite get to send SessionSecret in env hash

1 comment on commit 0cb11d2

👍

Please sign in to comment.