From b9316422d2eae1579fe0631531d375c11aadd2db Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 8 Dec 2016 11:43:21 +1100 Subject: [PATCH] Adding method to determine state of standing orders --- app/models/standing_order.rb | 12 +++++ spec/models/standing_order_spec.rb | 72 ++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/app/models/standing_order.rb b/app/models/standing_order.rb index 7ce10a3f75..89ddc91a7c 100644 --- a/app/models/standing_order.rb +++ b/app/models/standing_order.rb @@ -39,6 +39,18 @@ class StandingOrder < ActiveRecord::Base end end + def state + return 'canceled' if canceled_at + return 'paused' if paused_at + return nil unless begins_at + if begins_at > Time.zone.now + 'pending' + else + return 'ended' if ends_at.andand < Time.zone.now + 'active' + end + end + private def ends_at_after_begins_at diff --git a/spec/models/standing_order_spec.rb b/spec/models/standing_order_spec.rb index 71ac1702af..40e4fd7ee3 100644 --- a/spec/models/standing_order_spec.rb +++ b/spec/models/standing_order_spec.rb @@ -120,4 +120,76 @@ describe StandingOrder, type: :model do end end end + + describe "state" do + let(:standing_order) { StandingOrder.new } + + context "when the standing order has been cancelled" do + before { allow(standing_order).to receive(:canceled_at) { Time.zone.now } } + + it "returns 'canceled'" do + expect(standing_order.state).to eq 'canceled' + end + end + + context "when the standing order has not been cancelled" do + before { allow(standing_order).to receive(:canceled_at) { nil } } + + context "and the standing order has been paused" do + before { allow(standing_order).to receive(:paused_at) { Time.zone.now } } + + it "returns 'paused'" do + expect(standing_order.state).to eq 'paused' + end + end + + context "and the standing order has not been paused" do + before { allow(standing_order).to receive(:paused_at) { nil } } + + 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 + end + end + + context "and the standing order has a begins_at date in the future" do + before { allow(standing_order).to receive(:begins_at) { 1.minute.from_now } } + + it "returns 'pending'" do + expect(standing_order.state).to eq 'pending' + end + end + + context "and the standing order has a begins_at date in the past" do + before { allow(standing_order).to receive(:begins_at) { 1.minute.ago } } + + context "and the standing order has no ends_at date set" do + before { allow(standing_order).to receive(:ends_at) { nil } } + + it "returns 'active'" do + expect(standing_order.state).to eq 'active' + end + end + + context "and the standing order has an ends_at date in the future" do + before { allow(standing_order).to receive(:ends_at) { 1.minute.from_now } } + + it "returns 'active'" do + expect(standing_order.state).to eq 'active' + end + end + + context "and the standing order has an ends_at date in the past" do + before { allow(standing_order).to receive(:ends_at) { 1.minute.ago } } + + it "returns 'ended'" do + expect(standing_order.state).to eq 'ended' + end + end + end + end + end + end end