Fixed conditions syntax in some finder methods.
[merb_mart.git] / app / models / order_address.rb
blob3ea2efb14ba536c7c474073e4fcacf86f807c7e2
1 class OrderAddress
2   
3   include DataMapper::Persistable
4   
5   #has_one :order
6   belongs_to :order_user
7   belongs_to :country
8   
9   #t.integer "order_user_id",               :default => 0,  :null => false
10   
11   property :first_name, :string, :length => 50, :default => "", :nullable => false
12   property :last_name,  :string, :length => 50, :default => "", :nullable => false
13   property :telephone,  :string, :length => 20
14   property :address,    :string,                :default => "", :nullable => false
15   property :city,       :string, :length => 50
16   property :state,      :string, :length => 30
17   property :zip,        :string, :length => 10 
18   
19   # Validation
20   #validates_presence_of :order_user_id, :country_id
21   #validates_presence_of :zip, :message => "#{ERROR_EMPTY} If you live in a country that doesn't have postal codes please enter '00000'."
22   #validates_presence_of :telephone, :message => ERROR_EMPTY
23   #validates_presence_of :first_name, :message => ERROR_EMPTY
24   #validates_presence_of :last_name, :message => ERROR_EMPTY
25   #validates_presence_of :address, :message => ERROR_EMPTY
26   
27   #validates_length_of :first_name, :maximum => 50
28   #validates_length_of :last_name, :maximum => 50
29   #validates_length_of :address, :maximum => 255
30   
31   #validates_exclusion_of :address, :in => ['PO BOX', 'P.O. BOX', 'AFO', 'A.F.O.', 'APO', 'A.P.O.'],
32   #                       :message => 'Sorry, we don\'nt ship to P.O. boxes'
34   # Makes sure validation address doesn't allow PO Box or variants
35   def validate
36     invalid_strings = ['PO BOX', 'P.O. BOX', 'P.O BOX', 'PO. BOX',
37                        'POBOX', 'P.OBOX', 'P.O.BOX', 'PO.BOX', 'P.BOX',
38                        'PBOX', 'AFO', 'A.F.O.', 'APO', 'A.P.O.']
39     cap_address = self.address.upcase()
40     invalid_strings.each do |string|
41       if cap_address.include?(string) then
42         errors.add(:address, "Sorry, we don't ship to P.O. boxes")
43         return
44       end
45     end
46   end
47   
48         # Finds the shipping address for a given OrderUser
49   def self.find_shipping_address_for_user(user)
50     first(:conditions => ["order_user_id = ? AND is_shipping = 1", user.id])
51   end
52   
53   def name
54     "#{self.first_name} #{self.last_name}"
55   end
56   
57 end