Skip to content

Commit

Permalink
Enqueue PushNotification background job on Post persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
enricostano committed May 28, 2018
1 parent 3799a7c commit 9cff2ec
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
12 changes: 10 additions & 2 deletions app/services/persister/post_persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def save
::ActiveRecord::Base.transaction do
post.save!
create_save_event!
enqueue_push_notification_job!
post
end
rescue ActiveRecord::RecordInvalid => _exception
Expand All @@ -20,6 +21,7 @@ def update_attributes(params)
::ActiveRecord::Base.transaction do
post.update_attributes!(params)
create_update_event!
enqueue_push_notification_job!
post
end
rescue ActiveRecord::RecordInvalid => _exception
Expand All @@ -28,12 +30,18 @@ def update_attributes(params)

private

attr_accessor :event

def create_save_event!
::Event.create! action: :created, post: post
@event = ::Event.create! action: :created, post: post
end

def create_update_event!
::Event.create! action: :updated, post: post
@event = ::Event.create! action: :updated, post: post
end

def enqueue_push_notification_job!
CreatePushNotificationsJob.perform_later(event_id: event.id)
end
end
end
43 changes: 35 additions & 8 deletions spec/services/persister/post_persister_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,57 @@
)
end
let(:persister) { ::Persister::PostPersister.new(post) }
let(:event) { Fabricate.build(:event, id: 27) }

describe '#save' do
before { persister.save }

it 'saves the post' do
persister.save

expect(post).to be_persisted
end

# TODO: write better expectation
it 'creates an event' do
expect(Event.where(post_id: post.id).first.action).to eq('created')
expect(::Event).to receive(:create!).with(action: :created, post: post).and_return(event)

persister.save
end

context 'background job' do
before do
ActiveJob::Base.queue_adapter = :test
allow(::Event).to receive(:create!).and_return(event)
persister.save
end

it 'enqueues a CreatePushNotificationsJob background job' do
expect(CreatePushNotificationsJob).to have_been_enqueued.with(event_id: 27)
end
end
end

describe '#update_attributes' do
before { persister.update_attributes(title: 'New title') }

it 'updates the resource attributes' do
persister.update_attributes(title: 'New title')

expect(post.title).to eq('New title')
end

# TODO: write better expectation
it 'creates an event' do
expect(Event.where(post_id: post.id).first.action).to eq('updated')
expect(::Event).to receive(:create!).with(action: :updated, post: post).and_return(event)

persister.update_attributes(title: 'New title')
end

context 'background job' do
before do
ActiveJob::Base.queue_adapter = :test
allow(::Event).to receive(:create!).and_return(event)
persister.update_attributes(title: 'New title')
end

it 'enqueues a CreatePushNotificationsJob background job' do
expect(CreatePushNotificationsJob).to have_been_enqueued.with(event_id: 27)
end
end
end
end

0 comments on commit 9cff2ec

Please # to comment.