Skip to content
This repository
Browse code

Merge pull request #9046 from exviva/actionmailer_subject_interpolations

Allow passing interpolations to #default_i18n_subject in mailers
  • Loading branch information...
commit 01341e3e432f0602891fe226ed1f2d079f590327 2 parents 9826823 + 57bfbc2
Carlos Antonio da Silva authored January 25, 2013
19  actionmailer/CHANGELOG.md
Source Rendered
... ...
@@ -1,6 +1,23 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
3  
-*   Eager loading made to use relation's in_clause_length instead of host's one.
  3
+*   Allow passing interpolations to `#default_i18n_subject`, e.g.:
  4
+
  5
+        # config/locales/en.yml
  6
+        en:
  7
+          user_mailer:
  8
+            welcome:
  9
+              subject: 'Hello, %{username}'
  10
+
  11
+        # app/mailers/user_mailer.rb
  12
+        class UserMailer < ActionMailer::Base
  13
+          def welcome(user)
  14
+            mail(subject: default_i18n_subject(username: user.name))
  15
+          end
  16
+        end
  17
+
  18
+    *Olek Janiszewski*
  19
+
  20
+*   Eager loading made to use relation's `in_clause_length` instead of host's one.
4 21
     Fix #8474
5 22
 
6 23
     *Boris Staal*
5  actionmailer/lib/action_mailer/base.rb
@@ -726,9 +726,10 @@ def set_content_type(m, user_content_type, class_default)
726 726
     # Translates the +subject+ using Rails I18n class under <tt>[mailer_scope, action_name]</tt> scope.
727 727
     # If it does not find a translation for the +subject+ under the specified scope it will default to a
728 728
     # humanized version of the <tt>action_name</tt>.
729  
-    def default_i18n_subject #:nodoc:
  729
+    # If the subject has interpolations, you can pass them through the +interpolations+ parameter.
  730
+    def default_i18n_subject(interpolations = {})
730 731
       mailer_scope = self.class.mailer_name.tr('/', '.')
731  
-      I18n.t(:subject, scope: [mailer_scope, action_name], default: action_name.humanize)
  732
+      I18n.t(:subject, interpolations.merge(scope: [mailer_scope, action_name], default: action_name.humanize))
732 733
     end
733 734
 
734 735
     def collect_responses(headers) #:nodoc:
6  actionmailer/test/base_test.rb
@@ -209,6 +209,12 @@ def teardown
209 209
     assert_equal "New Subject!", email.subject
210 210
   end
211 211
 
  212
+  test 'default subject can have interpolations' do
  213
+    I18n.backend.store_translations('en', base_mailer: {with_subject_interpolations: {subject: 'Will the real %{rapper_or_impersonator} please stand up?'}})
  214
+    email = BaseMailer.with_subject_interpolations
  215
+    assert_equal 'Will the real Slim Shady please stand up?', email.subject
  216
+  end
  217
+
212 218
   test "translations are scoped properly" do
213 219
     I18n.backend.store_translations('en', base_mailer: {email_with_translations: {greet_user: "Hello %{name}!"}})
214 220
     email = BaseMailer.email_with_translations
4  actionmailer/test/mailers/base_mailer.rb
@@ -123,4 +123,8 @@ def with_nil_as_return_value(hash = {})
123 123
     mail(:template_name => "welcome")
124 124
     nil
125 125
   end
  126
+
  127
+  def with_subject_interpolations
  128
+    mail(subject: default_i18n_subject(rapper_or_impersonator: 'Slim Shady'), body: '')
  129
+  end
126 130
 end

0 notes on commit 01341e3

Please sign in to comment.
Something went wrong with that request. Please try again.