Permalink
Browse files

Handle nil dates on Warranty model

  • Loading branch information...
1 parent a406a16 commit 14e8e0ab5044bddac634d5184befd06524650b30 @jnraine committed Nov 20, 2013
Showing with 40 additions and 3 deletions.
  1. +16 −0 app/models/warranty.rb
  2. +3 −3 app/views/admin_mailer/warranty_notification.html.erb
  3. +21 −0 spec/models/warranty_spec.rb
View
16 app/models/warranty.rb
@@ -172,4 +172,20 @@ def last_notice_before(date)
@last_notice.nil? or @last_notice < date
end
+ def readable_purchase_date
+ readable_date purchase_date
+ end
+
+ def readable_updated_at
+ readable_date updated_at
+ end
+
+ def readable_hw_coverage_end_date
+ readable_date hw_coverage_end_date
+ end
+
+ def readable_date(date)
+ return "Unknown" unless date
+ date.to_s(:readable_date)
+ end
end
View
6 app/views/admin_mailer/warranty_notification.html.erb
@@ -1,6 +1,6 @@
<h3><%= @computer %> - hardware warranty will expire on <%= @computer.warranty_expiry_date %></h3>
<% if @computer.warranty.app_eligible %>
- <p><em>This computer is eligible for AppleCare Protection Plan until <%= @computer.warranty.hw_coverage_end_date.to_s(:readable_date) %>.</em></p>
+ <p><em>This computer is eligible for AppleCare Protection Plan until <%= @computer.warranty.readable_hw_coverage_end_date %>.</em></p>
<% elsif @computer.warranty.app_registered %>
<p><em>This computer is registered for AppleCare Protection Plan.</em></p>
<% else %>
@@ -25,11 +25,11 @@
</tr>
<tr>
<td><strong>Purchase Date</strong></td>
- <td><%= @computer.warranty.purchase_date.to_s(:readable_date) %></td>
+ <td><%= @computer.warranty.readable_purchase_date %></td>
</tr>
<tr>
<td><strong>Last updated</strong></td>
- <td><%= @computer.warranty.updated_at.to_s(:readable_date) %></td>
+ <td><%= @computer.warranty.readable_updated_at %></td>
</tr>
</table>
<br />
View
21 spec/models/warranty_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+describe Warranty do
+ describe "readable dates" do
+ it "returns readable dates when they are set" do
+ warranty = Warranty.new.tap do |w|
+ w.purchase_date = Time.parse("January 1, 2000")
+ w.updated_at = Time.parse("January 1, 2000")
+ end
+
+ warranty.readable_purchase_date.should == "January 01, 2000"
+ warranty.readable_updated_at.should == "January 01, 2000"
+ end
+
+ it "returns 'unknown' when attributes are nil" do
+ warranty = Warranty.new
+ warranty.readable_purchase_date.should == "Unknown"
+ warranty.readable_updated_at.should == "Unknown"
+ end
+ end
+end

0 comments on commit 14e8e0a

Please sign in to comment.