Files
growstuff/app/controllers/harvests_controller.rb
Daniel O'Connor 035210197f Prompt for updated planting rating when harvesting (#4608)
* Update planting rating when recording a harvest

- Added virtual attribute `overall_rating` to `Harvest` model.
- Updated `HarvestsController` to permit `overall_rating` and synchronize it to the associated `Planting`.
- Added a rating range field (1-5) to the harvest form.
- Added controller tests to verify that the planting rating is updated.
- Refined feature tests for harvesting.

Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>

* I have updated the system to allow for recording a planting rating when a harvest is logged. Here is a summary of the changes:

- Added a virtual attribute `overall_rating` to the `Harvest` model.
- Updated `HarvestsController` to permit `overall_rating` and synchronize it to the associated `Planting`.
- Added a rating range field (1-5) to the harvest form.
- Added controller tests to verify that the planting rating is updated correctly.
- Updated feature tests to ensure the harvest form functions as expected.

Co-authored-by: CloCkWeRX <365751+CloCkWeRX@users.noreply.github.com>

* Update database.yml

* Apply suggestions from code review

Co-authored-by: Daniel O'Connor <daniel.oconnor@gmail.com>

* Adjust wording

* Change harvest modal

* Fix tests

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-05-03 14:47:00 +09:30

123 lines
3.6 KiB
Ruby

# frozen_string_literal: true
class HarvestsController < DataController
after_action :update_crop_medians, only: %i(create update destroy)
def index
where = {}
if params[:member_slug].present?
@owner = Member.find_by!(slug: params[:member_slug])
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('*', where:,
limit: 100,
page: params[:page],
load: (request.format.csv? ? { include: %i(crop owner plant_part) } : false),
boost_by: [:created_at])
@filename = csv_filename
respond_with(@harvests)
end
def show
@matching_plantings = matching_plantings if @harvest.owner == current_member
@photos = @harvest.photos.order(created_at: :desc).paginate(page: params[:page])
respond_with(@harvest)
end
def new
@harvest = Harvest.new(new_harvest_params.merge(harvested_at: Time.zone.today))
@planting = @harvest.planting
@crop = @harvest.crop
respond_with(@harvest)
end
def edit
@planting = @harvest.planting if @harvest.planting_id
respond_with(@harvest)
end
def create
@harvest.crop_id = @harvest.planting.crop_id if @harvest.planting_id
@harvest.harvested_at = Time.zone.now if @harvest.harvested_at.blank?
update_planting_rating if @harvest.save
if params[:return] == 'planting'
respond_with(@harvest, location: @harvest.planting)
else
respond_with(@harvest)
end
end
def update
update_planting_rating if @harvest.update(harvest_params)
respond_with(@harvest)
end
def destroy
@harvest.destroy
respond_with(@harvest)
end
private
def harvest_params
params.require(:harvest)
.permit(:planting_id, :crop_id, :harvested_at, :description,
:quantity, :unit, :weight_quantity, :weight_unit,
:plant_part_id, :slug, :si_weight, :overall_rating)
.merge(owner_id: current_member.id)
end
def new_harvest_params
return {} unless params[:harvest]
params.require(:harvest)
.permit(:planting_id, :crop_id, :harvested_at, :description,
:quantity, :unit, :weight_quantity, :weight_unit,
:plant_part_id, :slug, :si_weight, :overall_rating)
.merge(owner_id: current_member.id)
end
def matching_plantings
Planting.where(crop: @harvest.crop, owner: @harvest.owner)
.where('(planted_at IS NULL OR planted_at <= ?)', @harvest.harvested_at)
.where('(finished_at IS NULL OR finished_at >= ?)', @harvest.harvested_at)
end
def csv_filename
specifics = if @owner
"#{@owner.to_param}-"
elsif @crop
"#{@crop.to_param}-"
end
"Growstuff-#{specifics}Harvests-#{Time.zone.now.to_fs(:number)}.csv"
end
def update_crop_medians
# We only update medians to predict plantings
# if this harvest is not linked to a planting, then do nothing
return if @harvest.planting.nil?
@harvest.planting.update_harvest_days!
@harvest.crop.update_harvest_medians
end
def update_planting_rating
return if @harvest.planting.nil? || params[:harvest][:overall_rating].blank?
@harvest.planting.update(overall_rating: params[:harvest][:overall_rating])
end
end