Skip to content

Commit b4b4fcc

Browse files
committed
Enqueue PushNotification background job on Post persistence
1 parent 57350f9 commit b4b4fcc

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

app/services/persister/post_persister.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def save
1010
::ActiveRecord::Base.transaction do
1111
post.save!
1212
create_save_event!
13+
enqueue_push_notification_job!
1314
post
1415
end
1516
rescue ActiveRecord::RecordInvalid => _exception
@@ -20,6 +21,7 @@ def update_attributes(params)
2021
::ActiveRecord::Base.transaction do
2122
post.update_attributes!(params)
2223
create_update_event!
24+
enqueue_push_notification_job!
2325
post
2426
end
2527
rescue ActiveRecord::RecordInvalid => _exception
@@ -28,12 +30,18 @@ def update_attributes(params)
2830

2931
private
3032

33+
attr_accessor :event
34+
3135
def create_save_event!
32-
::Event.create! action: :created, post: post
36+
@event = ::Event.create! action: :created, post: post
3337
end
3438

3539
def create_update_event!
36-
::Event.create! action: :updated, post: post
40+
@event = ::Event.create! action: :updated, post: post
41+
end
42+
43+
def enqueue_push_notification_job!
44+
CreatePushNotificationsJob.perform_later(event_id: event.id)
3745
end
3846
end
3947
end

spec/services/persister/post_persister_spec.rb

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,57 @@
1414
)
1515
end
1616
let(:persister) { ::Persister::PostPersister.new(post) }
17+
let(:event) { Fabricate.build(:event, id: 27) }
1718

1819
describe '#save' do
19-
before { persister.save }
20-
2120
it 'saves the post' do
21+
persister.save
22+
2223
expect(post).to be_persisted
2324
end
2425

25-
# TODO: write better expectation
2626
it 'creates an event' do
27-
expect(Event.where(post_id: post.id).first.action).to eq('created')
27+
expect(::Event).to receive(:create!).with(action: :created, post: post).and_return(event)
28+
29+
persister.save
30+
end
31+
32+
context 'background job' do
33+
before do
34+
ActiveJob::Base.queue_adapter = :test
35+
allow(::Event).to receive(:create!).and_return(event)
36+
persister.save
37+
end
38+
39+
it 'enqueues a CreatePushNotificationsJob background job' do
40+
expect(CreatePushNotificationsJob).to have_been_enqueued.with(event_id: 27)
41+
end
2842
end
2943
end
3044

3145
describe '#update_attributes' do
32-
before { persister.update_attributes(title: 'New title') }
33-
3446
it 'updates the resource attributes' do
47+
persister.update_attributes(title: 'New title')
48+
3549
expect(post.title).to eq('New title')
3650
end
3751

38-
# TODO: write better expectation
3952
it 'creates an event' do
40-
expect(Event.where(post_id: post.id).first.action).to eq('updated')
53+
expect(::Event).to receive(:create!).with(action: :updated, post: post).and_return(event)
54+
55+
persister.update_attributes(title: 'New title')
56+
end
57+
58+
context 'background job' do
59+
before do
60+
ActiveJob::Base.queue_adapter = :test
61+
allow(::Event).to receive(:create!).and_return(event)
62+
persister.update_attributes(title: 'New title')
63+
end
64+
65+
it 'enqueues a CreatePushNotificationsJob background job' do
66+
expect(CreatePushNotificationsJob).to have_been_enqueued.with(event_id: 27)
67+
end
4168
end
4269
end
4370
end

0 commit comments

Comments
 (0)