From 390282f9f231118db926810b1fe1ecc336c4e5d3 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Thu, 11 Jan 2018 09:03:13 +1300 Subject: [PATCH] Added Harvested for chart --- app/controllers/crops_controller.rb | 24 +++++++++++++++--------- app/views/crops/show.html.haml | 3 +++ config/routes.rb | 1 + 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 371e70726..d3bd5dcf9 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -103,23 +103,29 @@ class CropsController < ApplicationController end def sunniness - @crop = Crop.find(params[:crop_id]) - render json: Planting.where(crop: @crop) - .where.not(sunniness: nil) - .where.not(sunniness: '') - .group(:sunniness).count(:id) + pie_chart_query 'sunniness' end def planted_from + pie_chart_query 'planted_from' + end + + def harvested_for @crop = Crop.find(params[:crop_id]) - render json: Planting.where(crop: @crop) - .where.not(planted_from: nil) - .where.not(planted_from: '') - .group(:planted_from).count(:id) + render json: Harvest.joins(:plant_part).where(crop: @crop) + .group("plant_parts.name").count(:id) end private + def pie_chart_query(field) + @crop = Crop.find(params[:crop_id]) + render json: Planting.where(crop: @crop) + .where.not(field.to_sym => nil) + .where.not(field.to_sym => '') + .group(field.to_sym).count(:id) + end + def notifier case @crop.approval_status when "approved" diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index 8f4486fe9..270c0db2b 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -56,6 +56,9 @@ .col-md-3 %h2 Planted from = pie_chart crop_planted_from_path(@crop), legend: "bottom" + .col-md-3 + %h2 Harvested for + = pie_chart crop_harvested_for_path(@crop), legend: "bottom" .row .col-md-12 diff --git a/config/routes.rb b/config/routes.rb index 2872db18b..9d4e3fe60 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -52,6 +52,7 @@ Growstuff::Application.routes.draw do get 'photos' => 'photos#index' get 'sunniness' => 'crops#sunniness' get 'planted_from' => 'crops#planted_from' + get 'harvested_for' => 'crops#harvested_for' end resources :comments