Model

Ruby code posted
created at 25 Aug 21:28

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Contact
  # self is the class
  include Validatable
 
  attr_accessor :name, :email, :phone, :message
 
  validates_presence_of :name, :message => "Name is required."
  validates_format_of :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,
    :message => "Email is not valid."
  validates_format_of :phone, :with => /^[0-9]{3,3}-[0-9]{3,3}-[0-9]{4,4}$/,
    :message => "Phone number is not valid (xxx-xxx-xxxx)."
  validates_presence_of :message, :message => "Name is required."
  
  class << self
    alias_method :new_without_params, :new
    def new params ={}
      returning new_without_params do |contact|
        params.each_pair do |key,value|
          contact.send "#{key}=", value
        end
      end
    end
  end
 
  def phone=(phone)
    # self is the object
    @phone = phone.gsub(/[^0-9]/, "").gsub(/^([0-9]{0,3})([0-9]{0,3})([0-9]{0,})$/) do |match|
      tmp = ""
      tmp += $1 if $1.size > 0
      tmp += "-" + $2 if $2.size > 0
      tmp += "-" + $3 if $3.size > 0
      tmp
    end
  end  
 
  def deliver
    if valid?
      ContactMailer.deliver_contact(self)
    else
      false
    end
  end
end
1.16 KB in 4 ms with coderay