Minor cleanup to Milestone model and spec
Back-ported from my still-in-progress major cleanup.
This commit is contained in:
parent
232389f4e8
commit
580ce4f2d8
2 changed files with 21 additions and 20 deletions
|
@ -28,17 +28,9 @@ class Milestone < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def percent_complete
|
def percent_complete
|
||||||
@percent_complete ||= begin
|
((self.issues.closed.count * 100) / self.issues.count).abs
|
||||||
total_i = self.issues.count
|
rescue ZeroDivisionError
|
||||||
closed_i = self.issues.closed.count
|
100
|
||||||
if total_i > 0
|
|
||||||
(closed_i * 100) / total_i
|
|
||||||
else
|
|
||||||
100
|
|
||||||
end
|
|
||||||
rescue => ex
|
|
||||||
0
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def expires_at
|
def expires_at
|
||||||
|
|
|
@ -31,24 +31,33 @@ describe Milestone do
|
||||||
|
|
||||||
it { milestone.should be_valid }
|
it { milestone.should be_valid }
|
||||||
|
|
||||||
describe "Issues" do
|
describe "#percent_complete" do
|
||||||
before do
|
it "should not count open issues" do
|
||||||
milestone.issues << issue
|
milestone.issues << issue
|
||||||
|
milestone.percent_complete.should == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
it { milestone.percent_complete.should == 0 }
|
it "should count closed issues" do
|
||||||
|
issue.update_attributes(closed: true)
|
||||||
|
milestone.issues << issue
|
||||||
|
milestone.percent_complete.should == 100
|
||||||
|
end
|
||||||
|
|
||||||
it do
|
it "should recover from dividing by zero" do
|
||||||
issue.update_attributes closed: true
|
milestone.issues.should_receive(:count).and_return(0)
|
||||||
milestone.percent_complete.should == 100
|
milestone.percent_complete.should == 100
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe :expires_at do
|
describe "#expires_at" do
|
||||||
before do
|
it "should be nil when due_date is unset" do
|
||||||
milestone.update_attributes due_date: Date.today + 1.day
|
milestone.update_attributes(due_date: nil)
|
||||||
|
milestone.expires_at.should be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it { milestone.expires_at.should_not be_nil }
|
it "should not be nil when due_date is set" do
|
||||||
|
milestone.update_attributes(due_date: Date.tomorrow)
|
||||||
|
milestone.expires_at.should be_present
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue