Added the days before maturity feature #678

This commit is contained in:
AELOGICA
2015-06-26 06:54:49 +08:00
parent 6d81ef198c
commit 372a7f080d
6 changed files with 118 additions and 31 deletions

View File

@@ -67,6 +67,7 @@ class PlantingsController < ApplicationController
def create
params[:planted_at] = parse_date(params[:planted_at])
@planting = Planting.new(planting_params)
@planting.days_before_maturity = update_days_before_maturity(@planting, planting_params[:crop_id])
@planting.owner = current_member
respond_to do |format|
@@ -87,6 +88,8 @@ class PlantingsController < ApplicationController
@planting = Planting.find(params[:id])
params[:planted_at] = parse_date(params[:planted_at])
@planting.days_before_maturity = update_days_before_maturity(@planting, planting_params[:crop_id])
respond_to do |format|
if @planting.update(planting_params)
format.html { redirect_to @planting, notice: 'Planting was successfully updated.' }
@@ -119,4 +122,12 @@ class PlantingsController < ApplicationController
:quantity, :sunniness, :planted_from, :owner_id, :finished,
:finished_at)
end
def update_days_before_maturity(planting, crop_id)
if planting.finished_at.nil?
planting.calculate_days_before_maturity(crop_id)
else
(planting.finished_at - planting.planted_at).to_i
end
end
end

View File

@@ -94,6 +94,21 @@ class Planting < ActiveRecord::Base
return photos.present?
end
def calculate_days_before_maturity(crop)
p_crop = Planting.where(:crop_id => crop)
differences = p_crop.collect do |p|
if p.finished and !p.finished_at.nil?
(p.finished_at - p.planted_at).to_i
end
end
if differences.compact.empty?
average = nil
else
average = differences.compact.sum/differences.compact.length
end
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.

View File

@@ -0,0 +1,17 @@
- if DateTime.now.to_date < planting.planted_at
= "Progress: #{percent=0}% - "
= "not planted yet"
.progress
.progress-bar.progress-bar-warning{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "100%", :role => "progressbar", :style => "width: 100%"}
- elsif planting.days_before_maturity.nil?
= "Progress: #{percent=0}% - "
= "Days before maturity unknown"
.progress
.progress-bar.progress-bar-danger{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "100%", :role => "progressbar", :style => "width: 100%"}
- else
- if (percent = (((DateTime.now - planting.planted_at)/planting.days_before_maturity*100).to_i)) >= 100
- percent = 100
= "Progress: #{percent}" + "%"
.progress
.progress-bar.progress-bar-success{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "#{percent}", :role => "progressbar", :style => "width: #{percent}%"}

View File

@@ -22,40 +22,69 @@
%table.table.table-striped
%tr
- unless @owner
%th Owner
%th Crop
%th Garden
%th Quantity
%th Planted on
%th Finished
%th Sun/shade?
%th Planted from
%th
- @plantings.each do |planting|
%th #
%th Photo
%th{:colspan => "3"} Planting details
- @plantings.each.with_index do |planting, index|
%tr
- unless @owner
%td= link_to planting.owner.login_name, planting.owner
%td= link_to planting.crop.name, planting.crop
%td= link_to planting.garden.name, planting.garden
%td= planting.quantity
%td= planting.planted_at
%td= "##{index+1}"
%td= link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => planting.crop_id, :class => 'img'), planting
%td
- if planting.finished and planting.finished_at
= planting.finished_at
- elsif planting.finished
Yes (no date specified)
%td= planting.sunniness
%td= planting.planted_from
%ul{:style => "list-style-type:none"}
%li Owner :
%li Garden :
%li Planted on :
%li Finished on :
%li Sun/shade? :
%li Planted from :
%td
= link_to 'Details', planting, :class => 'btn btn-default btn-xs'
- if can? :edit, planting
=link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-default btn-xs'
- if ! planting.finished
= link_to "Mark as finished", planting_path(planting, :planting => {:finished => 1}), :method => :put, :class => 'btn btn-default btn-xs append-date'
- if can? :destroy, planting
=link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs'
%ul{:style => "list-style-type:none"}
%li= link_to planting.owner.login_name, planting.owner
%li= link_to planting.garden.name, planting.garden
%li= planting.planted_at
- if planting.finished and planting.finished_at?
%li= planting.finished_at
- elsif planting.finished
%li= "Yes (no date specified)"
- else
%li= "(no date specified)"
- if planting.sunniness?
%li= planting.sunniness
- else
%li= "n/a"
%li= planting.planted_from
%td
%ul{:style => "list-style-type:none"}
%li= link_to 'Details', planting, :class => 'btn btn-default btn-xs'
- if can? :edit, planting
%li= link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-default btn-xs'
- if can? :destroy, planting
%li= link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs'
%tr
%td
%td
%ul{:style => "list-style-type:none"}
%li
Crop name:
=link_to planting.crop.name, planting.crop
- if planting.finished and !planting.finished_at.nil?
- if ((p = planting.finished_at - DateTime.now).to_i) <= 0
%li= "Days until maturity: 0"
- else
%li= "Days until maturity: #{p.to_i}"
- elsif planting.days_before_maturity.nil?
%li= "Days until maturity: unknown"
- else
- if ((p = (planting.planted_at + planting.days_before_maturity) - DateTime.now).to_i) <= 0
%li= "Days until maturity: 0"
- else
%li= "Days until maturity: #{p.to_i}"
%td{:colspan => "3"}
%ul{:style => "list-style-type:none"}
%li= render 'planting_progress', planting: planting
%div.pagination
= page_entries_info @plantings, :model => "plantings"

View File

@@ -38,6 +38,16 @@
= @planting.finished_at
- else
Yes (no date specified)
%p
%b Days until maturity:
- if @planting.finished and @planting.finished_at?
= "#{(@planting.finished_at - DateTime.now).to_i}"
- elsif @planting.days_before_maturity.nil?
= "unknown"
- else
= "#{((@planting.planted_at + @planting.days_before_maturity) - DateTime.now).to_i}"
%p
%b= render 'planting_progress', planting: @planting

View File

@@ -0,0 +1,5 @@
class AddDaysBeforeMaturityToPlantings < ActiveRecord::Migration
def change
add_column :plantings, :days_before_maturity, :integer
end
end