Permalink
Browse files

Routing helpers available as ActionMailer instance methods (previousl…

…y they were only available on views).

Minor refactorings
  • Loading branch information...
1 parent a20c2e0 commit bebd4f3f4f7695055294f7679c1c6b6074a08d35 @raul committed Feb 14, 2009
Showing with 32 additions and 55 deletions.
  1. +11 −29 lib/translate_routes.rb
  2. +20 −26 lib/translate_routes_test_helper.rb
  3. +1 −0 test/translate_routes_test.rb
View
40 lib/translate_routes.rb
@@ -118,7 +118,7 @@ def #{new_helper_name}(*args)
end
DEF_NEW_HELPER
- [ActionController::Base, ActionView::Base].each { |d| d.module_eval(def_new_helper) }
+ [ActionController::Base, ActionView::Base, ActionMailer::Base].each { |d| d.module_eval(def_new_helper) }
ActionController::Routing::Routes.named_routes.helpers << new_helper_name.to_sym
end
end
@@ -199,39 +199,21 @@ def self.translate_route(route, route_name = nil)
end
end
-# Add set_locale_from_url and locale_suffix to controllers
-module ActionController
-
- class Base
-
- private
-
- def set_locale_from_url
- I18n.locale = params[ActionController::Routing::Translator.locale_param_key]
- default_url_options({ActionController::Routing::Translator => I18n.locale })
- end
-
- def locale_suffix(locale)
- eval ActionController::Routing::Translator.locale_suffix_code
- end
-
- end
-
+# Add set_locale_from_url to controllers
+ActionController::Base.class_eval do
+ private
+ def set_locale_from_url
+ I18n.locale = params[ActionController::Routing::Translator.locale_param_key]
+ default_url_options({ActionController::Routing::Translator => I18n.locale })
+ end
end
-# Add locale_suffix to views
-module ActionView
-
- class Base
-
+# Add locale_suffix to controllers, views and mailers
+[ActionController::Base, ActionView::Base, ActionMailer::Base].map do |klass|
+ klass.class_eval do
private
-
def locale_suffix(locale)
eval ActionController::Routing::Translator.locale_suffix_code
end
-
end
-
end
-
-
View
46 lib/translate_routes_test_helper.rb
@@ -1,39 +1,33 @@
-require 'test_help'
-
# Author: Raul Murciano [http://raul.murciano.net] for Domestika [http://domestika.org]
# Copyright (c) 2007, Released under the MIT license (see MIT-LICENSE)
-# Include default lang on your test requests (test requests doesn't support default_url_options):
-module ActionController
- module TestProcess
- unless method_defined?(:process_without_default_language)
- def process_with_default_language(action, parameters = nil, session = nil, flash = nil)
- lang_pair = {:locale, I18n.default_locale.to_s}
- parameters = lang_pair.merge(parameters) rescue lang_pair
- process_without_default_language(action, parameters, session, flash)
- end
+require 'test_help'
- alias :process_without_default_language :process
- alias :process :process_with_default_language
+# Include default lang on your test requests (test requests doesn't support default_url_options):
+ActionController::TestProcess.class_eval do
+ unless method_defined?(:process_without_default_language)
+ def process_with_default_language(action, parameters = nil, session = nil, flash = nil)
+ lang_pair = {:locale, I18n.default_locale.to_s}
+ parameters = lang_pair.merge(parameters) rescue lang_pair
+ process_without_default_language(action, parameters, session, flash)
end
+
+ alias :process_without_default_language :process
+ alias :process :process_with_default_language
end
end
# Add untranslated helper for named routes to integration tests
-module ActionController
- module Integration
- class Session
- ['path', 'url'].each do |suffix|
- ActionController::Routing::Translator.original_names.each do |old_name|
- new_helper_name = "#{old_name}_#{suffix}"
- def_new_helper = <<-DEF_NEW_HELPER
- def #{new_helper_name}(*args)
- send("#{old_name}_#{ActionController::Routing::Translator.locale_suffix(I18n.default_locale)}_#{suffix}", *args)
- end
- DEF_NEW_HELPER
- eval def_new_helper
+ActionController::Integration::Session.class_eval do
+ ['path', 'url'].each do |suffix|
+ ActionController::Routing::Translator.original_names.each do |old_name|
+ new_helper_name = "#{old_name}_#{suffix}"
+ def_new_helper = <<-DEF_NEW_HELPER
+ def #{new_helper_name}(*args)
+ send("#{old_name}_#{ActionController::Routing::Translator.locale_suffix(I18n.default_locale)}_#{suffix}", *args)
end
- end
+ DEF_NEW_HELPER
+ eval def_new_helper
end
end
end
View
1 test/translate_routes_test.rb
@@ -3,6 +3,7 @@
require 'action_controller'
require 'action_controller/test_process'
require 'active_support'
+require 'action_mailer'
plugin_root = File.join(File.dirname(__FILE__), '..')
require "#{plugin_root}/lib/translate_routes"

0 comments on commit bebd4f3

Please sign in to comment.