Tweak specs, implementation properly

This commit is contained in:
Daniel O'Connor
2016-04-09 22:54:03 +09:30
parent 5cfa051d75
commit 3644a8124f
2 changed files with 6 additions and 6 deletions

View File

@@ -109,17 +109,17 @@ class Planting < ActiveRecord::Base
end
end
def planted?(current_date = DateTime.now)
def planted?(current_date = Date.today)
planted_at.present? && current_date.to_date >= planted_at
end
def percentage_grown(current_date = DateTime.now)
def percentage_grown(current_date = Date.today)
return nil unless days_before_maturity && planted?(current_date)
days = (current_date.to_date - planted_at.to_date).to_i
return 0 if current_date < planted_at
return 100 if days > days_before_maturity
days = current_date - planted_at
percent = (days/days_before_maturity*100).to_i
if percent >= 100

View File

@@ -57,13 +57,13 @@ describe Planting do
now_earlier_than_planting = 2.days.ago
@planting.percentage_grown(now_earlier_than_planting).should be 0
@planting.percentage_grown(now_earlier_than_planting).should be nil
end
it 'should reflect the current growth' do
@planting = FactoryGirl.build(:planting, days_before_maturity: 10, planted_at: 4.days.ago)
@planting.percentage_grown.should be 40
@planting.percentage_grown(Date.today).should be 40
end
it 'should not be calculated for unplanted plantings' do