Skip to content

Latest commit

 

History

History
49 lines (38 loc) · 1.4 KB

best-practices.md

File metadata and controls

49 lines (38 loc) · 1.4 KB

Rails / Best practices

Fail on missing translations in test env

Expectations in specs might be set using I18n.t meaning that the missing translation error texts would be compared successfully. Add a custom handler that raises an error in test environment when keys are missing.

lib/i18n_raise_exception_handler.rb

module I18n
  class RaiseExceptionHandler < ExceptionHandler
    def call(exception, locale, key, options)
      raise exception.to_exception if exception.is_a?(I18n::MissingTranslation)

      super
    end
  end
end

config/environments/test.rb

require "#{Rails.root}/lib/i18n_raise_exception_handler.rb"

Rails.application.configure do
  config.i18n.exception_handler = I18n::RaiseExceptionHandler.new
end

Use lambda literals for consecutive after_commit hooks

There can only be a single after_commit hook defined for a model with the exception of using lambda literals. See also:

BAD

after_create_commit :log_create
after_update_commit :log_update

after_commit :callback, :on => :create
after_commit :callback, :on => :update, :if => :condition

GOOD

after_create_commit -> { foo }
after_update_commit -> { foo }