diff --git a/app/models/standing_order.rb b/app/models/standing_order.rb index 46ed235bfc..00a015b2c7 100644 --- a/app/models/standing_order.rb +++ b/app/models/standing_order.rb @@ -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 diff --git a/spec/models/standing_order_spec.rb b/spec/models/standing_order_spec.rb index 13694ce069..44ce77d2fd 100644 --- a/spec/models/standing_order_spec.rb +++ b/spec/models/standing_order_spec.rb @@ -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