mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Reduce cognitive complexity of StandingOrder#state
This commit is contained in:
@@ -50,14 +50,22 @@ class StandingOrder < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def state
|
||||
return 'canceled' if canceled?
|
||||
return 'paused' if paused?
|
||||
return nil unless begins_at
|
||||
if begins_at > Time.zone.now
|
||||
'pending'
|
||||
else
|
||||
return 'ended' if ends_at.andand < Time.zone.now
|
||||
'active'
|
||||
# NOTE: the order is important here
|
||||
%w(canceled paused pending ended).each do |state|
|
||||
return state if send("#{state}?")
|
||||
end
|
||||
"active"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def pending?
|
||||
return true unless begins_at
|
||||
begins_at > Time.zone.now
|
||||
end
|
||||
|
||||
def ended?
|
||||
return false unless ends_at
|
||||
ends_at < Time.zone.now
|
||||
end
|
||||
end
|
||||
|
||||
@@ -79,8 +79,8 @@ describe StandingOrder, type: :model do
|
||||
context "and the standing order has no begins_at date" do
|
||||
before { allow(standing_order).to receive(:begins_at) { nil } }
|
||||
|
||||
it "returns nil" do
|
||||
expect(standing_order.state).to be nil
|
||||
it "returns 'pending'" do
|
||||
expect(standing_order.state).to eq 'pending'
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user