From 573710246c7fba80a751ff6e09d8bad972ad082b Mon Sep 17 00:00:00 2001 From: Gabriel Gironda Date: Fri, 23 Jan 2015 15:08:21 -0600 Subject: [PATCH] Raise all I18n in development and test by default. * Update documentation to reflect changes. * Add TextHelpers::RaiseExceptionHandler for use with I18n.exception_handler. * Add new initializer and `config.text_helpers` config bucket. --- README.md | 37 +++++++++++++++++++++++++++++++++++++ lib/text_helpers.rb | 27 ++++++++++++++++++++++++++- lib/text_helpers/railtie.rb | 13 +++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0746975..16df99b 100644 --- a/README.md +++ b/README.md @@ -90,3 +90,40 @@ specs by adding `view: true` to the spec metadata. The controller text helpers described above can be accessed in controller specs by adding `controller: true` to your spec metadata. + +## Configuration & Initialization + +### Initialization + +`TextHelpers` performs some setup during your application's initialization. Four +initializers are installed: + +#### `text_helpers.action_view.extend_base` + +This initializer includes the `TextHelpers::Translation` module into +`ActionView::Base` and adds an appropriate `#translation_scope` method. + +#### `text_helpers.action_mailer.extend_base` + +This initializer includes the `TextHelpers::Translation` module into +`ActionMailer::Base` and adds an appropriate `#translation_scope` method. + +#### `text_helpers.action_controller.extend_base` + +This initializer includes the `TextHelpers::Translation` module into +`ActionController::Base` and adds an appropriate `#translation_scope` method. + +#### `text_helpers.setup_exception_handling` + +This initializer configures exception handling so that exceptions are raised +if `config.text_helpers.raise_on_missing_translations` is set to `true`, which +it is by default in the `test` or `development` environments. + +### Configuration + +#### `config.text_helpers.raise_on_missing_translations` + +This configuration value defaults to `true` in `test` or `development` +environments. If set to `false`, your own exception handling can be configured +by setting `config.action_view.raise_on_missing_translations` and +`I18n.exception_handler` as appropriate. diff --git a/lib/text_helpers.rb b/lib/text_helpers.rb index b565d62..7e632ad 100644 --- a/lib/text_helpers.rb +++ b/lib/text_helpers.rb @@ -2,4 +2,29 @@ require "text_helpers/translation" require "text_helpers/railtie" if defined?(Rails) -module TextHelpers; end +module TextHelpers + # RaiseExceptionHandler just raises all exceptions, rather than swallowing + # MissingTranslation ones. It's cribbed almost verbatim from + # http://edgeguides.rubyonrails.org/i18n.html#customize-your-i18n-setup. + class RaiseExceptionHandler < I18n::ExceptionHandler + def call(exception, locale, key, options) + if exception.is_a?(I18n::MissingTranslation) + raise exception.to_exception + else + super + end + end + end + + # Public: Install an instance of TextHelpers::RaiseExceptionHandler as the + # default I18n exception handler. + # + # Returns the handler instance. + def self.install_i18n_exception_handler + if I18n.exception_handler + Rails.logger.warn("Overwriting existing I18n exception handler") + end + + I18n.exception_handler = RaiseExceptionHandler.new + end +end diff --git a/lib/text_helpers/railtie.rb b/lib/text_helpers/railtie.rb index 4021058..6f5507c 100644 --- a/lib/text_helpers/railtie.rb +++ b/lib/text_helpers/railtie.rb @@ -1,5 +1,8 @@ module TextHelpers class Railtie < Rails::Railtie + config.text_helpers = ActiveSupport::OrderedOptions.new + config.text_helpers.raise_on_missing_translations = Rails.env.test? || Rails.env.development? + initializer "text_helpers.action_view.extend_base" do ActionView::Base.class_eval do include TextHelpers::Translation @@ -53,5 +56,15 @@ def translation_scope end end end + + initializer "text_helpers.setup_exception_handling", after: 'after_initialize' do + next unless config.text_helpers.raise_on_missing_translations + + if config.respond_to?(:action_view) + config.action_view.raise_on_missing_translations = true + end + + TextHelpers.install_i18n_exception_handler + end end end