mas-rad_core 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 729804d663161e4c029297c5f94d0c8860c4154c
4
- data.tar.gz: 6aaaf2aa553730efba3af71b9804a71da7a0d741
3
+ metadata.gz: c5b261f38a51302a85395826c7355cea63db8e13
4
+ data.tar.gz: 676461c66f0f56cfe67048bcaeb179160f784ed0
5
5
  SHA512:
6
- metadata.gz: e82e1dc7cf0ff771cffdf919610e8fe04a6904197fe2c2424b23f21cbb522ec8fe7009a56ce28e88b975ba71ddc5bfbbbf2aef20732bf829cfc8dfd1315ce896
7
- data.tar.gz: 5ab392895e2039f158f5463b11ff799d6e8117197eb032dc9632c0bd169e6aea0b41300d824c065d4a02049220c9ed798c5956b3d1a0037ed94279296dad369c
6
+ metadata.gz: e3dc6b9964c8a858a234cf968e899559ed76c7a4dca3b922e1632d87ea423269993cc68ea2a700e4d4c147ab6e979be1e05e0d0ed9cac620a85d3e45774b47fa
7
+ data.tar.gz: d67170765c73e79eed23d627d6fa8e51afd190c2818f8824c6b804b209379a67ede033e31f25fe836db15703510e165b4c01000a9314274093dceec95253462c
@@ -2,20 +2,14 @@ require 'geocoder'
2
2
 
3
3
  class GeocodeFirmJob < ActiveJob::Base
4
4
  def perform(firm)
5
- results = Geocoder.search(firm.full_street_address)
6
-
7
- if results.any?
8
- stat :success
9
- firm.geocode!(results.first.latitude, results.first.longitude)
10
- else
11
- stat :failed
12
- firm.geocode!
13
- end
5
+ coordinates = Geocoder.coordinates(firm.full_street_address)
6
+ coordinates ? stat(:success) : stat(:failed)
7
+ firm.geocode!(coordinates)
14
8
  end
15
9
 
16
10
  private
17
11
 
18
12
  def stat(key)
19
- Stats.increment("radsignup.geocode_firm.#{key}")
13
+ Stats.increment("radsignup.geocode.firm.#{key}")
20
14
  end
21
15
  end
data/app/models/firm.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  class Firm < ActiveRecord::Base
2
+ include Geocodable
3
+
2
4
  PERCENTAGE_ATTRIBUTES = [
3
5
  :retirement_income_products_percent,
4
6
  :pension_transfer_percent,
@@ -96,22 +98,6 @@ class Firm < ActiveRecord::Base
96
98
  address_line_one_changed? || address_line_two_changed? || address_postcode_changed?
97
99
  end
98
100
 
99
- def latitude=(value)
100
- value = value.to_f.round(6) unless value.nil?
101
- write_attribute(:latitude, value)
102
- end
103
-
104
- def longitude=(value)
105
- value = value.to_f.round(6) unless value.nil?
106
- write_attribute(:longitude, value)
107
- end
108
-
109
- def geocode!(latitude = nil, longitude = nil)
110
- self.latitude = latitude
111
- self.longitude = longitude
112
- save!(callbacks: false)
113
- end
114
-
115
101
  def in_person_advice?
116
102
  in_person_advice_methods.present?
117
103
  end
@@ -1,5 +1,5 @@
1
1
  module MAS
2
2
  module RadCore
3
- VERSION = '0.0.6'
3
+ VERSION = '0.0.7'
4
4
  end
5
5
  end
@@ -98,79 +98,9 @@ RSpec.describe Adviser do
98
98
  it { is_expected.to eql "#{adviser.postcode}, United Kingdom"}
99
99
  end
100
100
 
101
- describe '#latitude=' do
102
- let(:adviser) { create(:adviser) }
103
- let(:latitude) { Faker::Address.latitude }
104
-
105
- before { adviser.latitude = latitude }
106
-
107
- it 'casts the value to a float rounded to six decimal places' do
108
- expect(adviser.latitude).to eql(latitude.to_f.round(6))
109
- end
110
-
111
- context 'when the value is nil' do
112
- let(:latitude) { nil }
113
-
114
- it 'does not cast the value' do
115
- expect(adviser.latitude).to be_nil
116
- end
117
- end
118
- end
119
-
120
- describe '#longitude=' do
121
- let(:adviser) { create(:adviser) }
122
- let(:longitude) { Faker::Address.longitude }
123
-
124
- before { adviser.longitude = longitude }
125
-
126
- it 'casts the value to a float rounded to six decimal places' do
127
- expect(adviser.longitude).to eql(longitude.to_f.round(6))
128
- end
129
-
130
- context 'when the value is nil' do
131
- let(:longitude) { nil }
132
-
133
- it 'does not cast the value' do
134
- expect(adviser.longitude).to be_nil
135
- end
136
- end
137
- end
138
-
139
- describe '#geocode!' do
140
- let(:adviser) { create(:adviser) }
141
- let(:coordinates) { [Faker::Address.latitude, Faker::Address.longitude] }
142
-
143
- before do
144
- expect(GeocodeFirmJob).not_to receive(:perform_later)
145
- adviser.geocode!(coordinates)
146
- adviser.reload
147
- end
148
-
149
- it 'the adviser is persisted' do
150
- expect(adviser).to be_persisted
151
- end
152
-
153
- context 'with valid coordinates' do
154
- it 'the adviser latitude is updated' do
155
- expect(adviser.latitude).to eql(coordinates.first.to_f.round(6))
156
- end
157
-
158
- it 'the adviser longitude is updated' do
159
- expect(adviser.longitude).to eql(coordinates.last.to_f.round(6))
160
- end
161
- end
162
-
163
- context 'with no coordinates' do
164
- let(:coordinates) { nil }
165
-
166
- it 'the adviser latitude is updated' do
167
- expect(adviser.latitude).to be_nil
168
- end
169
-
170
- it 'the adviser longitude is updated' do
171
- expect(adviser.longitude).to be_nil
172
- end
173
- end
101
+ it_should_behave_like 'geocodable' do
102
+ subject(:adviser) { create(:adviser) }
103
+ let(:job_class) { GeocodeAdviserJob }
174
104
  end
175
105
 
176
106
  describe 'after save' do
@@ -229,69 +229,9 @@ RSpec.describe Firm do
229
229
  end
230
230
  end
231
231
 
232
- describe '#latitude=' do
233
- let(:firm) { create(:firm) }
234
- let(:latitude) { Faker::Address.latitude }
235
-
236
- before { firm.latitude = latitude }
237
-
238
- it 'casts the value to a float rounded to six decimal places' do
239
- expect(firm.latitude).to eql(latitude.to_f.round(6))
240
- end
241
-
242
- context 'when the value is nil' do
243
- let(:latitude) { nil }
244
-
245
- it 'does not cast the value' do
246
- expect(firm.latitude).to be_nil
247
- end
248
- end
249
- end
250
-
251
- describe '#longitude=' do
252
- let(:firm) { create(:firm) }
253
- let(:longitude) { Faker::Address.longitude }
254
-
255
- before { firm.longitude = longitude }
256
-
257
- it 'casts the value to a float rounded to six decimal places' do
258
- expect(firm.longitude).to eql(longitude.to_f.round(6))
259
- end
260
-
261
- context 'when the value is nil' do
262
- let(:longitude) { nil }
263
-
264
- it 'does not cast the value' do
265
- expect(firm.longitude).to be_nil
266
- end
267
- end
268
- end
269
-
270
- describe '#geocode!' do
271
- let(:firm) { create(:firm) }
272
- let(:latitude) { Faker::Address.latitude }
273
- let(:longitude) { Faker::Address.longitude }
274
-
275
- it 'does not schedule the firm for geocoding' do
276
- expect(GeocodeFirmJob).not_to receive(:perform_later)
277
- firm.geocode!(latitude, longitude)
278
- end
279
-
280
- context 'after the geocode is complete' do
281
- before do
282
- firm.geocode!(latitude, longitude)
283
- firm.reload
284
- end
285
-
286
- it 'the firm is persisted' do
287
- expect(firm).to be_persisted
288
- end
289
-
290
- it 'the latitude and longitude attributes are updated' do
291
- expect(firm.latitude).to eql(latitude.to_f.round(6))
292
- expect(firm.longitude).to eql(longitude.to_f.round(6))
293
- end
294
- end
232
+ it_should_behave_like 'geocodable' do
233
+ subject(:firm) { create(:firm) }
234
+ let(:job_class) { GeocodeFirmJob }
295
235
  end
296
236
 
297
237
  describe 'geocoding' do
@@ -0,0 +1,73 @@
1
+ RSpec.shared_examples 'geocodable' do
2
+ describe '#latitude=' do
3
+ let(:latitude) { Faker::Address.latitude }
4
+
5
+ before { subject.latitude = latitude }
6
+
7
+ it 'casts the value to a float rounded to six decimal places' do
8
+ expect(subject.latitude).to eql(latitude.to_f.round(6))
9
+ end
10
+
11
+ context 'when the value is nil' do
12
+ let(:latitude) { nil }
13
+
14
+ it 'does not cast the value' do
15
+ expect(subject.latitude).to be_nil
16
+ end
17
+ end
18
+ end
19
+
20
+ describe '#longitude=' do
21
+ let(:longitude) { Faker::Address.longitude }
22
+
23
+ before { subject.longitude = longitude }
24
+
25
+ it 'casts the value to a float rounded to six decimal places' do
26
+ expect(subject.longitude).to eql(longitude.to_f.round(6))
27
+ end
28
+
29
+ context 'when the value is nil' do
30
+ let(:longitude) { nil }
31
+
32
+ it 'does not cast the value' do
33
+ expect(subject.longitude).to be_nil
34
+ end
35
+ end
36
+ end
37
+
38
+ describe '#geocode!' do
39
+ let(:coordinates) { [Faker::Address.latitude, Faker::Address.longitude] }
40
+
41
+ before do
42
+ expect(job_class).not_to receive(:perform_later)
43
+ subject.geocode!(coordinates)
44
+ subject.reload
45
+ end
46
+
47
+ it 'the model is persisted' do
48
+ expect(subject).to be_persisted
49
+ end
50
+
51
+ context 'with valid coordinates' do
52
+ it 'the latitude is updated' do
53
+ expect(subject.latitude).to eql(coordinates.first.to_f.round(6))
54
+ end
55
+
56
+ it 'the longitude is updated' do
57
+ expect(subject.longitude).to eql(coordinates.last.to_f.round(6))
58
+ end
59
+ end
60
+
61
+ context 'with no coordinates' do
62
+ let(:coordinates) { nil }
63
+
64
+ it 'the latitude is updated' do
65
+ expect(subject.latitude).to be_nil
66
+ end
67
+
68
+ it 'the longitude is updated' do
69
+ expect(subject.longitude).to be_nil
70
+ end
71
+ end
72
+ end
73
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mas-rad_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Lovell
@@ -292,6 +292,7 @@ files:
292
292
  - spec/models/principal_spec.rb
293
293
  - spec/models/qualification_spec.rb
294
294
  - spec/spec_helper.rb
295
+ - spec/support/shared_examples/geocodable_examples.rb
295
296
  - spec/support/shared_examples/reference_data.rb
296
297
  - spec/support/vcr.rb
297
298
  homepage: https://www.moneyadviceservice.org.uk
@@ -392,5 +393,6 @@ test_files:
392
393
  - spec/models/principal_spec.rb
393
394
  - spec/models/qualification_spec.rb
394
395
  - spec/spec_helper.rb
396
+ - spec/support/shared_examples/geocodable_examples.rb
395
397
  - spec/support/shared_examples/reference_data.rb
396
398
  - spec/support/vcr.rb