Skip to content

Commit

Permalink
Allow passing interpolations to #default_i18n_subject, e.g.:
Browse files Browse the repository at this point in the history
    # config/locales/en.yml
    en:
      user_mailer:
        welcome:
          subject: 'Hello, %{username}'

    # app/mailers/user_mailer.rb
    class UserMailer < ActionMailer::Base
      def welcome(user)
        mail(subject: default_i18n_subject(username: user.name))
      end
    end
  • Loading branch information
exviva committed Jan 24, 2013
1 parent 1b75b94 commit 57bfbc2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
19 changes: 18 additions & 1 deletion actionmailer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
## Rails 4.0.0 (unreleased) ##

* Eager loading made to use relation's in_clause_length instead of host's one.
* Allow passing interpolations to `#default_i18n_subject`, e.g.:

# config/locales/en.yml
en:
user_mailer:
welcome:
subject: 'Hello, %{username}'

# app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
def welcome(user)
mail(subject: default_i18n_subject(username: user.name))
end
end

*Olek Janiszewski*

* Eager loading made to use relation's `in_clause_length` instead of host's one.
Fix #8474

*Boris Staal*
Expand Down
5 changes: 3 additions & 2 deletions actionmailer/lib/action_mailer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -726,9 +726,10 @@ def set_content_type(m, user_content_type, class_default)
# Translates the +subject+ using Rails I18n class under <tt>[mailer_scope, action_name]</tt> scope.
# If it does not find a translation for the +subject+ under the specified scope it will default to a
# humanized version of the <tt>action_name</tt>.
def default_i18n_subject #:nodoc:
# If the subject has interpolations, you can pass them through the +interpolations+ parameter.
def default_i18n_subject(interpolations = {})
mailer_scope = self.class.mailer_name.tr('/', '.')
I18n.t(:subject, scope: [mailer_scope, action_name], default: action_name.humanize)
I18n.t(:subject, interpolations.merge(scope: [mailer_scope, action_name], default: action_name.humanize))
end

def collect_responses(headers) #:nodoc:
Expand Down
6 changes: 6 additions & 0 deletions actionmailer/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ def teardown
assert_equal "New Subject!", email.subject
end

test 'default subject can have interpolations' do
I18n.backend.store_translations('en', base_mailer: {with_subject_interpolations: {subject: 'Will the real %{rapper_or_impersonator} please stand up?'}})
email = BaseMailer.with_subject_interpolations
assert_equal 'Will the real Slim Shady please stand up?', email.subject
end

test "translations are scoped properly" do
I18n.backend.store_translations('en', base_mailer: {email_with_translations: {greet_user: "Hello %{name}!"}})
email = BaseMailer.email_with_translations
Expand Down
4 changes: 4 additions & 0 deletions actionmailer/test/mailers/base_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,8 @@ def with_nil_as_return_value(hash = {})
mail(:template_name => "welcome")
nil
end

def with_subject_interpolations
mail(subject: default_i18n_subject(rapper_or_impersonator: 'Slim Shady'), body: '')
end
end

0 comments on commit 57bfbc2

Please sign in to comment.