diff options
Diffstat (limited to 'actionmailer/lib/action_mailer/parameterized.rb')
-rw-r--r-- | actionmailer/lib/action_mailer/parameterized.rb | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/actionmailer/lib/action_mailer/parameterized.rb b/actionmailer/lib/action_mailer/parameterized.rb index a0a5db7427..3acacc1f14 100644 --- a/actionmailer/lib/action_mailer/parameterized.rb +++ b/actionmailer/lib/action_mailer/parameterized.rb @@ -71,7 +71,7 @@ module ActionMailer # @project = params[:project] # @summarizer = ProjectInvitationSummarizer.new(@project.bucket) # - # mail subject: "#{@inviter.name.familiar} added you to a project in Basecamp (#{@account.name})" + # mail subject: "#{@inviter.name.familiar} added you to a project in Basecamp (#{@account.name})" # end # # def bulk_project_invitation @@ -82,9 +82,6 @@ module ActionMailer # end # # InvitationsMailer.with(inviter: person_a, invitee: person_b).account_invitation.deliver_later - # - # That's a big improvement! It's also fully backwards compatible. So you can start to gradually transition - # mailers that stand to benefit the most from parameterization one by one and leave the others behind. module Parameterized extend ActiveSupport::Concern @@ -92,33 +89,46 @@ module ActionMailer attr_accessor :params end - class_methods do + module ClassMethods + # Provide the parameters to the mailer in order to use them in the instance methods and callbacks. + # + # InvitationsMailer.with(inviter: person_a, invitee: person_b).account_invitation.deliver_later + # + # See Parameterized documentation for full example. def with(params) ActionMailer::Parameterized::Mailer.new(self, params) end end - class Mailer + class Mailer # :nodoc: def initialize(mailer, params) @mailer, @params = mailer, params end - def method_missing(method_name, *args) - if @mailer.action_methods.include?(method_name.to_s) - ActionMailer::Parameterized::MessageDelivery.new(@mailer, method_name, *args).tap { |pmd| pmd.params = @params } - else - super + private + def method_missing(method_name, *args) + if @mailer.action_methods.include?(method_name.to_s) + ActionMailer::Parameterized::MessageDelivery.new(@mailer, method_name, @params, *args) + else + super + end + end + + def respond_to_missing?(method, include_all = false) + @mailer.respond_to?(method, include_all) end - end end - class MessageDelivery < ActionMailer::MessageDelivery - attr_accessor :params + class MessageDelivery < ActionMailer::MessageDelivery # :nodoc: + def initialize(mailer_class, action, params, *args) + super(mailer_class, action, *args) + @params = params + end private def processed_mailer @processed_mailer ||= @mailer_class.new.tap do |mailer| - mailer.params = params + mailer.params = @params mailer.process @action, *@args end end @@ -134,7 +144,7 @@ module ActionMailer end class DeliveryJob < ActionMailer::DeliveryJob # :nodoc: - def perform(mailer, mail_method, delivery_method, params, *args) #:nodoc: + def perform(mailer, mail_method, delivery_method, params, *args) mailer.constantize.with(params).public_send(mail_method, *args).send(delivery_method) end end |