mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-11 09:17:20 -04:00
Merge pull request #865 from CloCkWeRX/fix_progress
Fix progress calculations
This commit is contained in:
@@ -109,6 +109,26 @@ class Planting < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def planted?(current_date = Date.today)
|
||||
planted_at.present? && current_date.to_date >= planted_at
|
||||
end
|
||||
|
||||
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
|
||||
percent = (days/days_before_maturity*100).to_i
|
||||
|
||||
if percent >= 100
|
||||
percent = 100
|
||||
end
|
||||
|
||||
percent
|
||||
end
|
||||
|
||||
# return a list of interesting plantings, for the homepage etc.
|
||||
# we can't do this via a scope (as far as we know) so sadly we have to
|
||||
# do it this way.
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
- if (planting.planted_at.nil? || DateTime.now.to_date < planting.planted_at)
|
||||
- if !planting.planted?
|
||||
= "Progress: 0% - not planted yet"
|
||||
= render partial: "plantings/progress_bar", locals: {status: "warning", progress: "100%"}
|
||||
- elsif planting.finished?
|
||||
= "Progress: 100%"
|
||||
= render partial: "plantings/progress_bar", locals: {status: "success", progress: "100%"}
|
||||
- elsif planting.days_before_maturity.nil?
|
||||
= "Progress: 0% - Days before maturity unknown"
|
||||
= render partial: "plantings/progress_bar", locals: {status: "danger", progress: "100%"}
|
||||
= "Progress: Not calculated, days before maturity unknown"
|
||||
- else
|
||||
- if (percent = (((DateTime.now - planting.planted_at)/planting.days_before_maturity*100).to_i)) >= 100
|
||||
- percent = 100
|
||||
= "Progress: #{percent}%"
|
||||
= render partial: "plantings/progress_bar", locals: {status: "success", progress: "#{percent}%"}
|
||||
= "Progress: #{planting.percentage_grown}%"
|
||||
= render partial: "plantings/progress_bar", locals: {status: "success", progress: "#{planting.percentage_grown}%"}
|
||||
|
||||
@@ -38,4 +38,4 @@
|
||||
%dd= "#{display_days_before_maturity(planting)}"
|
||||
|
||||
.col-xs-9.col-md-8
|
||||
= render partial: 'plantings/planting_progress', locals: {:planting => planting}
|
||||
= render partial: 'plantings/planting_progress', locals: {planting: planting}
|
||||
|
||||
@@ -42,7 +42,7 @@ feature "Planting a crop", :js do
|
||||
end
|
||||
|
||||
expect(page).to have_content "Planting was successfully created"
|
||||
expect(page).to have_content "Progress: 0% - Days before maturity unknown"
|
||||
expect(page).to have_content "Progress: Not calculated, days before maturity unknown"
|
||||
end
|
||||
|
||||
scenario "Clicking link to owner's profile" do
|
||||
@@ -53,16 +53,19 @@ feature "Planting a crop", :js do
|
||||
|
||||
describe "Progress bar status on planting creation" do
|
||||
before do
|
||||
DateTime.stub(:now) { DateTime.new(2015, 10, 20, 10, 34) }
|
||||
login_as member
|
||||
visit new_planting_path
|
||||
|
||||
@a_past_date = 15.days.ago.strftime("%Y-%m-%d")
|
||||
@right_now = Date.today.strftime("%Y-%m-%d")
|
||||
@a_future_date = 1.years.from_now.strftime("%Y-%m-%d")
|
||||
end
|
||||
|
||||
it "should show that it is not planted yet" do
|
||||
fill_autocomplete "crop", with: "mai"
|
||||
select_from_autocomplete "maize"
|
||||
within "form#new_planting" do
|
||||
fill_in "When", with: "2015-12-15"
|
||||
fill_in "When", with: @a_future_date
|
||||
fill_in "How many?", with: 42
|
||||
select "cutting", from: "Planted from:"
|
||||
select "semi-shade", from: "Sun or shade?"
|
||||
@@ -78,7 +81,7 @@ feature "Planting a crop", :js do
|
||||
fill_autocomplete "crop", with: "mai"
|
||||
select_from_autocomplete "maize"
|
||||
within "form#new_planting" do
|
||||
fill_in "When", with: "2015-9-15"
|
||||
fill_in "When", with: @a_past_date
|
||||
fill_in "How many?", with: 42
|
||||
select "cutting", from: "Planted from:"
|
||||
select "semi-shade", from: "Sun or shade?"
|
||||
@@ -87,7 +90,7 @@ feature "Planting a crop", :js do
|
||||
end
|
||||
|
||||
expect(page).to have_content "Planting was successfully created"
|
||||
expect(page).to have_content "Progress: 0% - Days before maturity unknown"
|
||||
expect(page).to have_content "Progress: Not calculated, days before maturity unknown"
|
||||
expect(page).to have_content "Days until maturity: unknown"
|
||||
end
|
||||
|
||||
@@ -95,25 +98,25 @@ feature "Planting a crop", :js do
|
||||
fill_autocomplete "crop", with: "mai"
|
||||
select_from_autocomplete "maize"
|
||||
within "form#new_planting" do
|
||||
fill_in "When", with: "2015-10-15"
|
||||
fill_in "When", with: @right_now
|
||||
fill_in "How many?", with: 42
|
||||
select "cutting", from: "Planted from:"
|
||||
select "semi-shade", from: "Sun or shade?"
|
||||
fill_in "Tell us more about it", with: "It's rad."
|
||||
fill_in "Finished date", with: "2015-10-30"
|
||||
fill_in "Finished date", with: @a_future_date
|
||||
click_button "Save"
|
||||
end
|
||||
|
||||
expect(page).to have_content "Planting was successfully created"
|
||||
expect(page).to_not have_content "Progress: 0% - not planted yet"
|
||||
expect(page).to_not have_content "Progress: 0% - Days before maturity unknown"
|
||||
expect(page).to_not have_content "Progress: Not calculated, days before maturity unknown"
|
||||
end
|
||||
|
||||
it "should show that planting is 100% complete (no date specified)" do
|
||||
fill_autocomplete "crop", with: "mai"
|
||||
select_from_autocomplete "maize"
|
||||
within "form#new_planting" do
|
||||
fill_in "When", with: "2015-10-15"
|
||||
fill_in "When", with: @right_now
|
||||
fill_in "How many?", with: 42
|
||||
select "cutting", from: "Planted from:"
|
||||
select "semi-shade", from: "Sun or shade?"
|
||||
@@ -132,12 +135,12 @@ feature "Planting a crop", :js do
|
||||
fill_autocomplete "crop", with: "mai"
|
||||
select_from_autocomplete "maize"
|
||||
within "form#new_planting" do
|
||||
fill_in "When", with: "2015-10-15"
|
||||
fill_in "When", with: @a_past_date
|
||||
fill_in "How many?", with: 42
|
||||
select "cutting", from: "Planted from:"
|
||||
select "semi-shade", from: "Sun or shade?"
|
||||
fill_in "Tell us more about it", with: "It's rad."
|
||||
fill_in "Finished date", with: "2015-10-19"
|
||||
fill_in "Finished date", with: @right_now
|
||||
click_button "Save"
|
||||
end
|
||||
|
||||
@@ -169,13 +172,13 @@ feature "Planting a crop", :js do
|
||||
|
||||
scenario "Editing a planting to fill in the finished date" do
|
||||
visit planting_path(planting)
|
||||
expect(page).to have_content "Progress: 0% - Days before maturity unknown"
|
||||
expect(page).to have_content "Progress: Not calculated, days before maturity unknown"
|
||||
click_link "Edit"
|
||||
check "finished"
|
||||
fill_in "Finished date", with: "2015-06-25"
|
||||
click_button "Save"
|
||||
expect(page).to have_content "Planting was successfully updated"
|
||||
expect(page).to_not have_content "Progress: 0% - Days before maturity unknown"
|
||||
expect(page).to_not have_content "Progress: Not calculated, days before maturity unknown"
|
||||
end
|
||||
|
||||
scenario "Marking a planting as finished" do
|
||||
|
||||
@@ -37,16 +37,62 @@ describe Planting do
|
||||
Planting.first.should eq @planting2
|
||||
end
|
||||
|
||||
describe '#planted?' do
|
||||
it "should be false for future plantings"
|
||||
it "should be false for never planted"
|
||||
it "should be false for future plantings"
|
||||
end
|
||||
|
||||
describe '#percentage_grown' do
|
||||
it 'should not be more than 100%' do
|
||||
@planting = FactoryGirl.build(:planting, days_before_maturity: 1, planted_at: 1.day.ago)
|
||||
|
||||
now_later_than_planting = 2.days.from_now
|
||||
|
||||
@planting.percentage_grown(now_later_than_planting).should be 100
|
||||
end
|
||||
|
||||
it 'should not be less than 0%' do
|
||||
@planting = FactoryGirl.build(:planting, days_before_maturity: 1, planted_at: 1.day.ago)
|
||||
|
||||
now_earlier_than_planting = 2.days.ago
|
||||
|
||||
@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(Date.today).should be 40
|
||||
end
|
||||
|
||||
it 'should not be calculated for unplanted plantings' do
|
||||
@planting = FactoryGirl.build(:planting, planted_at: nil)
|
||||
|
||||
@planting.planted?.should be false
|
||||
@planting.percentage_grown.should be nil
|
||||
end
|
||||
|
||||
it 'should not be calculated for plantings with an unknown days before maturity' do
|
||||
@planting = FactoryGirl.build(:planting, days_before_maturity: nil)
|
||||
|
||||
@planting.percentage_grown.should be nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'delegation' do
|
||||
it 'system name' do
|
||||
planting.crop_name.should eq planting.crop.name
|
||||
end
|
||||
|
||||
it 'wikipedia url' do
|
||||
planting.crop_en_wikipedia_url.should eq planting.crop.en_wikipedia_url
|
||||
end
|
||||
|
||||
it 'default scientific name' do
|
||||
planting.crop_default_scientific_name.should eq planting.crop.default_scientific_name
|
||||
end
|
||||
|
||||
it 'plantings count' do
|
||||
planting.crop_plantings_count.should eq planting.crop.plantings_count
|
||||
end
|
||||
@@ -267,13 +313,11 @@ describe Planting do
|
||||
@f = FactoryGirl.build(:planting, :planted_at => '2013-01-01', :finished_at => nil)
|
||||
@f.should be_valid
|
||||
end
|
||||
|
||||
it 'allows just the finished date' do
|
||||
@f = FactoryGirl.build(:planting, :finished_at => '2013-01-01', :planted_at => nil)
|
||||
@f.should be_valid
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user