diff options
| author | mo khan <mo@mokhan.ca> | 2014-02-22 08:52:13 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-02-22 08:52:13 -0700 |
| commit | 7947414c935aa6a20a2de068b343df2a0f141384 (patch) | |
| tree | 46fc01930169f48a5debf42092baebb141bd18ba /app/models | |
| parent | 1388b346450b975703b7890eb87505e975713c22 (diff) | |
add status to license and fix broken jbuilder spec.
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/license.rb | 4 | ||||
| -rw-r--r-- | app/models/license_status.rb | 5 | ||||
| -rw-r--r-- | app/models/license_status/active.rb | 5 | ||||
| -rw-r--r-- | app/models/license_status/confidential.rb | 4 | ||||
| -rw-r--r-- | app/models/license_status/expired.rb | 4 |
5 files changed, 22 insertions, 0 deletions
diff --git a/app/models/license.rb b/app/models/license.rb index 2b4fc3c..12bb387 100644 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -4,6 +4,10 @@ class License < ActiveRecord::Base has_one :location belongs_to :applicant, class_name: 'User', foreign_key: 'user_id' + def status + LicenseStatus.status_for(self) + end + def self.most_recent(page: 1, per_page: 10) offset = (page - 1) * per_page offset = offset >= 0 ? offset : 0 diff --git a/app/models/license_status.rb b/app/models/license_status.rb index 41b54c5..d5da68f 100644 --- a/app/models/license_status.rb +++ b/app/models/license_status.rb @@ -2,10 +2,15 @@ class LicenseStatus ACTIVE=Active.new EXPIRED=Expired.new CONFIDENTIAL=Confidential.new + UNKNOWN=Object.new ALL=[ACTIVE, EXPIRED, CONFIDENTIAL] def self.find_match(status = "") ALL.find { |x| x.matches?("#{status}".downcase) } || ACTIVE end + + def self.status_for(license) + ALL.find { |x| x.best_represents?(license) } || UNKNOWN + end end diff --git a/app/models/license_status/active.rb b/app/models/license_status/active.rb index 864ee1a..f959efb 100644 --- a/app/models/license_status/active.rb +++ b/app/models/license_status/active.rb @@ -8,6 +8,11 @@ class LicenseStatus::Active to_s == name end + def best_represents?(license) + today = Date.today + license.issued_at < today && license.expired_at > today + end + def to_s 'active' end diff --git a/app/models/license_status/confidential.rb b/app/models/license_status/confidential.rb index d108491..b2354cd 100644 --- a/app/models/license_status/confidential.rb +++ b/app/models/license_status/confidential.rb @@ -7,6 +7,10 @@ class LicenseStatus::Confidential to_s == name end + def best_represents?(license) + license.confidential? + end + def to_s 'confidential' end diff --git a/app/models/license_status/expired.rb b/app/models/license_status/expired.rb index adbc3fb..8e24af9 100644 --- a/app/models/license_status/expired.rb +++ b/app/models/license_status/expired.rb @@ -8,6 +8,10 @@ class LicenseStatus::Expired to_s == name end + def best_represents?(license) + license.expired_at < Date.today + end + def to_s 'expired' end |
