Skip to content

Instantly share code, notes, and snippets.

@danteregis
Forked from davithss/gist:1108199
Created July 27, 2011 13:41
Show Gist options
  • Save danteregis/1109375 to your computer and use it in GitHub Desktop.
Save danteregis/1109375 to your computer and use it in GitHub Desktop.
models and action mailer
#sendemail.rb
class Sendemail < ActiveRecord::Base
has_many :newsletters, :dependent => :destroy
validates_presence_of :subject, :body
after_create :deliver_notification
protected
def deliver_notification
SendemailMailer.notification(self).deliver
end
end
#newsletter.rb
class Newsletter < ActiveRecord::Base
belongs_to :sendemail
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates_presence_of :name, :email
#scope :pending, where(:open => true)
#scope :joined, where(:open => false)
after_create :deliver_notification
protected
def deliver_notification
NewsletterMailer.deliver_notification(self)
end
end
#sendemail_mailer.rb
class SendemailMailer < ActionMailer::Base
default :from => "contato@lifetic.com.br"
# pelo menos é assim no Rails 2.3.x
def notification(newsletter)
recipients newsletter.email
subject "NEWSLETTER DO POKEMON"
body :newsletter => newsletter
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment