Skip to content

Commit

Permalink
Disable namespaced lookup by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Mar 16, 2012
1 parent 74dd56e commit f362451
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
8 changes: 5 additions & 3 deletions lib/responders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module Responders

class Railtie < ::Rails::Railtie
config.responders = ActiveSupport::OrderedOptions.new
config.responders.flash_keys = [ :notice, :alert ]
config.responders.namespace_lookup = false

if config.respond_to?(:app_generators)
config.app_generators.scaffold_controller = :responders_controller
else
Expand All @@ -20,9 +23,8 @@ class Railtie < ::Rails::Railtie
I18n.load_path << File.expand_path('../responders/locales/en.yml', __FILE__)

initializer "responders.flash_responder" do |app|
if app.config.responders.flash_keys
Responders::FlashResponder.flash_keys = app.config.responders.flash_keys
end
Responders::FlashResponder.flash_keys = app.config.responders.flash_keys
Responders::FlashResponder.namespace_lookup = app.config.responders.namespace_lookup
end
end
end
15 changes: 9 additions & 6 deletions lib/responders/flash_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ module Responders
# and :alert.
#
module FlashResponder
mattr_accessor :flash_keys
@@flash_keys = [ :notice, :alert ]
class << self
attr_accessor :flash_keys, :namespace_lookup, :helper
end

mattr_reader :helper
@@helper = Object.new.extend(ActionView::Helpers::TranslationHelper)
self.flash_keys = [ :notice, :alert ]
self.namespace_lookup = false
self.helper = Object.new.extend(ActionView::Helpers::TranslationHelper)

def initialize(controller, resources, options={})
super
Expand Down Expand Up @@ -163,8 +165,9 @@ def mount_i18n_options(status) #:nodoc:
def flash_defaults_by_namespace(status) #:nodoc:
defaults = []
slices = controller.controller_path.split('/')
lookup = Responders::FlashResponder.namespace_lookup

while slices.size > 0
begin
controller_scope = :"flash.#{slices.fill(controller.controller_name, -1).join('.')}.#{controller.action_name}.#{status}"
actions_scope = :"flash.#{slices.fill('actions', -1).join('.')}.#{controller.action_name}.#{status}"

Expand All @@ -175,7 +178,7 @@ def flash_defaults_by_namespace(status) #:nodoc:
defaults << actions_scope

slices.shift
end
end while slices.size > 0 && lookup

defaults << ""
end
Expand Down
8 changes: 8 additions & 0 deletions test/flash_responder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,15 @@ def test_sets_the_flash_message_based_on_namespace_actions
end

def test_fallbacks_to_non_namespaced_controller_flash_message
Responders::FlashResponder.namespace_lookup = true
delete :destroy
assert_equal "Successfully deleted the chosen address at Ocean Avenue", flash[:notice]
ensure
Responders::FlashResponder.namespace_lookup = false
end

def test_does_not_fallbacks_to_non_namespaced_controller_flash_message_if_disabled
delete :destroy
assert_equal nil, flash[:notice]
end
end

0 comments on commit f362451

Please sign in to comment.