From 5d937942170c81bcb2304cab1c71eefda48e4e35 Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 21 Feb 2014 07:04:37 -0700 Subject: move status filtering behaviour to separate status classes. --- app/models/active_status.rb | 11 +++++++++++ app/models/company.rb | 6 ++++-- app/models/expired_status.rb | 10 ++++++++++ app/models/license_status.rb | 7 ++++--- 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 app/models/active_status.rb create mode 100644 app/models/expired_status.rb (limited to 'app/models') 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 -- cgit v1.2.3