address_concern 2.0.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Readme.md +73 -6
- data/address_concern.gemspec +2 -3
- data/app/models/concerns/address.rb +784 -0
- data/{lib/address_concern → app/models/concerns}/address_associations.rb +33 -12
- data/app/models/concerns/attributes_slice.rb +54 -0
- data/app/models/concerns/inspect_base.rb +32 -0
- data/config/address_concern.rb +3 -0
- data/lib/address_concern/attribute_normalizer.rb +10 -6
- data/lib/address_concern/engine.rb +24 -0
- data/lib/address_concern/version.rb +1 -1
- data/lib/address_concern.rb +4 -3
- data/lib/core_extensions/hash/reorder.rb +11 -0
- data/lib/core_extensions/string/cleanlines.rb +28 -0
- data/lib/generators/address_concern/templates/migration.rb +14 -10
- data/spec/models/acts_as_address_spec.rb +66 -0
- data/spec/models/address_spec.rb +345 -117
- data/spec/spec_helper.rb +1 -5
- data/spec/support/models/address.rb +2 -1
- data/spec/support/models/address_custom_attr_names.rb +11 -0
- data/spec/support/models/address_with_code_only.rb +12 -0
- data/spec/support/models/address_with_name_only.rb +5 -0
- data/spec/support/models/address_with_separate_address_columns.rb +5 -0
- data/spec/support/models/user.rb +6 -1
- data/spec/support/schema.rb +22 -0
- metadata +17 -34
- data/lib/address_concern/address.rb +0 -306
data/spec/models/address_spec.rb
CHANGED
@@ -1,32 +1,185 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Address do
|
4
|
+
def klass
|
5
|
+
described_class
|
6
|
+
end
|
7
|
+
|
8
|
+
describe AddressWithNameOnly do
|
9
|
+
it 'using attribute alias' do
|
10
|
+
expect(klass.state_name_attribute).to eq :state
|
11
|
+
expect(klass.country_name_attribute).to eq :country
|
12
|
+
|
13
|
+
address = klass.new(country_code: 'AE')
|
14
|
+
expect(address.country_code).to eq 'AE'
|
15
|
+
expect(address.country ).to eq 'United Arab Emirates'
|
16
|
+
|
17
|
+
address = klass.new(country_name: 'Iceland')
|
18
|
+
expect(address.country_name).to eq 'Iceland'
|
19
|
+
expect(address.country ).to eq 'Iceland'
|
20
|
+
|
21
|
+
address = klass.new(country: 'United States', state_code: 'ID')
|
22
|
+
expect(address.state_code).to eq 'ID'
|
23
|
+
expect(address.state ).to eq 'Idaho'
|
24
|
+
|
25
|
+
address = klass.new(country: 'United States', state_name: 'Idaho')
|
26
|
+
expect(address.state_name).to eq 'Idaho'
|
27
|
+
expect(address.state ).to eq 'Idaho'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe AddressWithCodeOnly do
|
32
|
+
it 'using attribute alias' do
|
33
|
+
expect(klass.state_code_attribute).to eq :state
|
34
|
+
expect(klass.country_code_attribute).to eq :country
|
35
|
+
|
36
|
+
address = klass.new(country_code: 'AE')
|
37
|
+
expect(address.country_code).to eq 'AE'
|
38
|
+
expect(address.country ).to eq 'AE'
|
39
|
+
|
40
|
+
address = klass.new(country_name: 'Iceland')
|
41
|
+
expect(address.country_name).to eq 'Iceland'
|
42
|
+
expect(address.country ).to eq 'IS'
|
43
|
+
|
44
|
+
address = klass.new(country: 'US', state_code: 'ID')
|
45
|
+
expect(address.state_code).to eq 'ID'
|
46
|
+
expect(address.state ).to eq 'ID'
|
47
|
+
|
48
|
+
address = klass.new(country: 'US', state_name: 'Idaho')
|
49
|
+
address.state_name = 'Idaho'
|
50
|
+
expect(address.state_name).to eq 'Idaho'
|
51
|
+
expect(address.state ).to eq 'ID'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
#═════════════════════════════════════════════════════════════════════════════════════════════════
|
56
|
+
describe 'setting to invalid values' do
|
57
|
+
let(:address) { klass.new }
|
58
|
+
|
59
|
+
# Even if it's an unrecognized country/state code/name, we need to write the value to the
|
60
|
+
# attribute (in memory only), so that it can be validated and shown in the form if it
|
61
|
+
# re-rendered. To prevent invalid values from being persisted, you should add validations.
|
62
|
+
it do
|
63
|
+
address = Address.new
|
64
|
+
address.country = 'Fireland'
|
65
|
+
expect(address.country_name).to eq('Fireland')
|
66
|
+
expect(address.country_code).to eq(nil)
|
67
|
+
|
68
|
+
address = Address.new
|
69
|
+
address.country_code = 'FL'
|
70
|
+
expect(address.country_name).to eq(nil)
|
71
|
+
expect(address.country_code).to eq('FL')
|
72
|
+
|
73
|
+
address = Address.new(country: 'United States')
|
74
|
+
address.state = 'New Zork'
|
75
|
+
expect(address.state_name).to eq('New Zork')
|
76
|
+
expect(address.state_code).to eq(nil)
|
77
|
+
|
78
|
+
address = Address.new(country: 'United States')
|
79
|
+
address.state_code = 'NZ'
|
80
|
+
expect(address.state_name).to eq(nil)
|
81
|
+
expect(address.state_code).to eq('NZ')
|
82
|
+
end
|
83
|
+
|
84
|
+
describe AddressWithCodeOnly do
|
85
|
+
it do
|
86
|
+
address = klass.new
|
87
|
+
address.country = 'Fireland'
|
88
|
+
expect(address.country_name).to eq(nil)
|
89
|
+
expect(address.country_code).to eq('Fireland')
|
90
|
+
|
91
|
+
address = klass.new
|
92
|
+
address.country_name = 'Fireland'
|
93
|
+
expect(address.country_name).to eq(nil) # No attribute to store it in
|
94
|
+
expect(address.country_code).to eq(nil)
|
95
|
+
|
96
|
+
address = klass.new
|
97
|
+
address.country_code = 'FL'
|
98
|
+
expect(address.country_name).to eq(nil)
|
99
|
+
expect(address.country_code).to eq('FL')
|
100
|
+
|
101
|
+
address = klass.new(country: 'United States')
|
102
|
+
address.state = 'New Zork'
|
103
|
+
expect(address.state_name).to eq(nil)
|
104
|
+
expect(address.state_code).to eq('New Zork')
|
105
|
+
|
106
|
+
address = klass.new(country: 'United States')
|
107
|
+
address.state_name = 'New Zork'
|
108
|
+
expect(address.state_name).to eq(nil) # No attribute to store it in
|
109
|
+
expect(address.state_code).to eq(nil)
|
110
|
+
|
111
|
+
address = klass.new(country: 'United States')
|
112
|
+
address.state_code = 'NZ'
|
113
|
+
expect(address.state_name).to eq(nil)
|
114
|
+
expect(address.state_code).to eq('NZ')
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe AddressWithNameOnly do
|
119
|
+
it do
|
120
|
+
address = klass.new
|
121
|
+
address.country = 'Fireland'
|
122
|
+
expect(address.country_name).to eq('Fireland')
|
123
|
+
expect(address.country_code).to eq(nil)
|
124
|
+
|
125
|
+
address = klass.new
|
126
|
+
address.country_name = 'Fireland'
|
127
|
+
expect(address.country_name).to eq('Fireland')
|
128
|
+
expect(address.country_code).to eq(nil)
|
129
|
+
|
130
|
+
address = klass.new
|
131
|
+
address.country_code = 'FL'
|
132
|
+
expect(address.country_name).to eq(nil)
|
133
|
+
expect(address.country_code).to eq(nil) # No attribute to store it in
|
134
|
+
|
135
|
+
address = klass.new(country: 'United States')
|
136
|
+
address.state = 'New Zork'
|
137
|
+
expect(address.state_name).to eq('New Zork')
|
138
|
+
expect(address.state_code).to eq(nil)
|
139
|
+
|
140
|
+
address = klass.new(country: 'United States')
|
141
|
+
address.state_name = 'New Zork'
|
142
|
+
expect(address.state_name).to eq('New Zork')
|
143
|
+
expect(address.state_code).to eq(nil)
|
144
|
+
|
145
|
+
address = klass.new(country: 'United States')
|
146
|
+
address.state_code = 'NZ'
|
147
|
+
expect(address.state_name).to eq(nil)
|
148
|
+
expect(address.state_code).to eq(nil) # No attribute to store it in
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
#═════════════════════════════════════════════════════════════════════════════════════════════════
|
154
|
+
|
4
155
|
describe 'setting country by name' do
|
5
156
|
let(:address) { Address.new }
|
6
157
|
specify 'setting to known country' do
|
7
158
|
address.country = 'Iceland'
|
8
|
-
address.country_name.
|
9
|
-
address.country_code.
|
159
|
+
expect(address.country_name).to eq('Iceland')
|
160
|
+
expect(address.country_code).to eq('IS')
|
10
161
|
end
|
11
162
|
|
12
|
-
|
13
|
-
|
14
|
-
|
163
|
+
context "setting to name instead of code" do
|
164
|
+
subject { Address.new(country_name: 'US') }
|
165
|
+
# Unlike state_name=, country_name does not use find_carmen_country. Usually with country
|
166
|
+
# you'll know very well ahead of time whether you're dealing with names or codes. Less likely
|
167
|
+
# with states.
|
168
|
+
it { expect(subject.country_name).to eq('US') }
|
169
|
+
end
|
15
170
|
|
16
|
-
|
171
|
+
specify 'setting to unknown country' do
|
17
172
|
address.country = 'Fireland'
|
18
|
-
|
19
|
-
|
20
|
-
address.country_name.should eq nil
|
21
|
-
address.country_code.should eq nil
|
173
|
+
expect(address.country_name).to eq 'Fireland'
|
174
|
+
expect(address.country_code).to eq nil
|
22
175
|
|
23
176
|
address.country = 'Northern Ireland'
|
24
|
-
address.country_name.
|
25
|
-
address.country_code.
|
177
|
+
expect(address.country_name).to eq 'Northern Ireland'
|
178
|
+
expect(address.country_code).to eq nil
|
26
179
|
|
27
|
-
address.country = '
|
28
|
-
address.country_name.
|
29
|
-
address.country_code.
|
180
|
+
address.country = 'Estados Unidos'
|
181
|
+
expect(address.country_name).to eq 'Estados Unidos'
|
182
|
+
expect(address.country_code).to eq nil
|
30
183
|
end
|
31
184
|
end
|
32
185
|
|
@@ -34,24 +187,14 @@ describe Address do
|
|
34
187
|
let(:address) { Address.new }
|
35
188
|
specify 'setting to known country' do
|
36
189
|
address.country_code = 'IS'
|
37
|
-
address.country_name.
|
38
|
-
address.country_code.
|
190
|
+
expect(address.country_name).to eq('Iceland')
|
191
|
+
expect(address.country_code).to eq('IS')
|
39
192
|
end
|
40
193
|
|
41
194
|
specify 'setting to unknown country' do
|
42
|
-
# Set it to a known country first to show that it actually *clears* these fields if they were previously set
|
43
|
-
address.country_code = 'IS'
|
44
|
-
|
45
|
-
#expect { expect {
|
46
195
|
address.country = 'FL'
|
47
|
-
|
48
|
-
|
49
|
-
address.country_name.should eq nil
|
50
|
-
address.country_code.should eq nil
|
51
|
-
end
|
52
|
-
|
53
|
-
specify 'setting to a country that is part of another country (weird)' do
|
54
|
-
# Not currently possible using country_code=
|
196
|
+
expect(address.country_name).to eq 'FL'
|
197
|
+
expect(address.country_code).to eq nil
|
55
198
|
end
|
56
199
|
end
|
57
200
|
|
@@ -59,31 +202,108 @@ describe Address do
|
|
59
202
|
let(:address) { Address.new }
|
60
203
|
['The Democratic Republic of the Congo', 'Democratic Republic of the Congo'].each do |_| specify _ do
|
61
204
|
address.country = _
|
62
|
-
address.country_name.
|
63
|
-
address.country_name_from_code.
|
205
|
+
expect(address.country_name).to eq('Congo, The Democratic Republic of the')
|
206
|
+
expect(address.country_name_from_code).to eq('Congo, The Democratic Republic of the')
|
64
207
|
end; end
|
208
|
+
it do
|
209
|
+
address.country = 'USA'
|
210
|
+
expect(address.country_name).to eq('United States')
|
211
|
+
end
|
65
212
|
end
|
66
213
|
|
67
|
-
|
214
|
+
#═════════════════════════════════════════════════════════════════════════════════════════════════
|
215
|
+
|
216
|
+
describe 'setting state by name' do
|
217
|
+
context "simple" do
|
218
|
+
subject { Address.new(state: 'FL', country_name: 'United States') }
|
219
|
+
it { expect(subject.state_code).to eq('FL') }
|
220
|
+
end
|
221
|
+
|
222
|
+
context "setting to name instead of code" do
|
223
|
+
subject { Address.new(state: 'Florida', country_name: 'United States') }
|
224
|
+
# Uses find_carmen_state, which finds by name, falling back to finding by code.
|
225
|
+
it { expect(subject.state_code).to eq('FL') }
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
#═════════════════════════════════════════════════════════════════════════════════════════════════
|
230
|
+
|
231
|
+
describe 'present?' do
|
232
|
+
let(:address) { Address.new }
|
68
233
|
[:address, :city, :state, :postal_code].each do |attr_name|
|
69
234
|
it "should be true when #{attr_name} (and only #{attr_name}) is present" do
|
70
|
-
|
235
|
+
address.write_attribute attr_name, 'something'
|
236
|
+
expect(address).to be_present
|
71
237
|
end
|
72
238
|
end
|
73
239
|
it "should be true when country (and only country) is present" do
|
74
|
-
Address.new(:
|
240
|
+
expect(Address.new(country: 'Latvia')).to be_present
|
75
241
|
end
|
76
242
|
end
|
77
243
|
|
78
|
-
describe 'normalization' do
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
244
|
+
#describe 'normalization' do
|
245
|
+
# describe 'address' do
|
246
|
+
# it { is_expected.to normalize_attribute(:address).from(" Line 1 \n Line 2 \n ").to("Line 1\nLine 2")}
|
247
|
+
# [:city, :state, :postal_code, :country].each do |attr_name|
|
248
|
+
# it { is_expected.to normalize_attribute(attr_name) }
|
249
|
+
# end
|
250
|
+
# end
|
251
|
+
#end
|
252
|
+
|
253
|
+
#═════════════════════════════════════════════════════════════════════════════════════════════════
|
254
|
+
describe 'address, address_lines' do
|
255
|
+
describe Address do
|
256
|
+
it do
|
257
|
+
expect(klass.multi_line_address?).to eq true
|
258
|
+
|
259
|
+
address = klass.new(address: str = 'Line 1')
|
260
|
+
expect(address.address).to eq str
|
261
|
+
|
262
|
+
address = klass.new(address: str = "Line 1\nLine 2\nLine 3")
|
263
|
+
expect(address.address).to eq str
|
264
|
+
expect(address.address_lines).to eq [
|
265
|
+
'Line 1',
|
266
|
+
'Line 2',
|
267
|
+
'Line 3',
|
268
|
+
]
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
describe AddressWithSeparateAddressColumns do
|
273
|
+
it do
|
274
|
+
expect(klass.multi_line_address?).to eq false
|
275
|
+
|
276
|
+
address = klass.new(
|
277
|
+
address_1: 'Line 1',
|
278
|
+
address_2: 'Line 2',
|
279
|
+
address_3: 'Line 3',
|
280
|
+
)
|
281
|
+
expect(address.address_1).to eq 'Line 1'
|
282
|
+
expect(address.address_2).to eq 'Line 2'
|
283
|
+
expect(address.address_3).to eq 'Line 3'
|
284
|
+
expect(address.address_lines).to eq [
|
285
|
+
'Line 1',
|
286
|
+
'Line 2',
|
287
|
+
'Line 3',
|
288
|
+
]
|
83
289
|
end
|
84
290
|
end
|
85
291
|
end
|
86
292
|
|
293
|
+
#═════════════════════════════════════════════════════════════════════════════════════════════════
|
294
|
+
describe '#inspect' do
|
295
|
+
it do
|
296
|
+
address = Address.new(
|
297
|
+
address: '10 Some Road',
|
298
|
+
city: 'Watford', state: 'HRT', postal_code: 'WD25 9JZ',
|
299
|
+
country: 'United Kingdom'
|
300
|
+
)
|
301
|
+
expect(address.inspect).to eq \
|
302
|
+
"<Address new: address: 10 Some Road, city: Watford, state: Hertfordshire, state_code: HRT, postal_code: WD25 9JZ, country: United Kingdom, country_code: GB>"
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
#═════════════════════════════════════════════════════════════════════════════════════════════════
|
87
307
|
describe 'parts and lines' do
|
88
308
|
it do
|
89
309
|
address = Address.new(
|
@@ -91,20 +311,20 @@ describe Address do
|
|
91
311
|
city: 'Watford', state: 'HRT', postal_code: 'WD25 9JZ',
|
92
312
|
country: 'United Kingdom'
|
93
313
|
)
|
94
|
-
address.parts.
|
314
|
+
expect(address.parts).to eq([
|
95
315
|
'10 Some Road',
|
96
316
|
'Watford',
|
97
317
|
'Hertfordshire',
|
98
318
|
'WD25 9JZ',
|
99
319
|
'United Kingdom'
|
100
|
-
]
|
101
|
-
address.lines.
|
320
|
+
])
|
321
|
+
expect(address.lines).to eq([
|
102
322
|
'10 Some Road',
|
103
323
|
'Watford WD25 9JZ',
|
104
324
|
'United Kingdom'
|
105
|
-
]
|
106
|
-
address.city_line.
|
107
|
-
address.city_state_name.
|
325
|
+
])
|
326
|
+
expect(address.city_line).to eq('Watford WD25 9JZ')
|
327
|
+
expect(address.city_state_name).to eq('Watford, Hertfordshire')
|
108
328
|
end
|
109
329
|
end
|
110
330
|
|
@@ -112,131 +332,139 @@ describe Address do
|
|
112
332
|
context "when address doesn't have a state" do
|
113
333
|
let(:user) { User.create }
|
114
334
|
subject { user.build_physical_address(address: '123', city: 'Stockholm', country_name: 'Sweden') }
|
115
|
-
it { subject.city_state_code.
|
116
|
-
it { subject.city_state_name.
|
117
|
-
it { subject.city_state_country.
|
335
|
+
it { expect(subject.city_state_code).to eq('Stockholm') }
|
336
|
+
it { expect(subject.city_state_name).to eq('Stockholm') }
|
337
|
+
it { expect(subject.city_state_country).to eq('Stockholm, Sweden') }
|
118
338
|
end
|
119
339
|
context "when address has a state abbrevitation in :state field" do
|
120
340
|
let(:user) { User.create }
|
121
341
|
subject { user.build_physical_address(address: '123', city: 'Nelspruit', state: 'MP', country_name: 'South Africa') }
|
122
|
-
it { subject.
|
123
|
-
it { subject.
|
124
|
-
it { subject.
|
342
|
+
it { expect(subject.state_code).to eq('MP') }
|
343
|
+
it { expect(subject.city_state_code).to eq('Nelspruit, MP') }
|
344
|
+
it { expect(subject.city_state_name).to eq('Nelspruit, Mpumalanga') }
|
345
|
+
it { expect(subject.city_state_country).to eq('Nelspruit, Mpumalanga, South Africa') }
|
125
346
|
end
|
126
347
|
context "when address has a state abbrevitation in :state field (Denmark)" do
|
127
348
|
let(:user) { User.create }
|
128
349
|
subject { user.build_physical_address(address: '123', city: 'Copenhagen', state: '84', country_name: 'Denmark') }
|
129
|
-
it { subject.city_state_name.
|
130
|
-
it { subject.state_name.
|
350
|
+
it { expect(subject.city_state_name).to eq('Copenhagen, Hovedstaden') }
|
351
|
+
it { expect(subject.state_name).to eq('Hovedstaden') }
|
131
352
|
end
|
132
353
|
|
133
354
|
# United States
|
134
355
|
context "when address has a state name entered for :state instead of an abbreviation" do
|
135
356
|
let(:user) { User.create }
|
136
357
|
subject { user.build_physical_address(address: '123', city: 'Ocala', state: 'Florida', country_name: 'United States') }
|
137
|
-
it { subject.city_state_code.
|
138
|
-
it { subject.city_state_name.
|
139
|
-
it { subject.city_state_country.
|
358
|
+
it { expect(subject.city_state_code).to eq('Ocala, FL') }
|
359
|
+
it { expect(subject.city_state_name).to eq('Ocala, Florida') }
|
360
|
+
it { expect(subject.city_state_country).to eq('Ocala, Florida, United States') }
|
140
361
|
end
|
141
362
|
context "when address has a state abbrevitation in :state field" do
|
142
363
|
let(:user) { User.create }
|
143
364
|
subject { user.build_physical_address(address: '123', city: 'Ocala', state: 'FL', country_name: 'United States') }
|
144
|
-
it { subject.city_state_code.
|
145
|
-
it { subject.city_state_name.
|
146
|
-
it { subject.city_state_country.
|
365
|
+
it { expect(subject.city_state_code).to eq('Ocala, FL') }
|
366
|
+
it { expect(subject.city_state_name).to eq('Ocala, Florida') }
|
367
|
+
it { expect(subject.city_state_country).to eq('Ocala, Florida, United States') }
|
147
368
|
end
|
148
|
-
context "when address has a state name
|
369
|
+
context "when address has a state name instead of code entered for state_name, and state is for different country" do
|
149
370
|
let(:user) { User.create }
|
150
|
-
subject { user.build_physical_address(address: '123', city: 'Ocala',
|
151
|
-
it
|
152
|
-
|
153
|
-
|
371
|
+
subject { user.build_physical_address(address: '123', city: 'Ocala', state_code: 'FL', country_name: 'Denmark') }
|
372
|
+
it do
|
373
|
+
expect(subject.state_code). to eq('FL')
|
374
|
+
expect(subject.state_name). to eq(nil)
|
375
|
+
expect(subject.city_state_code). to eq('Ocala, FL')
|
376
|
+
expect(subject.city_state_name). to eq('Ocala')
|
377
|
+
expect(subject.city_state_country). to eq('Ocala, Denmark')
|
378
|
+
end
|
154
379
|
end
|
155
380
|
end
|
156
381
|
|
157
382
|
describe 'same_as?' do
|
158
383
|
it 'should be true when country is only present attribute and it matches' do
|
159
|
-
Address.new(country: 'United States').
|
384
|
+
expect(Address.new(country: 'United States')).to be_same_as(
|
160
385
|
Address.new(country: 'United States'))
|
161
386
|
end
|
162
387
|
it 'should be true when country and state are only present attributes and they match' do
|
163
|
-
Address.new(state: 'Washington', country: 'United States').
|
388
|
+
expect(Address.new(state: 'Washington', country: 'United States')).to be_same_as(
|
164
389
|
Address.new(state: 'Washington', country: 'United States'))
|
165
390
|
end
|
166
391
|
it "not should be true when address attribute doesn't match" do
|
167
|
-
Address.new(address: '123 C St.', state: 'Washington', country: 'United States').
|
392
|
+
expect(Address.new(address: '123 C St.', state: 'Washington', country: 'United States')).not_to be_same_as(
|
168
393
|
Address.new(address: '444 Z St.', state: 'Washington', country: 'United States'))
|
169
394
|
end
|
170
395
|
end
|
171
396
|
|
397
|
+
#═════════════════════════════════════════════════════════════════════════════════════════════════
|
172
398
|
describe '#carmen_country' do
|
173
|
-
it { Address.new(country: 'South Africa').carmen_country.
|
399
|
+
it { expect(Address.new(country: 'South Africa').carmen_country).to be_a Carmen::Country }
|
174
400
|
end
|
175
401
|
describe '#carmen_state' do
|
176
|
-
it { Address.new(country: 'United States', state: 'OH!').carmen_state.
|
177
|
-
it { Address.new(country: 'United States', state: 'OH').carmen_state.
|
178
|
-
it { Address.new(country: 'United States', state: 'AA').carmen_state.
|
179
|
-
it { Address.new(country: 'South Africa', state: 'MP').carmen_state.
|
402
|
+
it { expect(Address.new(country: 'United States', state: 'OH!').carmen_state).to be_nil }
|
403
|
+
it { expect(Address.new(country: 'United States', state: 'OH').carmen_state).to be_a Carmen::Region }
|
404
|
+
it { expect(Address.new(country: 'United States', state: 'AA').carmen_state).to be_a Carmen::Region }
|
405
|
+
it { expect(Address.new(country: 'South Africa', state: 'MP').carmen_state).to be_a Carmen::Region }
|
180
406
|
end
|
181
407
|
|
182
408
|
describe '#states_for_country, etc.' do
|
183
|
-
it { Address.new(country: 'United States').states_for_country.map(&:code).
|
184
|
-
it { Address.new(country: 'United States').states_for_country.map(&:name).
|
185
|
-
it { Address.new(country: 'United States').states_for_country.map(&:name).
|
186
|
-
it { Address.new(country: 'United States').states_for_country.map(&:name).
|
187
|
-
it { Address.new(country: 'South Africa').states_for_country.map(&:name).
|
188
|
-
it { Address.new(country: 'Kenya').states_for_country.typed('province').
|
409
|
+
it { expect(Address.new(country: 'United States').states_for_country.map(&:code)).to include 'AA' }
|
410
|
+
it { expect(Address.new(country: 'United States').states_for_country.map(&:name)).to include 'Ohio' }
|
411
|
+
it { expect(Address.new(country: 'United States').states_for_country.map(&:name)).to include 'District of Columbia' }
|
412
|
+
it { expect(Address.new(country: 'United States').states_for_country.map(&:name)).to include 'Puerto Rico' }
|
413
|
+
it { expect(Address.new(country: 'South Africa').states_for_country.map(&:name)).to include 'Mpumalanga' }
|
414
|
+
it { expect(Address.new(country: 'Kenya').states_for_country.typed('province')).to be_empty }
|
189
415
|
# At the time of this writing, it doesn't look like Carmen has been updated to
|
190
416
|
# include the 47 counties listed under https://en.wikipedia.org/wiki/ISO_3166-2:KE.
|
191
417
|
#it { Address.new(country: 'Kenya').states_for_country.map(&:name).should include 'Nyeri' }
|
192
|
-
it { Address.new(country: 'Denmark').state_options.
|
193
|
-
it { Address.new(country: 'Denmark').state_options.map(&:name).
|
194
|
-
it { Address.new(country: 'Denmark').state_possibly_included_in_postal_address
|
418
|
+
it { expect(Address.new(country: 'Denmark').state_options).to be_many }
|
419
|
+
it { expect(Address.new(country: 'Denmark').state_options.map(&:name)).to include 'Sjælland' }
|
420
|
+
it { expect(Address.new(country: 'Denmark').state_possibly_included_in_postal_address?).to eq false }
|
195
421
|
|
196
422
|
# Auckland (AUK) is a subregion of the North Island (N) subregion
|
197
|
-
it { Address.new(country: 'New Zealand').state_options.map(&:code).
|
198
|
-
it { Address.new(country: 'New Zealand').state_options.map(&:code).
|
423
|
+
it { expect(Address.new(country: 'New Zealand').state_options.map(&:code)).to include 'AUK' }
|
424
|
+
it { expect(Address.new(country: 'New Zealand').state_options.map(&:code)).not_to include 'N' }
|
199
425
|
# Chatham Islands Territory (CIT) is a top-level region
|
200
|
-
it { Address.new(country: 'New Zealand').state_options.map(&:code).
|
426
|
+
it { expect(Address.new(country: 'New Zealand').state_options.map(&:code)).to include 'CIT' }
|
201
427
|
|
202
428
|
# Abra (ABR) is a subregion of the Cordillera Administrative Region (CAR) (15) subregion
|
203
|
-
it { Address.new(country: 'Philippines').state_options.map(&:code).
|
204
|
-
it { Address.new(country: 'Philippines').state_options.map(&:code).
|
429
|
+
it { expect(Address.new(country: 'Philippines').state_options.map(&:code)).to include 'ABR' }
|
430
|
+
it { expect(Address.new(country: 'Philippines').state_options.map(&:code)).not_to include '15' }
|
205
431
|
# National Capital Region (00) is a top-level region
|
206
|
-
it { Address.new(country: 'Philippines').state_options.map(&:code).
|
432
|
+
it { expect(Address.new(country: 'Philippines').state_options.map(&:code)).to include '00' }
|
207
433
|
|
208
434
|
# https://en.wikipedia.org/wiki/Provinces_of_Indonesia
|
209
435
|
# The provinces are officially grouped into seven geographical units
|
210
436
|
# Jawa Barat (JB) is a subregion of the Jawa (JW) subregion
|
211
|
-
it { Address.new(country: 'Indonesia').state_options.map(&:code).
|
212
|
-
it { Address.new(country: 'Indonesia').state_options.map(&:code).
|
437
|
+
it { expect(Address.new(country: 'Indonesia').state_options.map(&:code)).to include 'JB' }
|
438
|
+
it { expect(Address.new(country: 'Indonesia').state_options.map(&:code)).not_to include 'JW' }
|
213
439
|
# The province is not called "Jakarta Raya" according to
|
214
440
|
# https://en.wikipedia.org/wiki/ISO_3166-2:ID and https://en.wikipedia.org/wiki/Jakarta — it's
|
215
441
|
# called 'DKI Jakarta', which is short for 'Daerah Khusus Ibukota Jakarta' ('Special Capital
|
216
442
|
# City District of Jakarta'),
|
217
|
-
it { Address.new(country: 'Indonesia').state_options.map(&:name).
|
443
|
+
it { expect(Address.new(country: 'Indonesia').state_options.map(&:name)).to include 'DKI Jakarta' }
|
218
444
|
|
219
445
|
# At the time of this writing, it doesn't look like Carmen has been updated to reflect the new 18
|
220
446
|
# regions of France.
|
221
|
-
it { Address.new(country: 'France').state_options.size.
|
447
|
+
it { expect(Address.new(country: 'France').state_options.size).to eq 26 }
|
448
|
+
it { expect(Address.new(country_name: 'France').state_options.map(&:name)).to include 'Auvergne-Rhône-Alpes' }
|
222
449
|
#it { Address.new(country: 'France').state_options.size.should eq 18 }
|
223
450
|
#it { Address.new(country: 'France').state_options.map(&:name).should include 'Auvergne-Rhône-Alpes' }
|
224
451
|
end
|
225
452
|
|
453
|
+
#═════════════════════════════════════════════════════════════════════════════════════════════════
|
226
454
|
describe 'associations' do
|
227
455
|
# To do (or maybe not even necessary—seems to work with only the has_one side of the association):
|
228
456
|
#describe 'when we have a polymorphic belongs_to :addressable in Address' do
|
229
|
-
#belongs_to :addressable, :
|
457
|
+
#belongs_to :addressable, polymorphic: true
|
230
458
|
|
231
459
|
describe 'Company has_one :address' do
|
232
460
|
let(:company) { Company.create }
|
233
461
|
|
234
462
|
it do
|
235
|
-
company.address.
|
463
|
+
expect(company.address).to eq(nil)
|
236
464
|
address = company.build_address(address: '1')
|
237
465
|
company.save!
|
238
|
-
company.reload.address.
|
239
|
-
address.addressable.
|
466
|
+
expect(company.reload.address).to eq(address)
|
467
|
+
expect(address.addressable).to eq(company)
|
240
468
|
end
|
241
469
|
|
242
470
|
end
|
@@ -245,26 +473,26 @@ describe Address do
|
|
245
473
|
let(:user) { User.create }
|
246
474
|
|
247
475
|
it do
|
248
|
-
user.addresses.
|
476
|
+
expect(user.addresses).to eq([])
|
249
477
|
address_1 = user.addresses.build(address: '1')
|
250
478
|
address_2 = user.addresses.build(address: '2')
|
251
479
|
user.save!; user.reload
|
252
|
-
user.addresses.
|
480
|
+
expect(user.addresses).to eq([address_1, address_2])
|
253
481
|
end
|
254
482
|
|
255
483
|
specify 'should able to set and retrieve a specific address by type (shipping or billing)' do
|
256
484
|
physical_address = user.build_physical_address(address: 'Physical')
|
257
485
|
shipping_address = user.build_shipping_address(address: 'Shipping')
|
258
486
|
billing_address = user.build_billing_address( address: 'Billing')
|
259
|
-
vacation_address = user.addresses.build(address: 'Vacation', :
|
487
|
+
vacation_address = user.addresses.build(address: 'Vacation', address_type: 'Vacation')
|
260
488
|
user.save!; user.reload
|
261
|
-
user.physical_address.
|
262
|
-
user.shipping_address.
|
263
|
-
user.billing_address.
|
264
|
-
user.addresses.to_set.
|
265
|
-
physical_address.addressable.
|
266
|
-
shipping_address.addressable.
|
267
|
-
billing_address .addressable.
|
489
|
+
expect(user.physical_address).to eq(physical_address)
|
490
|
+
expect(user.shipping_address).to eq(shipping_address)
|
491
|
+
expect(user.billing_address). to eq(billing_address)
|
492
|
+
expect(user.addresses.to_set).to eq([physical_address, shipping_address, billing_address, vacation_address].to_set)
|
493
|
+
expect(physical_address.addressable).to eq(user)
|
494
|
+
expect(shipping_address.addressable).to eq(user)
|
495
|
+
expect(billing_address .addressable).to eq(user)
|
268
496
|
end
|
269
497
|
end
|
270
498
|
|
@@ -272,15 +500,15 @@ describe Address do
|
|
272
500
|
subject!(:employee) { Employee.create! }
|
273
501
|
|
274
502
|
it do
|
275
|
-
employee.home_address.
|
276
|
-
employee.work_address.
|
503
|
+
expect(employee.home_address).to eq(nil)
|
504
|
+
expect(employee.work_address).to eq(nil)
|
277
505
|
home_address = employee.build_home_address(address: '1')
|
278
506
|
work_address = employee.build_work_address(address: '2')
|
279
507
|
employee.save!
|
280
|
-
employee.reload.home_address.
|
281
|
-
employee.reload.work_address.
|
282
|
-
home_address.employee_home.
|
283
|
-
work_address.employee_work.
|
508
|
+
expect(employee.reload.home_address).to eq(home_address)
|
509
|
+
expect(employee.reload.work_address).to eq(work_address)
|
510
|
+
expect(home_address.employee_home).to eq(employee)
|
511
|
+
expect(work_address.employee_work).to eq(employee)
|
284
512
|
end
|
285
513
|
end
|
286
514
|
|
@@ -288,15 +516,15 @@ describe Address do
|
|
288
516
|
subject!(:child) { Child.create! }
|
289
517
|
|
290
518
|
it do
|
291
|
-
child.address.
|
292
|
-
child.secret_hideout.
|
519
|
+
expect(child.address). to eq(nil)
|
520
|
+
expect(child.secret_hideout).to eq(nil)
|
293
521
|
address = child.build_address(address: '2')
|
294
522
|
secret_hideout = child.build_secret_hideout(address: '1')
|
295
523
|
child.save!
|
296
|
-
child.reload.address.
|
297
|
-
child.reload.secret_hideout.
|
298
|
-
address. child.
|
299
|
-
secret_hideout.child_for_secret_hideout.
|
524
|
+
expect(child.reload.address). to eq(address)
|
525
|
+
expect(child.reload.secret_hideout).to eq(secret_hideout)
|
526
|
+
expect(address. child). to eq(child)
|
527
|
+
expect(secret_hideout.child_for_secret_hideout).to eq(child)
|
300
528
|
end
|
301
529
|
end
|
302
530
|
end
|