Skip to content

Instantly share code, notes, and snippets.

@whalec
Created February 9, 2010 03:07
Show Gist options
  • Save whalec/298880 to your computer and use it in GitHub Desktop.
Save whalec/298880 to your computer and use it in GitHub Desktop.
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