Skip to content

Commit 147023b

Browse files
committed
Check if create, update or noop before publish, plus Rubocop fixes
1 parent fbb7baa commit 147023b

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

lib/roo_on_rails/routemaster/publisher.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(model, event, client: ::Routemaster::Client)
1313
end
1414

1515
def publish?
16-
true
16+
noop? || @model.new_record? || @model.previous_changes.any?
1717
end
1818

1919
def will_publish?
@@ -22,7 +22,7 @@ def will_publish?
2222

2323
def publish!
2424
return unless will_publish?
25-
@client.send(event, topic, url, data: stringify_keys(data))
25+
@client.send(@event, topic, url, data: stringify_keys(data))
2626
end
2727

2828
def topic
@@ -37,9 +37,9 @@ def data
3737
nil
3838
end
3939

40-
%i(created updated deleted noop).each do |event_type|
40+
%i[created updated deleted noop].each do |event_type|
4141
define_method :"#{event_type}?" do
42-
event.to_sym == event_type
42+
@event.to_sym == event_type
4343
end
4444
end
4545

spec/integration/routemaster_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
end
1414

1515
context 'if ROUTEMASTER_ENABLED is true' do
16-
let(:app_env_vars) { ["ROUTEMASTER_ENABLED=true", super()].join("\n") }
16+
let(:app_env_vars) { ['ROUTEMASTER_ENABLED=true', super()].join("\n") }
1717

1818
context 'and ROOBUS_URL/ROOBUS_UUID are not set' do
1919
it 'the app fails to load' do
20-
app.wait_log /Exiting/
20+
app.wait_log(/Exiting/)
2121
app.stop
2222
expect(app.status).not_to be_success
2323
end
2424

2525
it 'the app logs the failure' do
26-
app.wait_log /ROOBUS_URL and ROOBUS_UUID are required/
26+
app.wait_log(/ROOBUS_URL and ROOBUS_UUID are required/)
2727
end
2828
end
2929
end

spec/roo_on_rails/routemaster/lifecycle_events_spec.rb

+16-12
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,30 @@ def self.after_commit(*args)
2222
let(:subject_instance) { subject.new }
2323
let(:publisher_spy) { spy('publisher') }
2424

25-
describe "::publish_lifecycle_events" do
26-
context "when called without arguments" do
25+
describe '::publish_lifecycle_events' do
26+
context 'when called without arguments' do
2727
before { subject.publish_lifecycle_events }
2828

29-
it "adds three event hooks" do
29+
it 'adds three event hooks' do
3030
expect(subject.after_commit_hooks).to match_array([
3131
[:publish_lifecycle_event_on_create, { on: :create }],
3232
[:publish_lifecycle_event_on_update, { on: :update }],
3333
[:publish_lifecycle_event_on_destroy, { on: :destroy }]
3434
])
3535
end
3636

37-
describe "and calling a callback" do
37+
describe 'and calling a callback' do
3838
events_and_types = [
39-
%i(create created),
40-
%i(update updated),
41-
%i(destroy deleted)
39+
%i[create created],
40+
%i[update updated],
41+
%i[destroy deleted]
4242
]
4343

4444
events_and_types.each do |lifecycle_event|
4545
it "fetches a publisher for #{lifecycle_event.first}" do
46-
callback = subject.after_commit_hooks.detect { |event| event.last[:on] == lifecycle_event.first }.first
46+
callback = subject.after_commit_hooks.detect do |event|
47+
event.last[:on] == lifecycle_event.first
48+
end.first
4749

4850
allow(RooOnRails::Routemaster::Publishers).to receive(:for).with(subject, lifecycle_event.last) do
4951
[publisher_spy]
@@ -55,21 +57,23 @@ def self.after_commit(*args)
5557
end
5658
end
5759

58-
it "defines all three lifecycle events and noop on an instance" do
59-
expect {
60+
it 'defines all three lifecycle events and noop on an instance' do
61+
expect do
6062
subject_instance.method(:publish_lifecycle_event_on_create)
6163
subject_instance.method(:publish_lifecycle_event_on_update)
6264
subject_instance.method(:publish_lifecycle_event_on_destroy)
6365
subject_instance.method(:publish_lifecycle_event_on_noop)
64-
}.to_not raise_error
66+
end.to_not raise_error
6567
end
6668
end
6769

6870
context "when called with a 'create' lifecycle event" do
6971
before { subject.publish_lifecycle_events(:create) }
7072

7173
it "adds a 'create' hook only" do
72-
expect(subject.after_commit_hooks).to match_array([[:publish_lifecycle_event_on_create, { on: :create }]])
74+
expect(subject.after_commit_hooks).to match_array([
75+
[:publish_lifecycle_event_on_create, { on: :create }]
76+
])
7377
end
7478
end
7579
end

spec/roo_on_rails/routemaster/publisher_spec.rb

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,30 @@
1717

1818
before do
1919
allow(publisher).to receive_messages(
20-
url: "https://deliveroo.test/url",
21-
data: { test_key_1: "Test value 1", test_key_2: "Test value 2" }
20+
url: 'https://deliveroo.test/url',
21+
data: { test_key_1: 'Test value 1', test_key_2: 'Test value 2' }
2222
)
2323
end
2424

2525
it 'should publish an event to Routemaster fine' do
2626
expect(::Routemaster::Client).to receive(:send).with(
2727
:noop,
28-
"test_models",
29-
"https://deliveroo.test/url",
30-
{ data: {
31-
"test_key_1" => "Test value 1",
32-
"test_key_2" => "Test value 2"
33-
}}
28+
'test_models',
29+
'https://deliveroo.test/url',
30+
data: {
31+
'test_key_1' => 'Test value 1',
32+
'test_key_2' => 'Test value 2'
33+
}
3434
)
3535
publisher.publish!
3636
end
3737

3838
it 'should have a topic named after the model class' do
39-
expect(publisher.topic).to eq("test_models")
39+
expect(publisher.topic).to eq('test_models')
4040
end
4141

4242
it 'should have the correct URL' do
43-
expect(publisher.url).to eq("https://deliveroo.test/url")
43+
expect(publisher.url).to eq('https://deliveroo.test/url')
4444
end
4545

4646
it 'should have the correct event type' do

0 commit comments

Comments
 (0)