Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support to :notice and :alert to respond_with.

  • Loading branch information...
commit 450015bda1c3431fd1b3f4f777911bab62faec2b 1 parent 075669f
@josevalim josevalim authored
View
3  CHANGELOG.rdoc
@@ -1,6 +1,7 @@
== 0.2
-* Added Responders::FlashResponder.status_keys and default to [ :notice, :alert ]
+* Added Responders::FlashResponder.flash_keys and default to [ :notice, :alert ]
+* Added support to respond_with(@resource, :notice => "Yes!", :alert => "No!")
== 0.1
View
24 README.rdoc
@@ -7,14 +7,14 @@ A set of responders to dry up your Rails 3 app:
does not contain errors, it will automatically set the flash message to
"Post was successfully created" as long as you configure your I18n file:
- flash:
- actions:
- create:
- success: "{resource_name} was successfully created"
- update:
- success: "{resource_name} was successfully updated"
- destroy:
- failure: "{resource_name} could not be destroyed"
+ flash:
+ actions:
+ create:
+ notice: "{resource_name} was successfully created"
+ update:
+ notice: "{resource_name} was successfully updated"
+ destroy:
+ alert: "{resource_name} could not be destroyed"
In case the resource contains errors, you should use the failure key on I18n. This is
useful to dry up flash messages from your controllers. If you need a specific message
@@ -23,10 +23,12 @@ A set of responders to dry up your Rails 3 app:
flash:
posts:
create:
- success: "Your post was created and will be published soon"
+ notice: "Your post was created and will be published soon"
+
+ This responder is activated in all non get requests. By default it will use the keys
+ :notice and :alert, but they can be changed as well:
- This responder is activated in all non get requests and searches for the key :success
- if the resource does not have errors and :failure otherwise.
+ Responders::FlashResponder.flash_keys = [ :success, :failure ]
* HttpCacheResponder - Automatically adds Last-Modified headers to API requests. This
allows clients to easily query the server if a resource changed and if the client tries
View
81 lib/responders/flash_responder.rb
@@ -13,9 +13,7 @@ module Responders
#
# 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:
- #
- # Responders::FlashResponder.status_keys = [ :sucess, :failure ]
+ # or updated).
#
# On I18n, the resource_name given is available as interpolation option,
# this means you can set:
@@ -23,7 +21,7 @@ module Responders
# flash:
# actions:
# create:
- # success: "Hooray! {{resource_name}} was successfully created!"
+ # notice: "Hooray! {{resource_name}} was successfully created!"
#
# But sometimes, flash messages are not that simple. Going back
# to cars example, you might want to say the brand of the car when it's
@@ -32,7 +30,7 @@ module Responders
# flash:
# cars:
# update:
- # success: "Hooray! You just tuned your {{car_brand}}!"
+ # notice: "Hooray! You just tuned your {{car_brand}}!"
#
# Since :car_name is not available for interpolation by default, you have
# to overwrite interpolation_options in your controller.
@@ -53,38 +51,55 @@ module Responders
# flash.cars.create.status
# flash.actions.create.status
#
+ # == Options
+ #
+ # FlashResponder also accepts some options through respond_with API.
+ #
+ # * :flash - When set to false, no flash message is set.
+ #
+ # respond_with(@user, :flash => true)
+ #
+ # * :notice - Supply the message to be set if the record has no errors.
+ # * :alert - Supply the message to be set if the record has errors.
+ #
+ # respond_with(@user, :notice => "Hooray! Welcome!", :alert => "Woot! You failed.")
+ #
+ # == Configure status keys
+ #
+ # As said previously, FlashResponder by default use :notice and :alert
+ # keys. You can change that by setting the status_keys:
+ #
+ # Responders::FlashResponder.flash_keys = [ :success, :failure ]
+ #
+ # However, the options :notice and :alert to respond_with are kept :notice
+ # and :alert.
+ #
module FlashResponder
- mattr_accessor :status_keys
- @@status_keys = [ :notice, :alert ]
+ mattr_accessor :flash_keys
+ @@flash_keys = [ :notice, :alert ]
def initialize(controller, resources, options={})
super
- @flash = options.delete(:flash)
+ @flash = options.delete(:flash)
+ @notice = options.delete(:notice)
+ @alert = options.delete(:alert)
end
def navigation_behavior(error)
super
unless get? || @flash == false
- 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
+ if has_errors?
+ controller.flash[:alert] ||= @alert if @alert
+ status = Responders::FlashResponder.flash_keys.last
else
- resource.class.name.underscore.humanize
+ controller.flash[:notice] ||= @notice if @notice
+ status = Responders::FlashResponder.flash_keys.first
end
- options = {
- :default => flash_defaults_by_namespace(status),
- :resource_name => resource_name,
- :resource_sym => resource_name.downcase
- }
-
- if controller.respond_to?(:interpolation_options, true)
- options.merge!(controller.send(:interpolation_options))
- end
+ return if controller.flash[status].present?
+ options = mount_i18n_options(status)
message = ::I18n.t options[:default].shift, options
controller.flash[status] = message unless message.blank?
end
@@ -92,7 +107,25 @@ def navigation_behavior(error)
protected
- def flash_defaults_by_namespace(status)
+ def mount_i18n_options(status) #:nodoc:
+ resource_name = if resource.class.respond_to?(:human_name)
+ resource.class.human_name
+ else
+ resource.class.name.underscore.humanize
+ end
+
+ options = {
+ :default => flash_defaults_by_namespace(status),
+ :resource_name => resource_name,
+ :resource_sym => resource_name.downcase
+ }
+
+ if controller.respond_to?(:interpolation_options, true)
+ options.merge!(controller.send(:interpolation_options))
+ end
+ end
+
+ def flash_defaults_by_namespace(status) #:nodoc:
defaults = []
slices = controller.controller_path.split('/')
View
20 test/flash_responder_test.rb
@@ -27,6 +27,10 @@ def action
alias :update :action
alias :destroy :action
+ def another
+ respond_with(@resource, :notice => "Yes, notice this!", :alert => "Warning, warning!")
+ end
+
protected
def interpolation_options
@@ -48,7 +52,7 @@ class FlashResponderTest < ActionController::TestCase
tests AddressesController
def setup
- Responders::FlashResponder.status_keys = [ :success, :failure ]
+ Responders::FlashResponder.flash_keys = [ :success, :failure ]
@controller.stubs(:polymorphic_url).returns("/")
end
@@ -91,13 +95,25 @@ def test_does_not_overwrite_the_flash_if_already_set
post :create, :set_flash => true
assert_equal "Flash is set", flash[:success]
end
+
+ def test_sets_message_based_on_notice_key
+ Responders::FlashResponder.flash_keys = [ :notice, :alert ]
+ post :another
+ assert_equal "Yes, notice this!", flash[:notice]
+ end
+
+ def test_sets_message_based_on_alert_key
+ Responders::FlashResponder.flash_keys = [ :notice, :alert ]
+ post :another, :fail => true
+ assert_equal "Warning, warning!", flash[:alert]
+ end
end
class NamespacedFlashResponderTest < ActionController::TestCase
tests Admin::AddressesController
def setup
- Responders::FlashResponder.status_keys = [ :notice, :alert ]
+ Responders::FlashResponder.flash_keys = [ :notice, :alert ]
@controller.stubs(:polymorphic_url).returns("/")
end
Please sign in to comment.
Something went wrong with that request. Please try again.