Skip to content

Commit

Permalink
Add status_keys and default to :notice and :alert.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Dec 22, 2009
1 parent 2269b23 commit 10415ad
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
20 changes: 13 additions & 7 deletions lib/responders/flash_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ module Responders
# flash.cars.create.status
# flash.actions.create.status
#
# The statuses can be :success (when the object can be created, updated
# or destroyed with success) or :failure (when the objecy cannot be created
# or updated).
# The statuses by default are :notice (when the object can be created, updated
# or destroyed with success) and :alert (when the objecy cannot be created
# or updated). Although they can be configured:
#
# The resource_name given is available as interpolation option, this means you can set:
# Responders::FlashResponder.status_keys = [ :sucess, :failure ]
#
# On I18n, the resource_name given is available as interpolation option,
# this means you can set:
#
# flash:
# actions:
Expand Down Expand Up @@ -51,6 +54,9 @@ module Responders
# flash.actions.create.status
#
module FlashResponder
mattr_accessor :status_keys
@@status_keys = [ :notice, :alert ]

def initialize(controller, resources, options={})
super
@flash = options.delete(:flash)
Expand All @@ -60,8 +66,8 @@ def navigation_behavior(error)
super

unless get? || @flash == false
status = has_errors? ? :failure : :success
return if controller.send(:flash)[status].present?
status = Responders::FlashResponder.status_keys.send(has_errors? ? :last : :first)
return if controller.flash[status].present?

resource_name = if resource.class.respond_to?(:human_name)
resource.class.human_name
Expand All @@ -80,7 +86,7 @@ def navigation_behavior(error)
end

message = ::I18n.t options[:default].shift, options
controller.send(:flash)[status] = message unless message.blank?
controller.flash[status] = message unless message.blank?
end
end

Expand Down
8 changes: 5 additions & 3 deletions test/flash_responder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class FlashResponderTest < ActionController::TestCase
tests AddressesController

def setup
Responders::FlashResponder.status_keys = [ :success, :failure ]
@controller.stubs(:polymorphic_url).returns("/")
end

Expand Down Expand Up @@ -96,21 +97,22 @@ class NamespacedFlashResponderTest < ActionController::TestCase
tests Admin::AddressesController

def setup
Responders::FlashResponder.status_keys = [ :notice, :alert ]
@controller.stubs(:polymorphic_url).returns("/")
end

def test_sets_the_flash_message_based_on_the_current_controller
put :update
assert_equal "Admin updated address with success", flash[:success]
assert_equal "Admin updated address with success", flash[:notice]
end

def test_sets_the_flash_message_based_on_namespace_actions
post :create
assert_equal "Admin created address with success", flash[:success]
assert_equal "Admin created address with success", flash[:notice]
end

def test_fallbacks_to_non_namespaced_controller_flash_message
delete :destroy
assert_equal "Successfully deleted the address at Ocean Avenue", flash[:success]
assert_equal "Successfully deleted the chosen address at Ocean Avenue", flash[:notice]
end
end
5 changes: 3 additions & 2 deletions test/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ en:
failure: "Oh no! We could not update your address!"
destroy:
success: "Successfully deleted the address at {{reference}}"
notice: "Successfully deleted the chosen address at {{reference}}"
admin:
actions:
create:
success: "Admin created address with success"
notice: "Admin created address with success"
addresses:
update:
success: "Admin updated address with success"
notice: "Admin updated address with success"

4 changes: 2 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
ENV["RAILS_ENV"] = "test"
RAILS_ROOT = "anywhere"

require File.expand_path(File.dirname(__FILE__) + "/../../../rails/vendor/gems/environment")
require File.expand_path(File.dirname(__FILE__) + "/../../rails/vendor/gems/environment")
require 'active_support'
require 'action_controller'
require 'action_controller/testing/test_case'
require 'action_controller/test_case'

class ApplicationController < ActionController::Base
respond_to :html, :xml
Expand Down

0 comments on commit 10415ad

Please sign in to comment.