mas-rad_core 0.0.79 → 0.0.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 521b22a5badf25b6ce05404aa5c17be7bbbd4dae
4
- data.tar.gz: b6e4c36d334ba2ee984007c5703e3557ecd7884d
3
+ metadata.gz: 439f2ddeb557336ca2337cfbf574b802cb70d4bb
4
+ data.tar.gz: e3bbba73a760d498ffaa5ac78cc9fb7a41bd2e3a
5
5
  SHA512:
6
- metadata.gz: 6eccaa43a5482ef5619be1f43d8eae4a568b9b5e7d5de29948b3e1f2fbe3f491500c77aa9b03663432b5e21eea0253dc668a20d43cfcc4b162b62d0d42027e36
7
- data.tar.gz: ef809c8c824fe1af9e6d1ae52599bc3f78f20c3e4fff63a9fea97acf9585e6728c74be9362ca88b0f70f8d563728563149cab36d671f223fe7ed8d0c727328cf
6
+ metadata.gz: c65f5254b56b84ef07cc0482a0fc63a5ae55a6e7fbcc63fcb11af08ae64a946e045c174cb59d851787fb31e9af6757dd33f160e4c1a4e1da9a885b49f303f7c1
7
+ data.tar.gz: 0cbbb811bb47b2e00fec9b4d7292ba386c31cfba517c80974636d0bbd7180351e7df8113aabfb8af46d1f33ff8be8dc0a908da301fd8fea01b38a10b21c27440
@@ -31,9 +31,9 @@ class Adviser < ActiveRecord::Base
31
31
 
32
32
  validate :match_reference_number
33
33
 
34
+ after_save :flag_changes_for_after_commit
34
35
  after_commit :geocode_and_reindex_firm
35
- after_commit :reindex_old_firm, if: :firm_id_changed?
36
-
36
+ after_commit :reindex_old_firm
37
37
  scope :sorted_by_name, -> { order(:name) }
38
38
 
39
39
  def self.on_firms_with_fca_number(fca_number)
@@ -63,6 +63,13 @@ class Adviser < ActiveRecord::Base
63
63
 
64
64
  private
65
65
 
66
+ # All record of what changed is gone by the time we get to the after_commit
67
+ # hooks. So we cannot use #firm_id_changed? at that point. To work around
68
+ # this we flag any important changes here to be actioned later.
69
+ def flag_changes_for_after_commit
70
+ @old_firm_id = firm_id_change.first if firm_id_changed?
71
+ end
72
+
66
73
  def geocode_and_reindex_firm
67
74
  if destroyed?
68
75
  firm.geocode_and_reindex
@@ -72,7 +79,8 @@ class Adviser < ActiveRecord::Base
72
79
  end
73
80
 
74
81
  def reindex_old_firm
75
- IndexFirmJob.perform_later(Firm.find(attribute_was(:firm_id)))
82
+ IndexFirmJob.perform_later(Firm.find(@old_firm_id)) if @old_firm_id.present?
83
+ @old_firm_id = nil
76
84
  end
77
85
 
78
86
  def upcase_postcode
data/app/models/firm.rb CHANGED
@@ -109,7 +109,7 @@ class Firm < ActiveRecord::Base
109
109
  validates :investment_sizes,
110
110
  length: { minimum: 1 }
111
111
 
112
- after_save :publish_to_elastic_search
112
+ after_commit :publish_to_elastic_search, unless: :destroyed?
113
113
  after_commit :delete_elastic_search_entry, if: :destroyed?
114
114
 
115
115
  def publish_to_elastic_search
@@ -195,7 +195,11 @@ class Firm < ActiveRecord::Base
195
195
  end
196
196
 
197
197
  def publishable?
198
- main_office.present?
198
+ valid? && main_office.present? && !missing_advisers?
199
+ end
200
+
201
+ def missing_advisers?
202
+ (primary_advice_method == :local) && advisers.empty?
199
203
  end
200
204
 
201
205
  private
@@ -1,5 +1,5 @@
1
1
  module MAS
2
2
  module RadCore
3
- VERSION = '0.0.79'
3
+ VERSION = '0.0.80'
4
4
  end
5
5
  end
Binary file
@@ -12,7 +12,7 @@ FactoryGirl.define do
12
12
  travel_distance '650'
13
13
  latitude { Faker::Address.latitude.to_f.round(6) }
14
14
  longitude { Faker::Address.longitude.to_f.round(6) }
15
- firm
15
+ firm factory: :firm_without_advisers
16
16
 
17
17
  after(:build) do |a, evaluator|
18
18
  if a.reference_number? && evaluator.create_linked_lookup_advisor
@@ -1,7 +1,7 @@
1
1
  FactoryGirl.define do
2
2
  sequence(:registered_name) { |n| "Financial Advice #{n} Ltd." }
3
3
 
4
- factory :firm, aliases: [:publishable_firm] do
4
+ factory :firm, aliases: [:publishable_firm, :onboarded_firm] do
5
5
  fca_number
6
6
  registered_name
7
7
  email_address { Faker::Internet.email }
@@ -34,24 +34,27 @@ FactoryGirl.define do
34
34
  firm.reload
35
35
  end
36
36
 
37
- factory :onboarded_firm, traits: [:with_advisers] do
37
+ transient do
38
38
  advisers_count 1
39
39
  end
40
40
 
41
- factory :not_onboarded_firm, traits: [:invalid]
41
+ after(:create) do |firm, evaluator|
42
+ create_list(:adviser, evaluator.advisers_count, firm: firm)
43
+ end
42
44
 
43
45
  factory :trading_name, aliases: [:subsidiary] do
44
46
  parent factory: Firm
45
47
  end
46
48
 
47
49
  factory :firm_with_advisers, traits: [:with_advisers]
50
+ factory :firm_without_advisers, traits: [:without_advisers]
48
51
  factory :firm_with_offices, traits: [:with_offices]
49
52
  factory :firm_with_principal, traits: [:with_principal]
50
53
  factory :firm_with_no_business_split, traits: [:with_no_business_split]
51
54
  factory :firm_with_remote_advice, traits: [:with_remote_advice]
52
55
  factory :firm_with_subsidiaries, traits: [:with_trading_names]
53
56
  factory :firm_with_trading_names, traits: [:with_trading_names]
54
- factory :invalid_firm, traits: [:invalid]
57
+ factory :invalid_firm, traits: [:invalid], aliases: [:not_onboarded_firm]
55
58
 
56
59
  trait :invalid do
57
60
  email_address nil
@@ -67,13 +70,11 @@ FactoryGirl.define do
67
70
  end
68
71
 
69
72
  trait :with_advisers do
70
- transient do
71
- advisers_count 3
72
- end
73
+ advisers_count 3
74
+ end
73
75
 
74
- after(:create) do |firm, evaluator|
75
- create_list(:adviser, evaluator.advisers_count, firm: firm)
76
- end
76
+ trait :without_advisers do
77
+ advisers_count 0
77
78
  end
78
79
 
79
80
  trait :with_principal do
@@ -154,6 +154,23 @@ RSpec.describe Adviser do
154
154
  end
155
155
  end
156
156
 
157
+ describe 'after_save :flag_changes_for_after_commit' do
158
+ let(:original_firm) { create(:firm) }
159
+ let(:receiving_firm) { create(:firm) }
160
+ subject { create(:adviser, firm: original_firm) }
161
+
162
+ before do
163
+ subject.firm = receiving_firm
164
+ subject.save!
165
+ end
166
+
167
+ context 'when the firm has changed' do
168
+ it 'stores the original firm id so it can be reindexed in an after_commit hook' do
169
+ expect(subject.old_firm_id).to eq(original_firm.id)
170
+ end
171
+ end
172
+ end
173
+
157
174
  describe 'after_commit :reindex_old_firm' do
158
175
  let(:original_firm) { create(:firm) }
159
176
  let(:receiving_firm) { create(:firm) }
@@ -165,19 +182,19 @@ RSpec.describe Adviser do
165
182
  end
166
183
 
167
184
  context 'when the firm has changed' do
168
- it 'triggers reindexing of the original firm the first time' do
185
+ it 'triggers reindexing of the adviser and new firm' do
186
+ expect(GeocodeAdviserJob).to receive(:perform_later).with(subject)
169
187
  subject.firm = receiving_firm
170
188
  save_with_commit_callback(subject)
171
- expect(queue_contains_a_job_for(IndexFirmJob)).to be_truthy
172
189
  end
173
190
 
174
- it 'does not trigger reindexing of the original firm thereafter' do
191
+ it 'triggers reindexing of the original firm (once)' do
192
+ expect(IndexFirmJob).to receive(:perform_later).once().with(original_firm)
175
193
  subject.firm = receiving_firm
176
194
  save_with_commit_callback(subject)
177
195
 
178
- clear_job_queue
196
+ # Trigger a second time
179
197
  subject.run_callbacks(:commit)
180
- expect(queue_contains_a_job_for(IndexFirmJob)).to be_falsey
181
198
  end
182
199
  end
183
200
  end
@@ -229,10 +246,12 @@ RSpec.describe Adviser do
229
246
 
230
247
  describe '#on_firms_with_fca_number' do
231
248
  it 'returns advisers on firm and its trading names' do
232
- firm = FactoryGirl.create(:firm)
233
- trading_name = FactoryGirl.create(:trading_name, fca_number: firm.fca_number)
234
- advisers = [FactoryGirl.create(:adviser, firm_id: firm.id),
235
- FactoryGirl.create(:adviser, firm_id: trading_name.id)]
249
+ firm = FactoryGirl.create(:firm_with_advisers, advisers_count: 1)
250
+ trading_name = FactoryGirl.create(:trading_name,
251
+ :with_advisers,
252
+ advisers_count: 1,
253
+ fca_number: firm.fca_number)
254
+ advisers = [firm.advisers.first, trading_name.advisers.first]
236
255
 
237
256
  returned_advisers = Adviser.on_firms_with_fca_number(firm.fca_number)
238
257
  expect(returned_advisers.length).to eq(2)
@@ -23,13 +23,13 @@ RSpec.describe 'Firm factory' do
23
23
  specify 'associations' do
24
24
  expect(subject.principal).not_to be_present
25
25
  expect(subject).to have(1).offices
26
- expect(subject).to have(:no).advisers
26
+ expect(subject).to have(1).advisers
27
27
  expect(subject).to have(:no).trading_names
28
28
  end
29
29
  end
30
30
 
31
- describe 'factory :onboarded_firm' do
32
- let(:factory) { :onboarded_firm }
31
+ describe 'factory :firm_with_advisers' do
32
+ let(:factory) { :firm_with_advisers }
33
33
 
34
34
  specify 'expected status' do
35
35
  expect(subject).to be_persisted
@@ -42,37 +42,18 @@ RSpec.describe 'Firm factory' do
42
42
  specify 'associations' do
43
43
  expect(subject.principal).not_to be_present
44
44
  expect(subject).to have(1).offices
45
- expect(subject).to have(1).advisers
46
- expect(subject).to have(:no).trading_names
47
- end
48
- end
49
-
50
- describe 'factory :not_onboarded_firm' do
51
- let(:factory) { :not_onboarded_firm }
52
-
53
- specify 'expected status' do
54
- expect(subject).not_to be_persisted
55
- expect(subject).not_to be_valid
56
- expect(subject).not_to be_publishable
57
- expect(subject).not_to be_trading_name
58
- expect(subject.primary_advice_method).to be(:local)
59
- end
60
-
61
- specify 'associations' do
62
- expect(subject.principal).not_to be_present
63
- expect(subject).to have(:no).offices
64
- expect(subject).to have(:no).advisers
45
+ expect(subject).to have(3).advisers
65
46
  expect(subject).to have(:no).trading_names
66
47
  end
67
48
  end
68
49
 
69
- describe 'factory :firm_with_advisers' do
70
- let(:factory) { :firm_with_advisers }
50
+ describe 'factory :firm_without_advisers' do
51
+ let(:factory) { :firm_without_advisers }
71
52
 
72
53
  specify 'expected status' do
73
54
  expect(subject).to be_persisted
74
55
  expect(subject).to be_valid
75
- expect(subject).to be_publishable
56
+ expect(subject).not_to be_publishable
76
57
  expect(subject).not_to be_trading_name
77
58
  expect(subject.primary_advice_method).to be(:local)
78
59
  end
@@ -80,7 +61,7 @@ RSpec.describe 'Firm factory' do
80
61
  specify 'associations' do
81
62
  expect(subject.principal).not_to be_present
82
63
  expect(subject).to have(1).offices
83
- expect(subject).to have(3).advisers
64
+ expect(subject).to have(0).advisers
84
65
  expect(subject).to have(:no).trading_names
85
66
  end
86
67
  end
@@ -99,7 +80,7 @@ RSpec.describe 'Firm factory' do
99
80
  specify 'associations' do
100
81
  expect(subject.principal).not_to be_present
101
82
  expect(subject).to have(3).offices
102
- expect(subject).to have(:no).advisers
83
+ expect(subject).to have(1).advisers
103
84
  expect(subject).to have(:no).trading_names
104
85
  end
105
86
  end
@@ -118,7 +99,7 @@ RSpec.describe 'Firm factory' do
118
99
  specify 'associations' do
119
100
  expect(subject.principal).not_to be_present
120
101
  expect(subject).to have(1).offices
121
- expect(subject).to have(:no).advisers
102
+ expect(subject).to have(1).advisers
122
103
 
123
104
  expect(subject).to have(3).trading_names
124
105
  expect(subject.trading_names).to all(have_attributes(fca_number: subject.fca_number))
@@ -146,7 +127,7 @@ RSpec.describe 'Firm factory' do
146
127
  # expect(subject.principal.firm).to eq(subject)
147
128
 
148
129
  expect(subject).to have(1).offices
149
- expect(subject).to have(:no).advisers
130
+ expect(subject).to have(1).advisers
150
131
  expect(subject).to have(:no).trading_names
151
132
  end
152
133
  end
@@ -165,7 +146,7 @@ RSpec.describe 'Firm factory' do
165
146
  specify 'associations' do
166
147
  expect(subject.principal).not_to be_present
167
148
  expect(subject).to have(1).offices
168
- expect(subject).to have(:no).advisers
149
+ expect(subject).to have(1).advisers
169
150
  expect(subject).to have(:no).trading_names
170
151
  end
171
152
  end
@@ -117,16 +117,65 @@ RSpec.describe Firm do
117
117
  end
118
118
 
119
119
  describe '#publishable?' do
120
- subject { create(:firm, offices_count: offices_count) }
120
+ let(:firm) { FactoryGirl.create(:firm) }
121
+ subject { firm.publishable? }
122
+
123
+ context 'when the firm is valid, has a main office and is not missing advisers' do
124
+ it { is_expected.to be_truthy }
125
+ end
126
+
127
+ context 'when the firm is not valid' do
128
+ let(:firm) do
129
+ FactoryGirl.create(:firm).tap do |f|
130
+ f.email_address = nil
131
+ f.save(validate: false)
132
+ end
133
+ end
134
+
135
+ it { is_expected.to be_falsey }
136
+ end
121
137
 
122
138
  context 'when the firm has no main office' do
123
- let(:offices_count) { 0 }
124
- it { expect(subject).not_to be_publishable }
139
+ let(:firm) { FactoryGirl.create(:firm, offices_count: 0) }
140
+
141
+ it { is_expected.to be_falsey }
125
142
  end
126
143
 
127
- context 'when the firm has a main office' do
128
- let(:offices_count) { 1 }
129
- it { expect(subject).to be_publishable }
144
+ context 'when the firm is missing advisers' do
145
+ before { allow(firm).to receive(:missing_advisers?).and_return(true) }
146
+
147
+ it { is_expected.to be_falsey }
148
+ end
149
+ end
150
+
151
+ describe '#missing_advisers?' do
152
+ let(:factory) { :firm }
153
+ subject { FactoryGirl.create(factory, advisers_count: advisers_count).missing_advisers? }
154
+
155
+ context 'when the firm offers face-to-face advice' do
156
+ context 'when the firm has advisers' do
157
+ let(:advisers_count) { 1 }
158
+ it { is_expected.to be_falsey }
159
+ end
160
+
161
+ context 'when the firm has no advisers' do
162
+ let(:advisers_count) { 0 }
163
+ it { is_expected.to be_truthy }
164
+ end
165
+ end
166
+
167
+ context 'when the firm offers remote advice' do
168
+ let(:factory) { :firm_with_remote_advice }
169
+
170
+ context 'when the firm has advisers' do
171
+ let(:advisers_count) { 1 }
172
+ it { is_expected.to be_falsey }
173
+ end
174
+
175
+ context 'when the firm has no advisers' do
176
+ let(:advisers_count) { 0 }
177
+ it { is_expected.to be_falsey }
178
+ end
130
179
  end
131
180
  end
132
181
 
@@ -395,6 +444,7 @@ RSpec.describe Firm do
395
444
  it 'the firm is published' do
396
445
  allow(IndexFirmJob).to receive(:perform_later)
397
446
  firm = create :firm
447
+ firm.run_callbacks(:commit)
398
448
  expect(IndexFirmJob).to have_received(:perform_later).with(firm)
399
449
  end
400
450
  end
@@ -404,6 +454,7 @@ RSpec.describe Firm do
404
454
  firm = create :firm
405
455
  allow(IndexFirmJob).to receive(:perform_later)
406
456
  firm.update_attributes(email_address: 'bill@example.com')
457
+ firm.run_callbacks(:commit)
407
458
  expect(IndexFirmJob).to have_received(:perform_later).with(firm)
408
459
  end
409
460
  end
@@ -476,8 +527,9 @@ RSpec.describe Firm do
476
527
  firm.run_callbacks(:commit)
477
528
  end
478
529
 
479
- it 'does not trigger geocoding of the firm' do
530
+ it 'does not trigger geocoding/reindexing of the firm' do
480
531
  expect(GeocodeFirmJob).not_to receive(:perform_later)
532
+ expect(IndexFirmJob).not_to receive(:perform_later)
481
533
  adviser = firm.advisers.first
482
534
  office = firm.offices.first
483
535
  firm.destroy
@@ -325,7 +325,7 @@ RSpec.describe Principal do
325
325
 
326
326
  context 'when principal has a parent firm and a trading name' do
327
327
  let(:parent_firm) { principal.firm }
328
- let!(:trading_name) { create(:firm, registered_name: 'cabbage', parent_id: parent_firm.id, fca_number: principal.fca_number) }
328
+ let!(:trading_name) { create(:firm_without_advisers, registered_name: 'cabbage', parent_id: parent_firm.id, fca_number: principal.fca_number) }
329
329
 
330
330
  before :each do
331
331
  parent_firm.update_column(:email_address, 'acme@example.com')
@@ -1,6 +1,6 @@
1
1
  RSpec.describe FirmSerializer do
2
2
  let(:firm) do
3
- create(:firm, principal: create(:principal)) { |f| create(:adviser, firm: f) }
3
+ FactoryGirl.create(:firm_with_principal)
4
4
  end
5
5
 
6
6
  describe 'the serialized json' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mas-rad_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.79
4
+ version: 0.0.80
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Lovell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-09 00:00:00.000000000 Z
11
+ date: 2015-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails