Skip to content

Commit

Permalink
Added support add_flash_types
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyj committed Jul 7, 2012
1 parent c55df93 commit 238a425
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
10 changes: 10 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
## 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*

* Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping:
Expand Down
31 changes: 23 additions & 8 deletions actionpack/lib/action_controller/metal/flash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,34 @@ module Flash
extend ActiveSupport::Concern

included do
class_attribute :_flash_types, :instance_methods => false
self._flash_types = []

delegate :flash, :to => :request
delegate :alert, :notice, :to => "request.flash"
helper_method :alert, :notice
add_flash_types(:alert, :notice)
end

protected
def redirect_to(options = {}, response_status_and_flash = {}) #:doc:
if alert = response_status_and_flash.delete(:alert)
flash[:alert] = alert
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

if notice = response_status_and_flash.delete(:notice)
flash[:notice] = notice
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
end

if other_flashes = response_status_and_flash.delete(:flash)
Expand Down
26 changes: 26 additions & 0 deletions actionpack/test/controller/flash_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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_types
@controller.class.add_flash_types :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_types :bar

def set_flash
flash["that"] = "hello"
head :ok
Expand All @@ -223,6 +236,11 @@ def set_flash_now
def use_flash
render :inline => "flash: #{flash["that"]}"
end

def set_bar
flash[:bar] = "for great justice"
head :ok
end
end

def test_flash
Expand Down Expand Up @@ -262,6 +280,14 @@ def test_setting_flash_now_does_not_raise_in_following_requests
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

# Overwrite get to send SessionSecret in env hash
Expand Down
12 changes: 6 additions & 6 deletions actionpack/test/template/test_case_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ class GeneralViewTest < ActionView::TestCase
assert_nil self.class.determine_default_helper_class("String")
end

test "delegates notice to request.flash" do
view.request.flash.expects(:notice).with("this message")
view.notice("this message")
test "delegates notice to request.flash[:notice]" do
view.request.flash.expects(:[]).with(:notice)
view.notice
end

test "delegates alert to request.flash" do
view.request.flash.expects(:alert).with("this message")
view.alert("this message")
test "delegates alert to request.flash[:alert]" do
view.request.flash.expects(:[]).with(:alert)
view.alert
end

test "uses controller lookup context" do
Expand Down

0 comments on commit 238a425

Please sign in to comment.