Merge remote-tracking branch 'mine/bw/bar_graphs' into bw/bar_graphs

This commit is contained in:
Miles Gould
2017-01-09 15:19:18 +00:00
13 changed files with 90 additions and 15 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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') }

View File

@@ -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) }

View File

@@ -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'

View File

@@ -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)

View File

@@ -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"

View File

@@ -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}

View File

@@ -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'

View File

@@ -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'

View File

@@ -0,0 +1,5 @@
class AddPlantingRefToHarvests < ActiveRecord::Migration
def change
add_reference :harvests, :planting, index: true, foreign_key: true
end
end

View File

@@ -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

View File

@@ -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