Skip to content

Instantly share code, notes, and snippets.

@balinterdi
Created September 7, 2009 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balinterdi/182289 to your computer and use it in GitHub Desktop.
Save balinterdi/182289 to your computer and use it in GitHub Desktop.
module FakeActiveRecord
class Errors < Hash
alias_method :count, :size
def full_messages
map do |key, message|
"#{message}"
end
end
end
end
class Invitation < ActiveRecord::Base
attr_reader :user_email, :invited_email, :message, :errors
EMAIL_FORMAT = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
def initialize(params)
@errors = FakeActiveRecord::Errors.new
%w[user_email invited_email message].each do |ivar|
instance_variable_set("@#{ivar}", params[ivar.to_sym]) unless params.nil?
end
end
def save
unless @user_email.match(EMAIL_FORMAT)
@errors[:user_email] = "Your email is invalid"
end
unless @invited_email.match(EMAIL_FORMAT)
@errors[:invited_email] = "Your friend's email is invalid"
end
if @errors.empty?
NetworkingMailer.deliver_invitation(self)
self
else
false
end
end
def new_record?
true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment