My favorites
▼
|
Sign in
substruct
Open-source Ruby on Rails E-Commerce
Project Home
Downloads
Wiki
Issues
Source
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
142
attachment: valid_us_state_and_zip.diff
(4.4 KB)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Index: plugins/substruct/app/models/order_address.rb
===================================================================
--- plugins/substruct/app/models/order_address.rb (revision 163)
+++ plugins/substruct/app/models/order_address.rb (working copy)
@@ -1,44 +1,110 @@
-class OrderAddress < ActiveRecord::Base
- # Association
- has_one :order
- belongs_to :order_user
- belongs_to :country
- # Validation
- validates_presence_of :order_user_id, :country_id
- validates_presence_of :zip, :message => "#{ERROR_EMPTY} If you live in a country that doesn't have postal codes please enter '00000'."
- validates_presence_of :telephone, :message => ERROR_EMPTY
- validates_presence_of :first_name, :message => ERROR_EMPTY
- validates_presence_of :last_name, :message => ERROR_EMPTY
- validates_presence_of :address, :message => ERROR_EMPTY
- # Require city / state if USA
- validates_presence_of :city, :state,
- :if => Proc.new { |oa| oa.country_id == 1 }
-
- validates_length_of :first_name, :maximum => 50
- validates_length_of :last_name, :maximum => 50
- validates_length_of :address, :maximum => 255
-
- # Makes sure validation address doesn't allow PO Box or variants
- def validate
- invalid_strings = ['PO BOX', 'P.O. BOX', 'P.O BOX', 'PO. BOX',
- 'POBOX', 'P.OBOX', 'P.O.BOX', 'PO.BOX', 'P.BOX',
- 'PBOX', 'AFO', 'A.F.O.', 'APO', 'A.P.O.']
- cap_address = self.address.upcase()
- invalid_strings.each do |string|
- if cap_address.include?(string) then
- errors.add(:address, "Sorry, we don't ship to P.O. boxes")
- return
- end
- end
- end
-
- # Finds the shipping address for a given OrderUser
- def self.find_shipping_address_for_user(user)
- find(:first,
- :conditions => ["order_user_id = ? AND is_shipping = 1", user.id])
- end
-
- def name
- "#{self.first_name} #{self.last_name}"
- end
-end
+class OrderAddress < ActiveRecord::Base
+ # Association
+ has_one :order
+ belongs_to :order_user
+ belongs_to :country
+ # Validation
+ validates_presence_of :order_user_id, :country_id
+ validates_presence_of :zip, :message => "#{ERROR_EMPTY} If you live in a country that doesn't have postal codes please enter '00000'."
+ validates_presence_of :telephone, :message => ERROR_EMPTY
+ validates_presence_of :first_name, :message => ERROR_EMPTY
+ validates_presence_of :last_name, :message => ERROR_EMPTY
+ validates_presence_of :address, :message => ERROR_EMPTY
+ # Require city / state if USA
+ validates_presence_of :city, :state,
+ :if => Proc.new { |oa| oa.country.name == 'United States of America' }
+
+ validates_length_of :first_name, :maximum => 50
+ validates_length_of :last_name, :maximum => 50
+ validates_length_of :address, :maximum => 255
+
+US_STATES =
+%\AK
+AZ
+CT
+FL
+HI
+IL
+KY
+MD
+MN
+MT
+NE
+NM
+OH
+PA
+SC
+TX
+VI
+WI
+AL
+CA
+DC
+GA
+IA
+IN
+LA
+ME
+MO
+NC
+NH
+NV
+OK
+PR
+SD
+UT
+VT
+WV
+AR
+CO
+DE
+GU
+ID
+KS
+MA
+MI
+MS
+ND
+NJ
+NY
+OR
+RI
+TN
+VA
+WA
+WY\.split("\n").map{|state| state.strip}
+
+ #Makes sure validation address doesn't allow PO Box or variants
+ def validate
+ invalid_strings =
+ ['PO BOX', 'P.O. BOX', 'P.O BOX', 'PO. BOX', 'POBOX',
+ 'P.OBOX', 'P.O.BOX', 'PO.BOX', 'P.BOX', 'PBOX', 'AFO',
+ 'A.F.O.', 'APO', 'A.P.O.']
+ cap_address = self.address.upcase()
+ invalid_strings.each do |string|
+ if cap_address.include?(string) then
+ errors.add(:address, "Sorry, we don't ship to P.O. boxes")
+ end
+ end
+ if self.country.name == "United States of America"
+ unless zip.blank?
+ unless zip =~ /^\d{5}/
+ errors.add(:zip, "Please enter a valid zip.")
+ end
+ end
+ self.state = self.state.upcase
+ errors.add(:state, "Please use a US state abbreviation") unless US_STATES.include?(self.state)
+ end
+ end
+
+
+ # Finds the shipping address for a given OrderUser
+ def self.find_shipping_address_for_user(user)
+ find(:first,
+ :conditions => ["order_user_id = ? AND is_shipping = 1", user.id])
+ end
+
+ def name
+ "#{self.first_name} #{self.last_name}"
+ end
+end
Powered by
Google Project Hosting