Suppose I have a mail program that sends different emails, but it is expected that they will be called with the same parameters. I want to process these parameters for all actions of the mail program. Thus, a before_action call that will read the parameters sent to the mailer method
/mailers/my_mailer.rb class MyMailer < ApplicationMailer before_filter do |c|
Then in my controller / service I am somewhere
MyMailer.actionx(*mailer_params).deliver_now
How can I access the same_param argument same_param inside the before_action block?
EDIT:
I want refactoring from
/mailers/my_mailer.rb class MyMailer < ApplicationMailer def action1(same_param) @to = same_params.recipient @from = same_params.initiator @context = same_params.context method_only_specific_to_action1 end def action2(same_param) @to = same_params.recipient @from = same_params.initiator @context = same_params.context method_only_specific_to_action2 end def actionx ... end end
And this refactoring
/mailers/my_mailer.rb class MyMailer < ApplicationMailer def action1(same_param) prepare_mail(same_params) method_only_specific_to_action1 end def action2(same_param) prepare_mail(same_params) method_only_specific_to_action2 end def actionx ... end private def prepare_mail(same_params) @to = same_params.recipient @from = same_params.initiator @context = same_params.context end end
Feels non-optimal ( prepare_mail(same_params) duplicated in every action)
Therefore, what was proposed above
source share