Skip to content

Commit

Permalink
Merge b501bb9 into 30bf0ca
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-sully committed Apr 23, 2023
2 parents 30bf0ca + b501bb9 commit 5a4c030
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/caffeinate/campaign_subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def ended?
# Updates `ended_at` and runs `on_complete` callbacks
def end!(reason = ::Caffeinate.config.default_ended_reason)
raise ::Caffeinate::InvalidState, 'CampaignSubscription is already unsubscribed.' if unsubscribed?
return true if ended?

update!(ended_at: ::Caffeinate.config.time_now, ended_reason: reason)

Expand All @@ -109,6 +110,7 @@ def end!(reason = ::Caffeinate.config.default_ended_reason)
# Updates `ended_at` and runs `on_complete` callbacks
def end(reason = ::Caffeinate.config.default_ended_reason)
return false if unsubscribed?
return true if ended?

result = update(ended_at: ::Caffeinate.config.time_now, ended_reason: reason)

Expand Down
9 changes: 9 additions & 0 deletions spec/models/caffeinate/campaign_subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
end

describe '#end!' do
context 'when already ended' do
it 'no-ops on subsequent #end! calls' do
subscription.end!
first_ended_at = subscription.ended_at
expect(subscription).to_not receive(:update!)
subscription.end!
expect(first_ended_at).to eq subscription.ended_at
end
end
context 'without argument' do
it 'is not ended' do
expect(subscription).not_to be_ended
Expand Down

0 comments on commit 5a4c030

Please sign in to comment.