Created
October 23, 2013 06:39
-
-
Save AndyObtiva/7113599 to your computer and use it in GitHub Desktop.
Project::BasicInfo
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 Project::BasicInfo < Project | |
validates :business_campaign_name, :presence => true, :length => {:maximum => 255} | |
validates :money_to_raise_min, :presence => true, :numericality => {:greater_than => 0} | |
validates :money_to_raise_max, :presence => true, :numericality => {:greater_than => 0} | |
#add validation that money_to_raise_min is greater than 0 | |
validates :deadline, :presence => true | |
validates :name, :presence => true, :length => {:maximum => 255} | |
validates :company_name, :presence => true, :length => {:maximum => 255} | |
validates :position_role, :presence => true, :length => {:maximum => 255} | |
validates :email, :presence => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i | |
validates :phone, :presence => true | |
validate :phone_well_formatted | |
validate :min_max_valid | |
validate :deadline_in_the_future | |
validates :offering_type, :presence => true | |
#before_validation :parse_money_fields | |
after_initialize :initialize_from_user | |
def money_to_raise_min=(value) | |
super(parse_money(value)) if value | |
end | |
def money_to_raise_max=(value) | |
super(parse_money(value)) if value | |
end | |
private | |
def initialize_from_user | |
self.email = user.email if user.present? && self.email.blank? | |
self.name = user.name if user.present? && self.name.blank? | |
end | |
def phone_well_formatted | |
unless Phoner::Phone.parse(phone).to_s.length == 12 | |
errors[:phone] << "is invalid" | |
end | |
rescue | |
errors[:phone] << "is invalid" | |
end | |
def min_max_valid | |
unless money_to_raise_max.to_i >= money_to_raise_min.to_i | |
errors[:money_to_raise_max] << "The maximum amount to raise must be greater than or equal to minimum amount." | |
end | |
end | |
def deadline_in_the_future | |
unless deadline && deadline >= Date.today.at_beginning_of_month | |
errors[:deadline] << "You must pick a fundraising completion date that is in the future." | |
end | |
end | |
def parse_money(amount) | |
amount_in_cents = Money.parse(amount).cents | |
amount_in_cents/100 | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment