From eae7c6dd6b60b8018a9c6e20044417f06dbb0e3d Mon Sep 17 00:00:00 2001 From: Arjan van der Gaag Date: Tue, 15 May 2012 19:36:42 +0200 Subject: [PATCH] Added note about preloading issues to Readme Draper's initialization may cause issues with application configuration being ignored (see #202). Note the problem and possible workaround in the docs. --- Readme.markdown | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Readme.markdown b/Readme.markdown index 0209dd2..83cecb5 100644 --- a/Readme.markdown +++ b/Readme.markdown @@ -357,6 +357,26 @@ Now when you call the association it will use a decorator. <%= @article.author.fancy_name %> ``` +### A note on Rails configuration + +Draper loads your application's decorators when Rails start. This may lead to an issue with configuring I18n, whereby the settings you provide in `./config/application.rb` are ignored. This happens when you use the `I18n` constant at the class-level in one of the models that have a decorator, as in the following example: + +```ruby +# app/models/user.rb +class User < ActiveRecord::Base + validates :email, presence: { message: I18n.t('invalid_email') } +end + +# app/decorators/user_decorator.rb +class UserDecorator < ApplicationDecorator + decorates :user +end +``` + +Note how the `validates` line is executed when the `User` class is loaded, triggering the initialization of the I18n framework _before_ Rails had a chance to apply its configuration. + +Using `I18n` directly in your model definition **is an antipattern**. The preferred solution would be to not override the `message` option in your `validates` macro, but provide the `activerecord.errors.models.attributes.user.email.presence` key in your translation files. + ## Contributing 1. Fork it.