1

This seems like a monkey patch. How can I improve it?

Trying to override the deliver_now instance method from the ActionMailer::MessageDelivery class. The below code works.

However, is there a way I can achieve this more elegantly say through class inheritance or some other way? The end code is used in a custom ActionMailer mailer.

module MyModule
  class Ses 

    ActionMailer::MessageDelivery.class_eval do
      def deliver_now!
        puts self.to
        puts self.subject
      end
    end
    ...
  end
end

Note: I've seen these similar questions one & two but they do not address this issue.

Community
  • 1
  • 1
csi
  • 9,018
  • 8
  • 61
  • 81
  • There are other ways to set the email header fields (to/from/subject). If this is the only reason you're overriding the method, I would look at those other ways to do it. I don't have a list handy, but you can set them on each method, at the class level, and even in the config files. – Sunil D. Oct 12 '15 at 17:23

1 Answers1

3

You might look into Ruby's refinements

An example refinement:

module MyMessageRefinement
  refine(ActionMailer::MessageDelivery) do
    def deliver_now!
      puts self.to
      puts self.subject
    end
  end    
end

Then in your class:

module MyModule
  class Ses 
    using MyMessageRefinement
  end
end
yez
  • 2,368
  • 9
  • 15