Skip to content

Instantly share code, notes, and snippets.

@aziz
Created August 10, 2010 22:38
Show Gist options
  • Save aziz/518141 to your computer and use it in GitHub Desktop.
Save aziz/518141 to your computer and use it in GitHub Desktop.
class ContactForm
@to = "Jason Seifer <jason@twistedmind.com>"
include ActiveModel::AttributeMethods
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Translation
extend ActiveModel::Callbacks
attr_accessor :name, :email, :message
validates_presence_of :name
validates_presence_of :email
validates_presence_of :message
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
define_model_callbacks :save
def initialize(attributes={})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def read_attribute_for_validation(key)
send(key)
end
def to
self.class.instance_variable_get("@to")
end
def from
"#{name} <#{email}>"
end
def persisted?
false
end
def save
if valid?
Notifier.contact_form(self).deliver
else
return false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment