mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #6992 from kennyj/improve_6977-2
Allow people to register their own flash types.
This commit is contained in:
commit
8a6780bd34
4 changed files with 65 additions and 14 deletions
|
@ -1,5 +1,15 @@
|
||||||
## Rails 4.0.0 (unreleased) ##
|
## Rails 4.0.0 (unreleased) ##
|
||||||
|
|
||||||
|
* Add `ActionController::Flash.add_flash_types` method to allow people to register their own flash types. e.g.:
|
||||||
|
|
||||||
|
class ApplicationController
|
||||||
|
add_flash_types :error, :warning
|
||||||
|
end
|
||||||
|
|
||||||
|
If you add the above code, you can use `<%= error %>` in an erb, and `rediect_to /foo, :error => 'message'` in a controller.
|
||||||
|
|
||||||
|
*kennyj*
|
||||||
|
|
||||||
* Remove Active Model dependency from Action Pack. *Guillermo Iguaran*
|
* Remove Active Model dependency from Action Pack. *Guillermo Iguaran*
|
||||||
|
|
||||||
* Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping:
|
* Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping:
|
||||||
|
|
|
@ -3,19 +3,34 @@ module ActionController #:nodoc:
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
included do
|
included do
|
||||||
|
class_attribute :_flash_types, :instance_methods => false
|
||||||
|
self._flash_types = []
|
||||||
|
|
||||||
delegate :flash, :to => :request
|
delegate :flash, :to => :request
|
||||||
delegate :alert, :notice, :to => "request.flash"
|
add_flash_types(:alert, :notice)
|
||||||
helper_method :alert, :notice
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
def add_flash_types(*types)
|
||||||
|
types.each do |type|
|
||||||
|
next if _flash_types.include?(type)
|
||||||
|
|
||||||
|
define_method(type) do
|
||||||
|
request.flash[type]
|
||||||
|
end
|
||||||
|
helper_method type
|
||||||
|
|
||||||
|
_flash_types << type
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
def redirect_to(options = {}, response_status_and_flash = {}) #:doc:
|
def redirect_to(options = {}, response_status_and_flash = {}) #:doc:
|
||||||
if alert = response_status_and_flash.delete(:alert)
|
self.class._flash_types.each do |flash_type|
|
||||||
flash[:alert] = alert
|
if type = response_status_and_flash.delete(flash_type)
|
||||||
end
|
flash[flash_type] = type
|
||||||
|
end
|
||||||
if notice = response_status_and_flash.delete(:notice)
|
|
||||||
flash[:notice] = notice
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if other_flashes = response_status_and_flash.delete(:flash)
|
if other_flashes = response_status_and_flash.delete(:flash)
|
||||||
|
|
|
@ -90,6 +90,10 @@ class FlashTest < ActionController::TestCase
|
||||||
def redirect_with_other_flashes
|
def redirect_with_other_flashes
|
||||||
redirect_to '/wonderland', :flash => { :joyride => "Horses!" }
|
redirect_to '/wonderland', :flash => { :joyride => "Horses!" }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def redirect_with_foo_flash
|
||||||
|
redirect_to "/wonderland", :foo => 'for great justice'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
tests TestController
|
tests TestController
|
||||||
|
@ -203,6 +207,12 @@ class FlashTest < ActionController::TestCase
|
||||||
get :redirect_with_other_flashes
|
get :redirect_with_other_flashes
|
||||||
assert_equal "Horses!", @controller.send(:flash)[:joyride]
|
assert_equal "Horses!", @controller.send(:flash)[:joyride]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_redirect_to_with_adding_flash_types
|
||||||
|
@controller.class.add_flash_types :foo
|
||||||
|
get :redirect_with_foo_flash
|
||||||
|
assert_equal "for great justice", @controller.send(:flash)[:foo]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class FlashIntegrationTest < ActionDispatch::IntegrationTest
|
class FlashIntegrationTest < ActionDispatch::IntegrationTest
|
||||||
|
@ -210,6 +220,9 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest
|
||||||
SessionSecret = 'b3c631c314c0bbca50c1b2843150fe33'
|
SessionSecret = 'b3c631c314c0bbca50c1b2843150fe33'
|
||||||
|
|
||||||
class TestController < ActionController::Base
|
class TestController < ActionController::Base
|
||||||
|
|
||||||
|
add_flash_types :bar
|
||||||
|
|
||||||
def set_flash
|
def set_flash
|
||||||
flash["that"] = "hello"
|
flash["that"] = "hello"
|
||||||
head :ok
|
head :ok
|
||||||
|
@ -223,6 +236,11 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest
|
||||||
def use_flash
|
def use_flash
|
||||||
render :inline => "flash: #{flash["that"]}"
|
render :inline => "flash: #{flash["that"]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_bar
|
||||||
|
flash[:bar] = "for great justice"
|
||||||
|
head :ok
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_flash
|
def test_flash
|
||||||
|
@ -262,6 +280,14 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_added_flash_types_method
|
||||||
|
with_test_route_set do
|
||||||
|
get '/set_bar'
|
||||||
|
assert_response :success
|
||||||
|
assert_equal 'for great justice', @controller.bar
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Overwrite get to send SessionSecret in env hash
|
# Overwrite get to send SessionSecret in env hash
|
||||||
|
|
|
@ -68,14 +68,14 @@ module ActionView
|
||||||
assert_nil self.class.determine_default_helper_class("String")
|
assert_nil self.class.determine_default_helper_class("String")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "delegates notice to request.flash" do
|
test "delegates notice to request.flash[:notice]" do
|
||||||
view.request.flash.expects(:notice).with("this message")
|
view.request.flash.expects(:[]).with(:notice)
|
||||||
view.notice("this message")
|
view.notice
|
||||||
end
|
end
|
||||||
|
|
||||||
test "delegates alert to request.flash" do
|
test "delegates alert to request.flash[:alert]" do
|
||||||
view.request.flash.expects(:alert).with("this message")
|
view.request.flash.expects(:[]).with(:alert)
|
||||||
view.alert("this message")
|
view.alert
|
||||||
end
|
end
|
||||||
|
|
||||||
test "uses controller lookup context" do
|
test "uses controller lookup context" do
|
||||||
|
|
Loading…
Reference in a new issue