tidy up and fix specs for harvest controller

This commit is contained in:
Brenda Wallace
2019-12-23 09:45:35 +13:00
parent 4cf5daf3b8
commit b730378e14
2 changed files with 36 additions and 23 deletions

View File

@@ -10,14 +10,21 @@ class HarvestsController < ApplicationController
responders :flash responders :flash
def index def index
@owner = Member.find_by(slug: params[:member_slug])
@crop = Crop.find_by(slug: params[:crop_slug])
@planting = Planting.find_by(slug: params[:planting_id])
where = {} where = {}
where['owner_id'] = @owner.id if @owner.present? if params[:member_slug]
where['crop_id'] = @crop.id if @crop.present? @owner = Member.find_by(slug: params[:member_slug])
where['planting_id'] = @planting.id if @planting.present? where['owner_id'] = @owner.id
end
if params[:crop_slug]
@crop = Crop.find_by(slug: params[:crop_slug])
where['crop_id'] = @crop.id
end
if params[:planting_slug]
@planting = Planting.find_by(slug: params[:planting_slug])
where['planting_id'] = @planting.id
end
@harvests = Harvest.search('*', @harvests = Harvest.search('*',
where: where, where: where,

View File

@@ -7,10 +7,10 @@ describe HarvestsController do
def valid_attributes def valid_attributes
{ {
owner_id: subject.current_member.id, owner_id: subject.current_member.id,
crop_id: FactoryBot.create(:crop).id, crop_id: FactoryBot.create(:crop).id,
plant_part_id: FactoryBot.create(:plant_part).id, plant_part_id: FactoryBot.create(:plant_part).id,
harvested_at: '2017-01-01' harvested_at: '2017-01-01'
} }
end end
@@ -22,24 +22,30 @@ describe HarvestsController do
let!(:harvest1) { FactoryBot.create(:harvest, owner_id: member1.id, crop_id: tomato.id) } let!(:harvest1) { FactoryBot.create(:harvest, owner_id: member1.id, crop_id: tomato.id) }
let!(:harvest2) { FactoryBot.create(:harvest, owner_id: member2.id, crop_id: maize.id) } let!(:harvest2) { FactoryBot.create(:harvest, owner_id: member2.id, crop_id: maize.id) }
before { Harvest.reindex }
describe "assigns all harvests as @harvests" do describe "assigns all harvests as @harvests" do
before { get :index, params: {} } before { get :index, params: {} }
it { expect(assigns(:harvests)).to eq [harvest1, harvest2] } it { expect(assigns(:harvests).size).to eq 2 }
it { expect(assigns(:harvests)[0]['slug']).to eq harvest1.slug }
it { expect(assigns(:harvests)[1]['slug']).to eq harvest2.slug }
end end
describe "picks up owner from params and shows owner's harvests only" do describe "picks up owner from params and shows owner's harvests only" do
before { get :index, params: { member_slug: member1.slug } } before { get :index, params: { member_slug: member1.slug } }
it { expect(assigns(:owner)).to eq member1 } it { expect(assigns(:owner)).to eq member1 }
it { expect(assigns(:harvests)).to eq [harvest1] } it { expect(assigns(:harvests).size).to eq 1 }
it { expect(assigns(:harvests)[0]['slug']).to eq harvest1.slug }
end end
describe "picks up crop from params and shows the harvests for the crop only" do describe "picks up crop from params and shows the harvests for the crop only" do
before { get :index, params: { crop_slug: maize.name } } before { get :index, params: { crop_slug: maize.name } }
it { expect(assigns(:crop)).to eq maize } it { expect(assigns(:crop)).to eq maize }
it { expect(assigns(:harvests)).to eq [harvest2] } it { expect(assigns(:harvests).size).to eq 1 }
it { expect(assigns(:harvests)[0]['slug']).to eq harvest2.slug }
end end
describe "generates a csv" do describe "generates a csv" do
@@ -53,7 +59,7 @@ describe HarvestsController do
let(:harvest) { Harvest.create! valid_attributes } let(:harvest) { Harvest.create! valid_attributes }
describe "assigns the requested harvest as @harvest" do describe "assigns the requested harvest as @harvest" do
before { get :show, params: { id: harvest.to_param } } before { get :show, params: { slug: harvest.to_param } }
it { expect(assigns(:harvest)).to eq(harvest) } it { expect(assigns(:harvest)).to eq(harvest) }
end end
@@ -75,7 +81,7 @@ describe HarvestsController do
let(:harvest) { Harvest.create! valid_attributes } let(:harvest) { Harvest.create! valid_attributes }
describe "assigns the requested harvest as @harvest" do describe "assigns the requested harvest as @harvest" do
before { get :edit, params: { id: harvest.to_param } } before { get :edit, params: { slug: harvest.to_param } }
it { expect(assigns(:harvest)).to eq(harvest) } it { expect(assigns(:harvest)).to eq(harvest) }
end end
@@ -149,19 +155,19 @@ describe HarvestsController do
it "updates the requested harvest" do it "updates the requested harvest" do
new_crop = FactoryBot.create :crop new_crop = FactoryBot.create :crop
expect do expect do
put :update, params: { id: harvest.to_param, harvest: { crop_id: new_crop.id } } put :update, params: { slug: harvest.to_param, harvest: { crop_id: new_crop.id } }
harvest.reload harvest.reload
end.to change(harvest, :crop_id).to(new_crop.id) end.to change(harvest, :crop_id).to(new_crop.id)
end end
describe "assigns the requested harvest as @harvest" do describe "assigns the requested harvest as @harvest" do
before { put :update, params: { id: harvest.to_param, harvest: valid_attributes } } before { put :update, params: { slug: harvest.to_param, harvest: valid_attributes } }
it { expect(assigns(:harvest)).to eq(harvest) } it { expect(assigns(:harvest)).to eq(harvest) }
end end
describe "redirects to the harvest" do describe "redirects to the harvest" do
before { put :update, params: { id: harvest.to_param, harvest: valid_attributes } } before { put :update, params: { slug: harvest.to_param, harvest: valid_attributes } }
it { expect(response).to redirect_to(harvest) } it { expect(response).to redirect_to(harvest) }
end end
@@ -172,13 +178,13 @@ describe HarvestsController do
harvest = Harvest.create! valid_attributes harvest = Harvest.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted # Trigger the behavior that occurs when invalid params are submitted
Harvest.any_instance.stub(:save).and_return(false) Harvest.any_instance.stub(:save).and_return(false)
put :update, params: { id: harvest.to_param, harvest: { "crop_id" => "invalid value" } } put :update, params: { slug: harvest.to_param, harvest: { "crop_id" => "invalid value" } }
expect(assigns(:harvest)).to eq(harvest) expect(assigns(:harvest)).to eq(harvest)
end end
it "re-renders the 'edit' template" do it "re-renders the 'edit' template" do
harvest = Harvest.create! valid_attributes harvest = Harvest.create! valid_attributes
put :update, params: { id: harvest.to_param, harvest: { "crop_id" => "invalid value" } } put :update, params: { slug: harvest.to_param, harvest: { "crop_id" => "invalid value" } }
expect(response).to render_template("edit") expect(response).to render_template("edit")
end end
end end
@@ -189,7 +195,7 @@ describe HarvestsController do
describe "does not save planting_id" do describe "does not save planting_id" do
before do before do
put :update, params: { id: harvest.to_param, put :update, params: { slug: harvest.to_param,
harvest: valid_attributes.merge(planting_id: not_my_planting.id) } harvest: valid_attributes.merge(planting_id: not_my_planting.id) }
end end
@@ -202,13 +208,13 @@ describe HarvestsController do
it "destroys the requested harvest" do it "destroys the requested harvest" do
harvest = Harvest.create! valid_attributes harvest = Harvest.create! valid_attributes
expect do expect do
delete :destroy, params: { id: harvest.to_param } delete :destroy, params: { slug: harvest.to_param }
end.to change(Harvest, :count).by(-1) end.to change(Harvest, :count).by(-1)
end end
it "redirects to the harvests list" do it "redirects to the harvests list" do
harvest = Harvest.create! valid_attributes harvest = Harvest.create! valid_attributes
delete :destroy, params: { id: harvest.to_param } delete :destroy, params: { slug: harvest.to_param }
expect(response).to redirect_to(harvests_url) expect(response).to redirect_to(harvests_url)
end end
end end