Skip to content

Commit

Permalink
Raise all I18n in development and test by default.
Browse files Browse the repository at this point in the history
* Update documentation to reflect changes.
* Add TextHelpers::RaiseExceptionHandler for use with
  I18n.exception_handler.
* Add new initializer and `config.text_helpers` config bucket.
  • Loading branch information
gabrielg committed Jan 23, 2015
1 parent 618833e commit 5737102
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
37 changes: 37 additions & 0 deletions README.md
Expand Up @@ -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.
27 changes: 26 additions & 1 deletion lib/text_helpers.rb
Expand Up @@ -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
13 changes: 13 additions & 0 deletions 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
Expand Down Expand Up @@ -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

0 comments on commit 5737102

Please sign in to comment.