diff options
Diffstat (limited to 'app/controllers/beds_controller.rb')
-rw-r--r-- | app/controllers/beds_controller.rb | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/app/controllers/beds_controller.rb b/app/controllers/beds_controller.rb new file mode 100644 index 0000000..c213978 --- /dev/null +++ b/app/controllers/beds_controller.rb @@ -0,0 +1,61 @@ +class BedsController < ApplicationController + before_action :set_bed, only: %i[ edit update ] + before_action :get_crops, only: %i[ index edit update ] + + def index + @beds = Bed.all + end + + def edit + end + + def update + if @bed.update(bed_params) + redirect_to beds_path, notice: "Bed #{@bed.id} successfully updated." + else + render :edit, status: :unprocessable_entity + end + end + + def bulk_assign_crops + crop = Crop.find(params[:crop_id]) + Raft.update_all(crop_id: crop.id) + redirect_back fallback_location: root_path, notice: "All rafts set to #{crop.name}." + end + + + def reset_seed_crops + # mirrors seed logic + tomatoes = Crop.find_by!(name: :tomatoes) + chives = Crop.find_by!(name: :chives) + lettuce = Crop.find_by!(name: :lettuce) + + Bed.includes(:rafts).find_each do |bed| + default_crop = + case bed.location + when 1..3 then tomatoes + when 4..7 then chives + else lettuce + end + bed.rafts.update_all(crop_id: default_crop.id) + end + redirect_back fallback_location: root_path, notice: "Raft crops reset to default seed layout." + end + + private + + def set_bed + @bed = Bed.find(params[:id]) + end + + def get_crops + @crops = Crop.order(:name) + end + + def bed_params + params.require(:bed).permit( + :location, + rafts_attributes: %i[id crop_id] + ) + end +end |