0

Recently I saw a quite odd code in Ruby on Rails http://railscasts.com/episodes/206-action-mailer-in-rails-3

   class UserMailer < ActionMailer::Base
      default :from => "[email protected]"

      def registration_confirmation(user)
        @user = user
        attachments["rails.png"] = File.read("#{Rails.root}/public/images/rails.png")
        mail(:to => "#{user.name} <#{user.email}>", :subject => "Registered")
      end
    end

    #controller
    UserMailer.registration_confirmation(@user).deliver

How is it possible? registration_confirmation is not a class method ( or kind of like a static one in C#), it's an instance method!

1 Answer 1

2

It's a little tricky.

Because it derives from AbstractController, the methods defined are added as public instance methods. (http://api.rubyonrails.org/classes/ActionMailer/Base.html)

You never instantiate your mailer class. Rather, you just call the method you defined on the class itself.

If you examine AbstractController::Base, you can see where the class is abstracted and the action methods are turned into public instance methods.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.