Created
January 8, 2012 18:06
-
-
Save Ravenna/1579150 to your computer and use it in GitHub Desktop.
Approval.rb
This file contains 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 Approval < ActiveRecord::Base | |
class ApproverEmailValidator < ActiveModel::EachValidator | |
def validate_each(approval, attribute, value) | |
approval.errors[attribute] << "must be a valid e-mail address in our system" unless is_valid_email?(value) | |
end | |
protected | |
def is_valid_email?(address) | |
User.find_by_email(address) | |
end | |
end # End Approver Validator | |
class EmailDomainValidator < ActiveModel::EachValidator | |
def email_domain_is?(domain) | |
unless /ravennainteractive.com$/ =~ email(domain) | |
errors.add(:email, "You must Use an Eddie Bauer email address") | |
end | |
end | |
end #End Email Domain Validator | |
belongs_to :recommendation | |
attr_accessible :approval, :email, :user_id | |
validates :email, :email_domain | |
validates :next_approver_email, :approver_email => { :if => :recently_approved? } | |
before_save :create_next_approval | |
after_create :approval_notification | |
attr_accessor :next_approver_email | |
def recently_approved? | |
self.approved_changed? && self.approved? | |
end | |
def create_next_approval | |
next_approval = self.recommendation.approvals.build(:email => self.next_approver_email, :user_id => User.find_by_email(next_approver_email)) | |
next_approval.save if next_approver_email.present? && recently_approved? | |
end | |
def email_domain_is? | |
unless /ravennainteractive.com$/ =~ email | |
errors.add(:email, "You must Use an Eddie Bauer email address") | |
end | |
end | |
private | |
def approval_notification | |
ApprovalMailer.needs_approval(self).deliver | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment