Commit 580ce4f2d85f89e81825c3b5993fd3ee68f4d93a
1 parent
232389f4
Exists in
master
and in
4 other branches
Minor cleanup to Milestone model and spec
Back-ported from my still-in-progress major cleanup.
Showing
2 changed files
with
21 additions
and
20 deletions
Show diff stats
app/models/milestone.rb
| ... | ... | @@ -28,17 +28,9 @@ class Milestone < ActiveRecord::Base |
| 28 | 28 | end |
| 29 | 29 | |
| 30 | 30 | def percent_complete |
| 31 | - @percent_complete ||= begin | |
| 32 | - total_i = self.issues.count | |
| 33 | - closed_i = self.issues.closed.count | |
| 34 | - if total_i > 0 | |
| 35 | - (closed_i * 100) / total_i | |
| 36 | - else | |
| 37 | - 100 | |
| 38 | - end | |
| 39 | - rescue => ex | |
| 40 | - 0 | |
| 41 | - end | |
| 31 | + ((self.issues.closed.count * 100) / self.issues.count).abs | |
| 32 | + rescue ZeroDivisionError | |
| 33 | + 100 | |
| 42 | 34 | end |
| 43 | 35 | |
| 44 | 36 | def expires_at | ... | ... |
spec/models/milestone_spec.rb
| ... | ... | @@ -31,24 +31,33 @@ describe Milestone do |
| 31 | 31 | |
| 32 | 32 | it { milestone.should be_valid } |
| 33 | 33 | |
| 34 | - describe "Issues" do | |
| 35 | - before do | |
| 34 | + describe "#percent_complete" do | |
| 35 | + it "should not count open issues" do | |
| 36 | 36 | milestone.issues << issue |
| 37 | + milestone.percent_complete.should == 0 | |
| 37 | 38 | end |
| 38 | 39 | |
| 39 | - it { milestone.percent_complete.should == 0 } | |
| 40 | + it "should count closed issues" do | |
| 41 | + issue.update_attributes(closed: true) | |
| 42 | + milestone.issues << issue | |
| 43 | + milestone.percent_complete.should == 100 | |
| 44 | + end | |
| 40 | 45 | |
| 41 | - it do | |
| 42 | - issue.update_attributes closed: true | |
| 46 | + it "should recover from dividing by zero" do | |
| 47 | + milestone.issues.should_receive(:count).and_return(0) | |
| 43 | 48 | milestone.percent_complete.should == 100 |
| 44 | 49 | end |
| 45 | 50 | end |
| 46 | 51 | |
| 47 | - describe :expires_at do | |
| 48 | - before do | |
| 49 | - milestone.update_attributes due_date: Date.today + 1.day | |
| 52 | + describe "#expires_at" do | |
| 53 | + it "should be nil when due_date is unset" do | |
| 54 | + milestone.update_attributes(due_date: nil) | |
| 55 | + milestone.expires_at.should be_nil | |
| 50 | 56 | end |
| 51 | 57 | |
| 52 | - it { milestone.expires_at.should_not be_nil } | |
| 58 | + it "should not be nil when due_date is set" do | |
| 59 | + milestone.update_attributes(due_date: Date.tomorrow) | |
| 60 | + milestone.expires_at.should be_present | |
| 61 | + end | |
| 53 | 62 | end |
| 54 | 63 | end | ... | ... |