diff options
| author | mo khan <mo@mokhan.ca> | 2014-02-21 07:04:37 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-02-21 07:04:37 -0700 |
| commit | 5d937942170c81bcb2304cab1c71eefda48e4e35 (patch) | |
| tree | f46cb5f947c15ec42e2c616ccd2e74adc26e773f | |
| parent | bd1b2145347c25ee6fba54e306d5e11749e511d0 (diff) | |
move status filtering behaviour to separate status classes.
| -rw-r--r-- | app/models/active_status.rb | 11 | ||||
| -rw-r--r-- | app/models/company.rb | 6 | ||||
| -rw-r--r-- | app/models/expired_status.rb | 10 | ||||
| -rw-r--r-- | app/models/license_status.rb | 7 | ||||
| -rw-r--r-- | spec/models/company_spec.rb | 17 |
5 files changed, 42 insertions, 9 deletions
diff --git a/app/models/active_status.rb b/app/models/active_status.rb new file mode 100644 index 0000000..efb2db4 --- /dev/null +++ b/app/models/active_status.rb @@ -0,0 +1,11 @@ +class ActiveStatus + def initialize(name) + @name = name + end + + def filter(licenses) + today = DateTime.now + licenses.where('issued_at < ? AND expired_at > ?', today, today) + end +end + diff --git a/app/models/company.rb b/app/models/company.rb index 0959d4d..7233020 100644 --- a/app/models/company.rb +++ b/app/models/company.rb @@ -2,7 +2,9 @@ class Company < ActiveRecord::Base has_many :licenses def status(status) - today = DateTime.now - licenses.where('issued_at < ? AND expired_at > ?', today, today) + #today = DateTime.now + #licenses.where('issued_at < ? AND expired_at > ?', today, today) + + status.filter(licenses) end end diff --git a/app/models/expired_status.rb b/app/models/expired_status.rb new file mode 100644 index 0000000..11ee7f7 --- /dev/null +++ b/app/models/expired_status.rb @@ -0,0 +1,10 @@ +class ExpiredStatus + def initialize(name) + @name = name + end + + def filter(licenses) + today = DateTime.now + licenses.where('expired_at < ?', today) + end +end diff --git a/app/models/license_status.rb b/app/models/license_status.rb index e22a71c..1c5833f 100644 --- a/app/models/license_status.rb +++ b/app/models/license_status.rb @@ -1,6 +1,7 @@ -module LicenseStatus - ACTIVE="active" - ALL=[ACTIVE] +class LicenseStatus + ACTIVE=ActiveStatus.new("active") + EXPIRED=ExpiredStatus.new("expired") + ALL=[ACTIVE, EXPIRED] def self.find_match(status) ACTIVE diff --git a/spec/models/company_spec.rb b/spec/models/company_spec.rb index f9e0a61..4b5f7ce 100644 --- a/spec/models/company_spec.rb +++ b/spec/models/company_spec.rb @@ -4,14 +4,23 @@ describe Company do describe ".status" do let(:company) { Company.create } let(:today) { DateTime.now } - let(:active_license) { company.licenses.create(issued_at: 1.day.ago, expired_at: 1.day.from_now) } - let(:expired_license) { company.licenses.create(issued_at: 2.days.ago, expired_at: 1.day.ago ) } - let(:pending_license) { company.licenses.create(issued_at: 2.days.from_now, expired_at: 3.days.from_now ) } + let!(:active_license) { company.licenses.create(issued_at: 1.day.ago, expired_at: 1.day.from_now) } + let!(:expired_license) { company.licenses.create(issued_at: 2.days.ago, expired_at: 1.day.ago ) } + let!(:pending_license) { company.licenses.create(issued_at: 2.days.from_now, expired_at: 3.days.from_now ) } it "returns all the licenses that are active" do licenses = company.status(LicenseStatus::ACTIVE) - licenses.count.should == 0 + licenses.count.should == 1 licenses.should include(active_license) end + + it "returns all expired licenses" do + licenses = company.status(LicenseStatus::EXPIRED) + licenses.count.should == 1 + licenses.should include(expired_license) + end + + pending "returns all confidential licenses" do + end end end |
