Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Before filter in action mailer Rails 3

What I need:

Something similar to before_filter in ActionMailer in Rails 3.

Problem:

I am working on Rails 3 and want to have a before_filter in ActionMailer. Checked the actionmailer api and learned about before_action and after_action callbacks. When implemented it gives the error:

NoMethodError: undefined method `before_action' for Notifier:Class

Later learned that there is no before action call for Rails 3 from this post

Isnt there any hook or gem so that we can have something similar like before_filter in Rails 3.

Please help. Many Thanks!!

like image 986
Swati Aggarwal Avatar asked Jul 10 '13 08:07

Swati Aggarwal


1 Answers

It can be achieved by including AbstractController::Callbacks. This mimics the change to Rails 4 which apart from comments and tests, just included Callbacks.

class MyMailer < ActionMailer::Base
  include AbstractController::Callbacks

  after_filter :check_email

  def some_mail_action(user)
    @user = user
    ...
  end

  private
   def check_email
     if @user.email.nil?
     message.perform_deliveries = false
    end
    true
   end

end

Reference - How to add a before_filter in UserMailer which checks if it is OK to mail a user?

like image 109
Swati Aggarwal Avatar answered Oct 11 '22 22:10

Swati Aggarwal