Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 21st, 2012  |  syntax: None  |  size: 3.50 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class Booking < ActiveRecord::Base
  2.  
  3.   belongs_to :property
  4.   belongs_to :booking_status
  5.   has_one :guest_comment
  6.  
  7.   validates_numericality_of :rental_charge, :damages_deposit, :linen_charge, :cleaning_charge,:baby_equipment_charge, :total_amount_due, :deposit_requested, :deposit_paid,:balance_due
  8.  
  9.   validates_presence_of :property_id, :booking_status_id, :firstname, :surname, :email, :address, :telephone, :arrival_date, :departure_date, :number_of_adults, :number_of_children, :number_of_babies, :total_in_party
  10.  
  11.   validates_numericality_of :number_of_adults, :number_of_children, :number_of_babies, :total_in_party
  12.  
  13.   validates_inclusion_of :number_of_adults, :in => 1..20, :message => "values of 1 to 20 allowed"
  14.   validates_inclusion_of :total_in_party, :in => 1..20, :message => "values of 1 to 20 allowed"
  15.  
  16.   validate :arrival_must_be_before_departure
  17.   validate :check_total_in_the_party_adds_up  
  18.   validate :arrival_must_be_on_a_saturday
  19.  
  20.  
  21.   def arrival_must_be_before_departure
  22.     # when we've got an arrival and departure date make sure we arrive before we depart
  23.     errors.add(:arrival_date, "You must arrive before you depart") if !arrival_date.blank? && !departure_date.blank? and arrival_date > departure_date
  24.   end
  25.  
  26.   def arrival_must_be_on_a_saturday
  27.     # you need to arrive on a Saturday
  28.     if self.property.user.saturday_arrivals_only == true
  29.       errors.add(:arrival_date, "Arrival must be on a Saturday") if !arrival_date.blank? and arrival_date.wday != 6
  30.     end
  31.   end
  32.  
  33.   def check_total_in_the_party_adds_up
  34.     # check the total for the party is correct
  35.     errors.add(:total_in_party, "Number in the party does not add up") if number_of_adults.to_i + number_of_children.to_i + number_of_babies.to_i != total_in_party
  36.   end
  37.  
  38.   def booking_reference
  39.     [self.firstname,self.surname,(I18n.l self.arrival_date),(I18n.l self.departure_date)].join("_")
  40.   end
  41.  
  42.   def booking_dates
  43.     [(I18n.l self.arrival_date),(I18n.l self.departure_date)].join(" to ")
  44.   end
  45.  
  46.  
  47.  
  48. end
  49.  
  50.  
  51.  
  52.  
  53.  
  54. # validates_length_of   :name, :within => 2..30
  55. # validates_format_of   :name, :with => /\A[a-zA-Z]+\z/, :message => "only letters allowed"
  56. # validates_format_of   :occurrences, :with => /\A[0-9]+\z/, :message => "only numbers allowed"
  57. # validates_format_of   :rank, :with => /\A[0-9]+\z/, :message => "only numbers allowed"
  58. # validates_format_of   :gender, :with => /Boy|Girl/, :message => "only Boy or Girl allowed"
  59. # validates_presence_of :advert_type, :message => "can't be blank"
  60. # validates_length_of   :tag_line, :within => 10..140, :message => "Required field. 10 to 140 characters allowed."
  61. # validates_length_of   :name, :within => 3..40, :message => "Required field. 3 to 40 characters allowed."
  62. # validates_length_of   :body, :within => 10..1000, :message => "Required field. 10 to 1000 characters."
  63. # validates_length_of   :resort_ids, :minimum => 1, :message => " : Please tick any resort checkbox to link your advert to a relevant location(s)."
  64. # validates_format_of   :landline_phone_number, :landline_fax_number, :mobile_phone_number,
  65. #   :message => "must be a valid telephone number.",
  66. #   :with =>  /^([\(\)0-9\- \+\.]{10,20})?$/
  67. #
  68. # validates_format_of :web_site,
  69. #                     :allow_nil => true,
  70. #                     :allow_blank => true,
  71. #                     :with => /(^(http|https):\/\/[a-z0-9]+([-.]{1}[a-z0-9]*)+. [a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix,
  72. #                     :message => "needs to be a valid address e.g. with 'http://something.com'."