Created
February 9, 2010 03:07
-
-
Save whalec/298880 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmailForm | |
EMAIL_REGEX = /\A[\w\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)\z/i | |
class Errors < Array | |
def on(method) | |
unless blank? | |
error = find{|error| error[0].to_sym == method.to_sym} | |
return error[1] unless error.blank? | |
end | |
end | |
end | |
attr_accessor :name, :email, :message, :card_number | |
attr_reader :errors | |
def initialize(params={}) | |
@name = params[:name] | |
@email = params[:email] | |
@message = params[:message] | |
@card_number = params[:card_number] || "" | |
@errors = Errors.new | |
end | |
def valid? | |
@errors.clear | |
@errors << ["name", "Name needs to be filled in"] if @name.blank? | |
@errors << ["email", "Email needs to be filled in"] if @email.blank? | |
unless (EMAIL_REGEX =~ @email) | |
@errors << ["email", "That doesn't appear to be a valid email address"] | |
end | |
@errors << ["message", "We need some details on the issues you're having"] if @message.blank? | |
@errors.length == 0 | |
end | |
def deliver | |
if valid? | |
Admin::Notifier.deliver_help_request(self) | |
return true | |
else | |
return false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment