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 | |
| parent | 1388b346450b975703b7890eb87505e975713c22 (diff) | |
add status to license and fix broken jbuilder spec.
| -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 | ||||
| -rw-r--r-- | app/views/v1/company_licenses/index.jbuilder | 6 | ||||
| -rw-r--r-- | app/views/v1/licenses/_license.json.jbuilder | 2 | ||||
| -rw-r--r-- | spec/models/license_spec.rb | 18 | ||||
| -rw-r--r-- | spec/views/v1/company_licenses/index.json.jbuilder_spec.rb | 37 | ||||
| -rw-r--r-- | spec/views/v1/licenses/show.json.jbuilder_spec.rb | 4 |
10 files changed, 74 insertions, 15 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 diff --git a/app/views/v1/company_licenses/index.jbuilder b/app/views/v1/company_licenses/index.jbuilder index db62113..8228f3e 100644 --- a/app/views/v1/company_licenses/index.jbuilder +++ b/app/views/v1/company_licenses/index.jbuilder @@ -1,5 +1,7 @@ -json.array! @licenses do |license| - json.partial! 'v1/licenses/license', license: license +json.licenses do + json.array! @licenses do |license| + json.partial! 'v1/licenses/license', license: license + end end json.partial! 'v1/shared/well_types', well_types: @well_types json.partial! 'v1/shared/license_statuses', license_statuses: @license_statuses diff --git a/app/views/v1/licenses/_license.json.jbuilder b/app/views/v1/licenses/_license.json.jbuilder index 33109d8..c66fb3f 100644 --- a/app/views/v1/licenses/_license.json.jbuilder +++ b/app/views/v1/licenses/_license.json.jbuilder @@ -1,5 +1,5 @@ json.id license.id -json.status license.status +json.status license.status.to_s json.partial! 'v1/licenses/location', location: license.location if license.confidential? json.partial! 'v1/licenses/confidential_well_type', well_type: license.well_type diff --git a/spec/models/license_spec.rb b/spec/models/license_spec.rb index 7df556e..1093d17 100644 --- a/spec/models/license_spec.rb +++ b/spec/models/license_spec.rb @@ -67,4 +67,22 @@ describe License do licenses.should include(confidential_license) end end + + describe "#status" do + let!(:active_license) { License.new(issued_at: 1.day.ago, expired_at: 1.day.from_now) } + let!(:expired_license) { License.new(issued_at: 2.days.ago, expired_at: 1.day.ago ) } + let!(:confidential_license) { License.new(issued_at: 2.days.from_now, expired_at: 3.days.from_now, confidential: true) } + + it "returns an active status" do + active_license.status.should == LicenseStatus::ACTIVE + end + + it "returns an expired status" do + expired_license.status.should == LicenseStatus::EXPIRED + end + + it "returns an confidential status" do + confidential_license.status.should == LicenseStatus::CONFIDENTIAL + end + end end diff --git a/spec/views/v1/company_licenses/index.json.jbuilder_spec.rb b/spec/views/v1/company_licenses/index.json.jbuilder_spec.rb index 6d85141..90eefb9 100644 --- a/spec/views/v1/company_licenses/index.json.jbuilder_spec.rb +++ b/spec/views/v1/company_licenses/index.json.jbuilder_spec.rb @@ -9,33 +9,50 @@ describe 'v1/company_licenses/index' do before :each do assign(:licenses, [license]) + assign(:well_types, WellType::ALL) + assign(:license_statuses, LicenseStatus::ALL) render end let(:result) { JSON.parse(rendered) } it "includes the license id" do - result.first["id"].should == license.id + result["licenses"].first["id"].should == license.id end it "includes the license date range" do - result.first["issued_at"].should == license.issued_at.to_s - result.first["expired_at"].should == license.expired_at.to_s + result["licenses"].first["issued_at"].should == license.issued_at.to_s + result["licenses"].first["expired_at"].should == license.expired_at.to_s end it "includes the company information" do - result.first["company"]["name"].should == license.company.name + result["licenses"].first["company"]["name"].should == license.company.name end it "includes information on the type of well" do - result.first["well_type"]["id"].should == well_type.id - result.first["well_type"]["acronym"].should == well_type.acronym - result.first["well_type"]["name"].should == well_type.name + result["licenses"].first["well_type"]["id"].should == well_type.id + result["licenses"].first["well_type"]["acronym"].should == well_type.acronym + result["licenses"].first["well_type"]["name"].should == well_type.name end it "includes location information" do - result.first["location"]["latitude"].should == location.latitude - result.first["location"]["longitude"].should == location.longitude - result.first["location"]["township"].should == location.township + result["licenses"].first["location"]["latitude"].should == location.latitude + result["licenses"].first["location"]["longitude"].should == location.longitude + result["licenses"].first["location"]["township"].should == location.township + end + + it "includes all the well types" do + WellType::ALL.each do |well_type| + row = result["well_types"].find { |x| x['id'] == well_type.id } + row['acronym'].should == well_type.acronym + row['name'].should == well_type.name + end + end + + it "includes all the license statuses" do + LicenseStatus::ALL.each do |status| + row = result["license_statuses"].find { |x| x['name'] == status.to_s } + row.should_not be_nil + end end end diff --git a/spec/views/v1/licenses/show.json.jbuilder_spec.rb b/spec/views/v1/licenses/show.json.jbuilder_spec.rb index 404a514..f000674 100644 --- a/spec/views/v1/licenses/show.json.jbuilder_spec.rb +++ b/spec/views/v1/licenses/show.json.jbuilder_spec.rb @@ -10,7 +10,7 @@ describe 'v1/licenses/show' do let(:public_license) { License.new(id: SecureRandom.uuid, company: company, applicant: user, location: location, issued_at: 2.days.ago, expired_at: 1.day.from_now, well_type: well_type) } before :each do - public_license.stub(:status).and_return('active') + public_license.stub(:status).and_return(LicenseStatus::ACTIVE) assign(:license, public_license) render end @@ -52,7 +52,7 @@ describe 'v1/licenses/show' do let(:confidential_license) { License.new(confidential: true, company: company, applicant: user, location: location, well_type: well_type) } before :each do - confidential_license.stub(:status).and_return('confidential') + confidential_license.stub(:status).and_return(LicenseStatus::CONFIDENTIAL) assign(:license, confidential_license) render end |
