Action Mailer new DSL merged

Hi all,

Just letting you know we have a new DSL for Action Mailer.

class Notifier < ActionMailer::Base   delivers_from("system@example.com")

  def signup_notification(recipient)     @account = recipient

    attachments['an-image.jp'] = File.read("an-image.jpg")     attachments['terms.pdf'] = {:content => generate_your_pdf_here() }

    mail(:to => recipient.email_address_with_name,          :subject => "New account information")   end end

Notifier.signup_notification(recipient).deliver

You can read all about it at:

Mikel

Nice.

I'm not sure I understand the new API though. You define instance methods but call class methods? Also, what's that magic recipient parameter we never pass in? Why not just define class methods? Eg:

class Notifier < ActionMailer::Base delivers_from("system@example.com")

def self.signup_notification(recipient) @account = recipient

attachments\[&#39;an\-image\.jp&#39;\] = File\.read\(&quot;an\-image\.jpg&quot;\)
attachments\[&#39;terms\.pdf&#39;\] = \{:content =&gt; generate\_your\_pdf\_here\(\) \}

mail\(:to =&gt; recipient\.email\_address\_with\_name,
     :subject =&gt; &quot;New account information&quot;\)

end end

And then do something like

Notifier.signup_notification(user).deliver

Cheers -foca

Nice.

Thanks :slight_smile:

I'm not sure I understand the new API though. You define instance methods but call class methods? Also, what's that magic recipient parameter we never pass in? Why not just define class methods? Eg:

Class methods won't work unfortunately, this is because you can't play with instance variables or call other instance methods (like headers or attachments)

I am not sure what the magic recipient parameter is we never pass in. If you are talking about the old API doing:

     recipients recipient.email_address_with_name

This is replaced with

    mail(:to => recipient.email_address_with_name)

Mikel

Nice work!

Have you seen this: http://gist.github.com/281420

I think he means in your blog post, you have samples like this:

Notifier.signup_notification.deliver

Ie. calling the signup_notification class method with no arguments. And yet what you showed defined in the Notifier class was an instance method expecting a parameter that you've named recipient in your examples:

def signup_notification(recipient)

I was wondering the same thing, where is that recipient argument coming from?

Ahh... thanks, updated the post to be clear :slight_smile:

Mikel

Of course, that was the result of the discussion in campfire we had that hashed out the new DSL that José and I implemented :slight_smile:

One change on that though is that delivers_from is not there, and it is replaced with a defaults hash.

Just letting you know we have a new DSL for Action Mailer.

I think removing the magic #deliver_* and #create_* methods, and returning an actual Mail object directly by an existing method makes a lot of sense.

On the other hand, I think replacing methods like #body, #subject, etc. with a hash is a missed idea, it's harder to write and read, and less clear to grasp, especially when you notice that ActiveRecord API changes in exactly the opposite direction (from :conditions => conditions to #where(conditions), etc.).

Are there any more improvements planned on this or is that the final version?

You don't have to pass body as a hash, you pass the body in through the template in 99% of the cases.

Think of the mail method as being analogous to the respond_to method in Action Controller.

There are more changes coming, not big, but intelligent ones. Watch this space :slight_smile:

BTW, Awesome work Mikel!