mas-rad_core 0.0.94 → 0.0.95

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: 17774aeb4e9da3bb4753dea23b904b0eec7f7cc1
4
- data.tar.gz: f509893a0e1f105581b713cc0f0c23e1556653e4
3
+ metadata.gz: 2d69336ba464137daf9b2b5c75caf488380f704e
4
+ data.tar.gz: 96b5d9ddabcbb1d2f5fa22c9162c8fcd343ad999
5
5
  SHA512:
6
- metadata.gz: 36922d7ebb281fbf003837e53bf25c2ba5f4eff37685ec3bfaeba678ddfda6e609a40a07050cb1043fea60caaaa83fc2fe4ac1dfe86e744a01087180fc0cdbbd
7
- data.tar.gz: 2fa9c442b64770106e69d24868deb227e94c232929f53c60c15a5e8b25a5db83340e59f6d54b64450833648da148ab78bd06a158cc7033d57423b640cf8be189
6
+ metadata.gz: 99f998d199dffad79993f6c0b4c39f6998d06966b5aea5ba0522ab5b1efa032d77f45417fea8331f5045b20caf5b87bcad2ecdb1a21292aa7745e8402574702b
7
+ data.tar.gz: 4419dfe1cd4c769a647c30a8d73436007174d157c230cfcf0c89a9f30d86b49935ff8908eb5eb94955bb1836947045f9f7a1c32226dc136b957887ccfeba52ae
@@ -1,6 +1,5 @@
1
1
  class Adviser < ActiveRecord::Base
2
2
  include Geocodable
3
- include GeocodableSync
4
3
 
5
4
  attr_reader :old_firm_id
6
5
 
@@ -25,8 +25,26 @@ module Geocodable
25
25
  self.latitude, self.longitude = coordinates
26
26
  end
27
27
 
28
- def update_coordinates!(coordinates)
29
- self.coordinates = coordinates
30
- update_columns(latitude: latitude, longitude: longitude)
28
+ def geocode
29
+ return false unless valid?
30
+ return true unless needs_to_be_geocoded?
31
+
32
+ self.coordinates = ModelGeocoder.geocode(self)
33
+ add_geocoding_failed_error unless geocoded?
34
+
35
+ geocoded?
36
+ end
37
+
38
+ def needs_to_be_geocoded?
39
+ !geocoded? || has_address_changes?
40
+ end
41
+
42
+ def save_with_geocoding
43
+ geocode && save
44
+ end
45
+
46
+ def update_with_geocoding(params)
47
+ self.attributes = params
48
+ save_with_geocoding
31
49
  end
32
50
  end
data/app/models/office.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  class Office < ActiveRecord::Base
2
2
  include Geocodable
3
- include GeocodableSync
4
3
 
5
4
  ADDRESS_FIELDS = [
6
5
  :address_line_one,
@@ -2,10 +2,4 @@ module ModelGeocoder
2
2
  def self.geocode(geocodable)
3
3
  Geocoder.coordinates(geocodable.full_street_address)
4
4
  end
5
-
6
- def self.geocode!(geocodable)
7
- coordinates = geocode(geocodable)
8
- geocodable.update_coordinates!(coordinates)
9
- coordinates.present?
10
- end
11
5
  end
@@ -15,4 +15,10 @@ class OfficeResult
15
15
  @disabled_access = data['disabled_access']
16
16
  @location = Location.new data['location']['lat'], data['location']['lon']
17
17
  end
18
+
19
+ def telephone_number
20
+ return nil unless @telephone_number
21
+
22
+ UKPhoneNumbers.format(@telephone_number)
23
+ end
18
24
  end
@@ -1,5 +1,5 @@
1
1
  module MAS
2
2
  module RadCore
3
- VERSION = '0.0.94'
3
+ VERSION = '0.0.95'
4
4
  end
5
5
  end
@@ -1,9 +1,7 @@
1
1
  RSpec.describe ModelGeocoder do
2
2
  let(:model_class) do
3
3
  Class.new do
4
- attr_accessor :address_line_one, :address_line_two, :address_postcode, :longitude, :latitude
5
-
6
- def update_coordinates!(*args); end
4
+ attr_accessor :address_line_one, :address_line_two, :address_postcode
7
5
 
8
6
  def full_street_address
9
7
  [address_line_one, address_line_two, address_postcode, 'United Kingdom'].reject(&:blank?).join(', ')
@@ -45,37 +43,4 @@ RSpec.describe ModelGeocoder do
45
43
  end
46
44
  end
47
45
  end
48
-
49
- describe '#geocode!' do
50
- context 'when the model address can be geocoded' do
51
- before do
52
- allow(ModelGeocoder).to receive(:geocode).and_return(expected_coordinates)
53
- end
54
-
55
- it 'calls model.update_coordinates! with the coordinates' do
56
- expect(model).to receive(:update_coordinates!).with(expected_coordinates)
57
- ModelGeocoder.geocode!(model)
58
- end
59
-
60
- it 'returns the true' do
61
- expect(ModelGeocoder.geocode!(model)).to be(true)
62
- end
63
- end
64
-
65
- context 'when model address cannot be geocoded' do
66
- before do
67
- allow(ModelGeocoder).to receive(:geocode).and_return(nil)
68
- end
69
-
70
- # This side effect is required while the geocoding is done on a background job
71
- it 'calls model.update_coordinates! with nil' do
72
- expect(model).to receive(:update_coordinates!).with(nil)
73
- ModelGeocoder.geocode!(model)
74
- end
75
-
76
- it 'returns false' do
77
- expect(ModelGeocoder.geocode!(model)).to be(false)
78
- end
79
- end
80
- end
81
46
  end
@@ -8,7 +8,7 @@ RSpec.describe OfficeResult do
8
8
  'address_county' => 'Cumbria',
9
9
  'address_postcode' => 'LA8 9BE',
10
10
  'email_address' => 'postie@example.com',
11
- 'telephone_number' => '5555 555 5555',
11
+ 'telephone_number' => '02085952346',
12
12
  'disabled_access' => true,
13
13
  'location' => { 'lat' => 51.5180697, 'lon' => -0.1085203 }
14
14
  }
@@ -24,7 +24,7 @@ RSpec.describe OfficeResult do
24
24
  specify { expect(subject.address_county).to eq('Cumbria') }
25
25
  specify { expect(subject.address_postcode).to eq('LA8 9BE') }
26
26
  specify { expect(subject.email_address).to eq('postie@example.com') }
27
- specify { expect(subject.telephone_number).to eq('5555 555 5555') }
27
+ specify { expect(subject.telephone_number).to eq('020 8595 2346') }
28
28
  specify { expect(subject.disabled_access).to eq(true) }
29
29
 
30
30
  it 'maps the location with latitude and longitude' do
@@ -103,10 +103,6 @@ RSpec.describe Adviser do
103
103
  end
104
104
 
105
105
  it_should_behave_like 'geocodable' do
106
- subject { create(:adviser) }
107
- end
108
-
109
- it_should_behave_like 'synchronously geocodable' do
110
106
  let(:invalid_geocodable) { Adviser.new }
111
107
  let(:valid_new_geocodable) { FactoryGirl.build(:adviser) }
112
108
  let(:saved_geocodable) { FactoryGirl.create(:adviser) }
@@ -5,10 +5,6 @@ RSpec.describe Office do
5
5
  subject(:office) { firm.offices.first }
6
6
 
7
7
  it_should_behave_like 'geocodable' do
8
- subject { office }
9
- end
10
-
11
- it_should_behave_like 'synchronously geocodable' do
12
8
  let(:invalid_geocodable) { Office.new }
13
9
  let(:valid_new_geocodable) { FactoryGirl.build(:office, firm: firm) }
14
10
  let(:saved_geocodable) { office }
@@ -1,4 +1,21 @@
1
1
  RSpec.shared_examples 'geocodable' do
2
+ describe 'the interface the geocodable must implement' do
3
+ subject { valid_new_geocodable }
4
+
5
+ # needed for the ModelGeocoder
6
+ it { is_expected.to respond_to(:full_street_address) }
7
+
8
+ # used by #needs_to_be_geocoded?
9
+ it { is_expected.to respond_to(:has_address_changes?) }
10
+
11
+ # used by #geocode
12
+ it { is_expected.to respond_to(:add_geocoding_failed_error) }
13
+ end
14
+
15
+ def modify_address(subject)
16
+ subject.send("#{address_field_name}=", address_field_updated_value)
17
+ end
18
+
2
19
  describe '#latitude=' do
3
20
  let(:latitude) { Faker::Address.latitude }
4
21
 
@@ -35,60 +52,232 @@ RSpec.shared_examples 'geocodable' do
35
52
  end
36
53
  end
37
54
 
38
- describe '#update_coordinates!' do
39
- let(:coordinates) { [Faker::Address.latitude, Faker::Address.longitude] }
55
+ describe '#geocoded?' do
56
+ context 'when the subject has lat/long' do
57
+ before do
58
+ subject.latitude, subject.longitude = [1.0, 1.0]
59
+ end
40
60
 
41
- before do
42
- subject.update_coordinates!(coordinates)
43
- subject.reload
61
+ it 'is classed as geocoded' do
62
+ expect(subject.geocoded?).to be(true)
63
+ end
44
64
  end
45
65
 
46
- it 'the model is persisted' do
47
- expect(subject).to be_persisted
66
+ context 'when the subject does not have lat/long' do
67
+ before do
68
+ subject.latitude, subject.longitude = [nil, nil]
69
+ end
70
+
71
+ it 'is not classed as geocoded' do
72
+ expect(subject.geocoded?).to be(false)
73
+ end
48
74
  end
75
+ end
76
+
77
+ describe '#geocode' do
78
+ context 'when the subject is not valid' do
79
+ subject { invalid_geocodable }
49
80
 
50
- context 'with valid coordinates' do
51
- it 'the latitude is updated' do
52
- expect(subject.latitude).to eql(coordinates.first.to_f.round(6))
81
+ it 'does not call the geocoder' do
82
+ expect(ModelGeocoder).not_to receive(:geocode)
83
+ subject.geocode
53
84
  end
54
85
 
55
- it 'the longitude is updated' do
56
- expect(subject.longitude).to eql(coordinates.last.to_f.round(6))
86
+ it 'returns false' do
87
+ expect(subject.geocode).to be(false)
57
88
  end
58
89
  end
59
90
 
60
- context 'with no coordinates' do
61
- let(:coordinates) { nil }
91
+ context 'when the subject is valid' do
92
+ subject { valid_new_geocodable }
62
93
 
63
- it 'the latitude is updated' do
64
- expect(subject.latitude).to be_nil
94
+ context 'when the subject does not need to be geocoded' do
95
+ before do
96
+ subject.coordinates = [1.0, 1.0]
97
+ subject.save!
98
+ end
99
+
100
+ it 'does not call the geocoder' do
101
+ expect(ModelGeocoder).not_to receive(:geocode)
102
+ subject.geocode
103
+ end
104
+
105
+ it 'returns true' do
106
+ expect(subject.geocode).to be(true)
107
+ end
65
108
  end
66
109
 
67
- it 'the longitude is updated' do
68
- expect(subject.longitude).to be_nil
110
+ context 'when the subject needs to be geocoded' do
111
+ before do
112
+ subject.coordinates = nil
113
+ end
114
+
115
+ context 'when geocoding succeeds' do
116
+ before do
117
+ allow(ModelGeocoder).to receive(:geocode).and_return([1.0, 1.0])
118
+ end
119
+
120
+ it 'returns true' do
121
+ expect(subject.geocode).to be(true)
122
+ end
123
+
124
+ specify 'subject.errors.count is 0' do
125
+ subject.geocode
126
+ expect(subject.errors.count).to be(0)
127
+ end
128
+
129
+ context 'no persistence' do
130
+ before do
131
+ subject.save!
132
+ expect(subject).not_to be_changed
133
+ expect(subject.coordinates).to eq([nil, nil])
134
+ subject.geocode
135
+ end
136
+
137
+ it 'sets the new coordinates on the in-memory instance' do
138
+ expect(subject.coordinates).to eq([1.0, 1.0])
139
+ end
140
+
141
+ it 'does not persist the changed fields' do
142
+ expect(subject).to be_changed
143
+ expect(subject.reload.coordinates).to eq([nil, nil])
144
+ end
145
+ end
146
+ end
147
+
148
+ context 'when geocoding fails' do
149
+ before do
150
+ allow(ModelGeocoder).to receive(:geocode).and_return(nil)
151
+ end
152
+
153
+ it 'adds an error to subject.errors' do
154
+ subject.geocode
155
+ expect(subject.errors).to have_key(:geocoding)
156
+ end
157
+
158
+ it 'returns false' do
159
+ expect(subject.geocode).to be(false)
160
+ end
161
+
162
+ context 'no persistence' do
163
+ before do
164
+ subject.coordinates = [1.0, 1.0]
165
+ subject.save!
166
+ modify_address(subject)
167
+ subject.geocode
168
+ end
169
+
170
+ it 'blanks out the in-memory instance coordinates' do
171
+ expect(subject.coordinates).to eq([nil, nil])
172
+ end
173
+
174
+ it 'does not persist the changed fields' do
175
+ expect(subject).to be_changed
176
+ expect(subject.reload.coordinates).to eq([1.0, 1.0])
177
+ end
178
+ end
179
+ end
69
180
  end
70
181
  end
71
182
  end
72
183
 
73
- describe '#geocoded?' do
74
- context 'when the subject has lat/long' do
184
+ describe '#needs_to_be_geocoded?' do
185
+ subject { saved_geocodable }
186
+
187
+ context 'when the model has not been geocoded' do
75
188
  before do
76
- subject.latitude, subject.longitude = [1.0, 1.0]
189
+ subject.coordinates = nil
190
+ expect(subject).not_to be_geocoded
77
191
  end
78
192
 
79
- it 'is classed as geocoded' do
80
- expect(subject.geocoded?).to be(true)
193
+ it 'returns true' do
194
+ expect(subject.needs_to_be_geocoded?).to be(true)
81
195
  end
82
196
  end
83
197
 
84
- context 'when the subject does not have lat/long' do
198
+ context 'when the model has been geocoded' do
85
199
  before do
86
- subject.latitude, subject.longitude = [nil, nil]
200
+ subject.coordinates = [1.0, 1.0]
201
+ subject.save!
202
+ expect(subject).to be_geocoded
87
203
  end
88
204
 
89
- it 'is not classed as geocoded' do
90
- expect(subject.geocoded?).to be(false)
205
+ context 'when the model address fields have not changed' do
206
+ before do
207
+ expect(subject).not_to have_address_changes
208
+ end
209
+
210
+ it 'returns false' do
211
+ expect(subject.needs_to_be_geocoded?).to be(false)
212
+ end
213
+ end
214
+
215
+ context 'when the model address fields have changed' do
216
+ before do
217
+ modify_address(subject)
218
+ expect(subject).to have_address_changes
219
+ end
220
+
221
+ it 'returns true' do
222
+ expect(subject.needs_to_be_geocoded?).to be(true)
223
+ end
91
224
  end
92
225
  end
93
226
  end
227
+
228
+ describe '#save_with_geocoding' do
229
+ before { allow(saved_geocodable).to receive(:geocode).and_return(result_of_geocoding) }
230
+ subject { saved_geocodable.save_with_geocoding }
231
+
232
+ context 'when geocoding fails' do
233
+ let(:result_of_geocoding) { false }
234
+
235
+ it { is_expected.to be(false) }
236
+
237
+ it 'does not call save' do
238
+ expect(saved_geocodable).not_to receive(:save)
239
+ subject
240
+ end
241
+ end
242
+
243
+ context 'when geocoding succeeds' do
244
+ let(:result_of_geocoding) { true }
245
+ let(:result_of_saving) { true }
246
+ before { allow(saved_geocodable).to receive(:save).and_return(result_of_saving) }
247
+
248
+ it 'calls save' do
249
+ expect(saved_geocodable).to receive(:save)
250
+ subject
251
+ end
252
+
253
+ context 'when saving fails' do
254
+ let(:result_of_saving) { false }
255
+ it { is_expected.to be(false) }
256
+ end
257
+
258
+ context 'when saving succeeds' do
259
+ it { is_expected.to be(true) }
260
+ end
261
+ end
262
+ end
263
+
264
+ describe '#update_with_geocoding' do
265
+ subject { saved_geocodable.update_with_geocoding(updated_address_params) }
266
+
267
+ it 'updates the geocodable with new attributes' do
268
+ allow(saved_geocodable).to receive(:save_with_geocoding)
269
+ subject
270
+ expect(saved_geocodable.changed_attributes).to include(updated_address_params.keys.first)
271
+ end
272
+
273
+ it 'calls #save_with_geocoding' do
274
+ expect(saved_geocodable).to receive(:save_with_geocoding)
275
+ subject
276
+ end
277
+
278
+ it 'returns the return value of #save_with_geocoding' do
279
+ allow(saved_geocodable).to receive(:save_with_geocoding).and_return(:return_marker)
280
+ expect(subject).to eq(:return_marker)
281
+ end
282
+ end
94
283
  end