Skip to content

Commit

Permalink
[CPDNPQ-2142] Add validation when marking Statement as paid
Browse files Browse the repository at this point in the history
  • Loading branch information
jebw committed Nov 6, 2024
1 parent 9c7cb1b commit 476d424
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
5 changes: 4 additions & 1 deletion app/models/statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ class Statement < ApplicationRecord
state_machine :state, initial: :open do
state :open
state :payable
state :paid

state :paid do
validates :marked_as_paid_at, presence: true
end

event :mark_payable do
transition [:open] => :payable
Expand Down
22 changes: 17 additions & 5 deletions spec/models/statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,24 @@
end

context "when from payable to paid" do
let(:statement) { create(:statement, :payable, marked_as_paid_at: 1.week.ago) }
let(:statement) { create(:statement, :payable) }

it "transitions state to payable" do
expect(statement).to be_payable
statement.mark_paid!
expect(statement).to be_paid
context "with mark_as_paid_at set" do
before { statement.update(marked_as_paid_at: Time.zone.now) }

it "transitions state to payable" do
expect(statement).to be_payable
statement.mark_paid!
expect(statement).to be_paid
end
end

context "without mark_as_paid_at being set" do
it "raises an error" do
expect(statement.reload).to be_payable
expect { statement.mark_paid! }.to raise_error(StateMachines::InvalidTransition)
expect(statement.reload).to be_payable
end
end
end

Expand Down

0 comments on commit 476d424

Please sign in to comment.