Forum: Ruby on Rails Alias mail method in class Mailer < ActionMailer::Base

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B82dab8ce36c8c55b36c783913f7b951?d=identicon&s=25 Rupesh Jayaprakash (rupi_j)
on 2014-07-16 12:03
Hello,

I have a class in the core application,
--------------------------------------------------
class Mailer < ActionMailer::Base
.
.
 def mail(headers={}, &block)
   headers.merge! 'Auto-Submitted' => 'auto-generated',
            'From' => Setting.mail_from,
            'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
  .
  .
  m
 end
end
--------------------------------------------------
I need to patch this class, (As a plugin),
So that the 'From' is configurable.
Here is the plugin patch I tried and failed to get the expected result.
--------------------------------------------------
module Patches
  module MailerPatch
    def self.included(base) # :nodoc:
      base.send(:include, InstanceMethods)
      base.class_eval do
        unloadable
        alias_method_chain :mail, :patch
      end
    end
  end

  module InstanceMethods
    def mail_with_patch(headers={}, &block)
      m = mail_without_patch(headers, &block)
      m.from = User.current.mail
      m
    end
  end
end
Mailer.send(:include, Patches::MailerPatch)

--------------------------------------------------

Am I doing something wrong here, Please help me.
Thank You.
This topic is locked and can not be replied to.