From 283bb76a9e473c2ae4ed3886c78f9c53ecf79a3c Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Wed, 4 Jan 2017 17:44:08 +1300 Subject: [PATCH] Link a harvest to a planting, and display --- app/controllers/harvests_controller.rb | 17 ++++++++---- app/models/ability.rb | 2 ++ app/models/harvest.rb | 1 + app/models/planting.rb | 1 + app/views/harvests/_form.html.haml | 19 ++++++++++---- app/views/harvests/show.html.haml | 5 ++++ .../plantings/_planting_harvest.html.haml | 9 +++++++ app/views/plantings/_thumbnail.html.haml | 4 +++ app/views/plantings/show.html.haml | 6 +++-- config/routes.rb | 4 ++- ...0104035248_add_planting_ref_to_harvests.rb | 5 ++++ db/schema.rb | 6 ++++- spec/controllers/harvests_controller_spec.rb | 26 ++++++++++++++++++- 13 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 app/views/plantings/_planting_harvest.html.haml create mode 100644 db/migrate/20170104035248_add_planting_ref_to_harvests.rb diff --git a/app/controllers/harvests_controller.rb b/app/controllers/harvests_controller.rb index 1b924cace..1e13444a3 100644 --- a/app/controllers/harvests_controller.rb +++ b/app/controllers/harvests_controller.rb @@ -26,10 +26,15 @@ class HarvestsController < ApplicationController end end + def show + @planting = @harvest.planting if @harvest.planting_id + end + # GET /harvests/new # GET /harvests/new.json def new @harvest = Harvest.new('harvested_at' => Date.today) + @planting = Planting.find_by(slug: params[:planting_id]) if params[:planting_id] # using find_by_id here because it returns nil, unlike find @crop = Crop.find_or_initialize_by(id: params[:crop_id]) @@ -42,14 +47,13 @@ class HarvestsController < ApplicationController # GET /harvests/1/edit def edit + @planting = @harvest.planting if @harvest.planting_id end # POST /harvests # POST /harvests.json def create - params[:harvest][:owner_id] = current_member.id - params[:harvested_at] = parse_date(params[:harvested_at]) - @harvest = Harvest.new(harvest_params) + @harvest.crop_id = @harvest.planting.crop_id if @harvest.planting_id respond_to do |format| if @harvest.save @@ -90,7 +94,10 @@ class HarvestsController < ApplicationController private def harvest_params - params.require(:harvest).permit(:crop_id, :harvested_at, :description, :owner_id, - :quantity, :unit, :weight_quantity, :weight_unit, :plant_part_id, :slug, :si_weight) + params.require(:harvest) + .permit(:planting_id, :crop_id, :harvested_at, :description, + :quantity, :unit, :weight_quantity, :weight_unit, + :plant_part_id, :slug, :si_weight) + .merge(owner_id: current_member.id) end end diff --git a/app/models/ability.rb b/app/models/ability.rb index b96e6cf77..db4e4af9e 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -92,6 +92,8 @@ class Ability can :create, Harvest can :update, Harvest, owner_id: member.id can :destroy, Harvest, owner_id: member.id + can :update, Harvest, owner_id: member.id, planting: { owner_id: member.id } + can :destroy, Harvest, owner_id: member.id, planting: { owner_id: member.id } can :create, Photo can :update, Photo, owner_id: member.id diff --git a/app/models/harvest.rb b/app/models/harvest.rb index 919dd6526..bb67ccbde 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -7,6 +7,7 @@ class Harvest < ActiveRecord::Base belongs_to :crop belongs_to :owner, class_name: 'Member' belongs_to :plant_part + belongs_to :planting default_scope { order('created_at DESC') } diff --git a/app/models/planting.rb b/app/models/planting.rb index 52c2d5463..7838d1539 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -6,6 +6,7 @@ class Planting < ActiveRecord::Base belongs_to :garden belongs_to :owner, class_name: 'Member', counter_cache: true belongs_to :crop, counter_cache: true + has_many :harvests, -> { order(harvested_at: :desc) }, dependent: :destroy default_scope { order("created_at desc") } scope :finished, -> { where(finished: true) } diff --git a/app/views/harvests/_form.html.haml b/app/views/harvests/_form.html.haml index c116f757c..d4366ff24 100644 --- a/app/views/harvests/_form.html.haml +++ b/app/views/harvests/_form.html.haml @@ -10,13 +10,22 @@ .form-group.required = f.label :crop, 'What did you harvest?', :class => 'control-label col-md-2' - .col-md-4 - = auto_suggest @harvest, :crop, :class => 'form-control col-md-2', :default => @crop + - if @planting + .col-md-8 + = link_to @planting.crop.name, planting_path(@planting) + from + = link_to @planting.garden.name, garden_path(@planting.garden) + = f.hidden_field :planting_id, value: @planting.id + - else + .col-md-4 + = auto_suggest @harvest, :crop, :class => 'form-control col-md-2', :default => @crop + .col-md-4 = collection_select(:harvest, :plant_part_id, PlantPart.all, :id, :name, { :selected => @harvest.plant_part_id }, { :class => 'form-control', :prompt => 'e.g. fruit', :required => "required" }) - %span.help-block.col-md-8 - Can't find what you're looking for? - = link_to "Request new crops.", new_crop_path + - unless @planting + %span.help-block.col-md-8 + Can't find what you're looking for? + = link_to "Request new crops.", new_crop_path .form-group = f.label :harvested_at, 'When?', :class => 'control-label col-md-2' diff --git a/app/views/harvests/show.html.haml b/app/views/harvests/show.html.haml index deb53d6e1..f9021f639 100644 --- a/app/views/harvests/show.html.haml +++ b/app/views/harvests/show.html.haml @@ -24,6 +24,11 @@ %p %b Harvested: = @harvest.harvested_at ? @harvest.harvested_at : "not specified" + + - if @planting + %p + %b Garden: + = link_to @planting.garden, garden_path(@planting.garden) %p %b Quantity: = display_quantity(@harvest) diff --git a/app/views/plantings/_planting_harvest.html.haml b/app/views/plantings/_planting_harvest.html.haml new file mode 100644 index 000000000..aecaf7939 --- /dev/null +++ b/app/views/plantings/_planting_harvest.html.haml @@ -0,0 +1,9 @@ +Harvests: +- if planting.harvests + %ul + - planting.harvests.each do |harvest| + %li + = harvest.harvested_at ? harvest.harvested_at : "undated" + = link_to harvest, harvest_path(harvest) +- else + = "none" \ No newline at end of file diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index 1afdf0b49..67ea71b4d 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -29,6 +29,8 @@ %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? :create, Harvest + %li= link_to 'Harvest', new_planting_harvest_path(planting), :class => 'btn btn-default btn-xs' - if ! planting.finished %li= 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 @@ -42,3 +44,5 @@ .col-xs-12.col-md-8 = render partial: 'plantings/planting_progress', locals: {planting: planting} + .col-xs-12.col-md-8 + = render partial: 'plantings/planting_harvest', locals: {planting: planting} diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 20b850978..766fe518a 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -48,8 +48,8 @@ %dt Finished: %dd= "#{display_finished(@planting)}" - %p - = render 'planting_progress', planting: @planting + %p= render 'plantings/planting_harvest', planting: @planting + %p= render 'planting_progress', planting: @planting - if can? :edit, @planting or can? :destroy, @planting %p @@ -57,6 +57,8 @@ =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? :create, Harvest + = link_to 'Harvest', new_planting_harvest_path(@planting), :class => 'btn btn-default btn-xs' - if can? :destroy, @planting =link_to 'Delete', @planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs' diff --git a/config/routes.rb b/config/routes.rb index e12cca00e..154cb3c62 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,7 +20,9 @@ Growstuff::Application.routes.draw do # rubocop:disable Metrics/BlockLength resources :authentications, only: [:create, :destroy] - resources :plantings + resources :plantings do + resources :harvests + end get '/plantings/owner/:owner' => 'plantings#index', :as => 'plantings_by_owner' get '/plantings/crop/:crop' => 'plantings#index', :as => 'plantings_by_crop' diff --git a/db/migrate/20170104035248_add_planting_ref_to_harvests.rb b/db/migrate/20170104035248_add_planting_ref_to_harvests.rb new file mode 100644 index 000000000..57935b073 --- /dev/null +++ b/db/migrate/20170104035248_add_planting_ref_to_harvests.rb @@ -0,0 +1,5 @@ +class AddPlantingRefToHarvests < ActiveRecord::Migration + def change + add_reference :harvests, :planting, index: true, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 429fabf04..4da8b8ae9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161201154922) do +ActiveRecord::Schema.define(version: 20170104035248) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -265,8 +265,11 @@ ActiveRecord::Schema.define(version: 20161201154922) do t.string "weight_unit" t.integer "plant_part_id" t.float "si_weight" + t.integer "planting_id" end + add_index "harvests", ["planting_id"], name: "index_harvests_on_planting_id", using: :btree + create_table "harvests_photos", id: false, force: :cascade do |t| t.integer "photo_id" t.integer "harvest_id" @@ -467,4 +470,5 @@ ActiveRecord::Schema.define(version: 20161201154922) do add_index "seeds", ["slug"], name: "index_seeds_on_slug", unique: true, using: :btree + add_foreign_key "harvests", "plantings" end diff --git a/spec/controllers/harvests_controller_spec.rb b/spec/controllers/harvests_controller_spec.rb index b07f1c08a..8b5cdcd80 100644 --- a/spec/controllers/harvests_controller_spec.rb +++ b/spec/controllers/harvests_controller_spec.rb @@ -97,6 +97,12 @@ describe HarvestsController do post :create, { harvest: valid_attributes } response.should redirect_to(Harvest.last) end + + it "links to planting" do + planting = FactoryGirl.create(:planting, owner_id: member.id) + post :create, { harvest: valid_attributes.merge(planting_id: planting.id) } + expect(Harvest.last.planting.id).to eq(planting.id) + end end describe "with invalid params" do @@ -114,6 +120,15 @@ describe HarvestsController do response.should render_template("new") end end + describe "not my planting" do + let(:not_my_planting) { FactoryGirl.create(:planting) } + let(:harvest) { FactoryGirl.create(:harvest) } + it "does not save planting_id" do + allow(Harvest).to receive(:new).and_return(harvest) + post :create, { harvest: valid_attributes.merge(planting_id: not_my_planting.id) } + expect(harvest.planting_id).to eq(nil) + end + end end describe "PUT update" do @@ -124,7 +139,7 @@ describe HarvestsController do # specifies that the Harvest created on the previous line # receives the :update message with whatever params are # submitted in the request. - Harvest.any_instance.should_receive(:update).with({ "crop_id" => "1" }) + Harvest.any_instance.should_receive(:update).with({ "crop_id" => "1", "owner_id": member.id }) put :update, { id: harvest.to_param, harvest: { "crop_id" => "1" } } end @@ -158,6 +173,15 @@ describe HarvestsController do response.should render_template("edit") end end + describe "not my planting" do + let(:not_my_planting) { FactoryGirl.create(:planting) } + let(:harvest) { FactoryGirl.create(:harvest) } + it "does not save planting_id" do + allow(Harvest).to receive(:new).and_return(harvest) + put :update, { id: harvest.to_param, harvest: valid_attributes.merge(planting_id: not_my_planting.id) } + expect(harvest.planting_id).to eq(nil) + end + end end describe "DELETE destroy" do