From efe2f49e7fc0febcb2f4f4a129bccd5d68ff0f3b Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 28 May 2017 14:20:11 +1200 Subject: [PATCH] More DRY Harvests controller --- app/controllers/harvests_controller.rb | 19 +++---------------- .../harvests/harvesting_a_crop_spec.rb | 10 +++++----- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/app/controllers/harvests_controller.rb b/app/controllers/harvests_controller.rb index c940ecbd2..9f716c2ff 100644 --- a/app/controllers/harvests_controller.rb +++ b/app/controllers/harvests_controller.rb @@ -3,9 +3,8 @@ class HarvestsController < ApplicationController load_and_authorize_resource respond_to :html, :json respond_to :csv, only: :index + responders :flash - # GET /harvests - # GET /harvests.json def index @owner = Member.find_by(slug: params[:owner]) @crop = Crop.find_by(slug: params[:crop]) @@ -16,45 +15,33 @@ class HarvestsController < ApplicationController respond_with(@harvests) end - # GET /harvests/1 - # GET /harvests/1.json def show @matching_plantings = matching_plantings if @harvest.owner == current_member respond_with(@harvest) end - # GET /harvests/new - # GET /harvests/new.json def new @harvest = Harvest.new(harvested_at: Time.zone.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_by(id: params[:crop_id]) respond_with(@harvest) end - # GET /harvests/1/edit def edit @planting = @harvest.planting if @harvest.planting_id end - # POST /harvests - # POST /harvests.json def create @harvest.crop_id = @harvest.planting.crop_id if @harvest.planting_id - flash[:notice] = I18n.t('harvests.created') if @harvest.save + @harvest.save respond_with(@harvest) end - # PUT /harvests/1 - # PUT /harvests/1.json def update - flash[:notice] = I18n.t('harvests.updated') if @harvest.update(harvest_params) + @harvest.update(harvest_params) respond_with(@harvest) end - # DELETE /harvests/1 - # DELETE /harvests/1.json def destroy @harvest.destroy respond_with(@harvest) diff --git a/spec/features/harvests/harvesting_a_crop_spec.rb b/spec/features/harvests/harvesting_a_crop_spec.rb index c9b1d511f..84c4b5a7f 100644 --- a/spec/features/harvests/harvesting_a_crop_spec.rb +++ b/spec/features/harvests/harvesting_a_crop_spec.rb @@ -39,7 +39,7 @@ feature "Harvesting a crop", :js, :elasticsearch do click_button "Save" end - expect(page).to have_content "Harvest was successfully created" + expect(page).to have_content "harvest was successfully created." end context "Clicking edit from the index page" do @@ -71,7 +71,7 @@ feature "Harvesting a crop", :js, :elasticsearch do click_button "Save" end - expect(page).to have_content "Harvest was successfully created" + expect(page).to have_content "harvest was successfully created." expect(page).to have_content "maize" end @@ -83,7 +83,7 @@ feature "Harvesting a crop", :js, :elasticsearch do select plant_part.name, from: 'harvest[plant_part_id]' click_button "Save" - expect(page).to have_content "Harvest was successfully created" + expect(page).to have_content "harvest was successfully created." expect(page).to have_content planting.garden.name expect(page).to have_content "maize" end @@ -101,14 +101,14 @@ feature "Harvesting a crop", :js, :elasticsearch do # Check that the autosuggest helper properly fills inputs with # existing resource's data click_button "Save" - expect(page).to have_content "Harvest was successfully updated" + expect(page).to have_content "harvest was successfully updated." expect(page).to have_content "maize" end scenario "change plant part" do select other_plant_part.name, from: 'harvest[plant_part_id]' click_button "Save" - expect(page).to have_content "Harvest was successfully updated" + expect(page).to have_content "harvest was successfully updated." expect(page).to have_content other_plant_part.name end end