diff options
author | Marius Peter <marius.peter@tutanota.com> | 2025-08-29 14:22:37 +0200 |
---|---|---|
committer | Marius Peter <marius.peter@tutanota.com> | 2025-08-29 14:22:37 +0200 |
commit | 075665c588989ed0decdfb20d83f32b33eed4639 (patch) | |
tree | d9ec78be7338bd51c895b7fb311f081a7c77c45f /app/controllers | |
parent | 52b044d6a4278c229992404ad5801769c2d13363 (diff) |
Properly implement bed and raft management logic.
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/beds_controller.rb | 61 | ||||
-rw-r--r-- | app/controllers/crops_controller.rb | 34 | ||||
-rw-r--r-- | app/controllers/rafts_controller.rb | 53 |
3 files changed, 86 insertions, 62 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 diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 9619852..951d380 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -1,40 +1,30 @@ class CropsController < ApplicationController before_action :set_crop, only: %i[ show edit update destroy ] - # GET /crops or /crops.json def index @crops = Crop.all end - # GET /crops/1 or /crops/1.json def show end - # GET /crops/new def new @crop = Crop.new end - # GET /crops/1/edit def edit end - # POST /crops or /crops.json def create @crop = Crop.new(crop_params) - respond_to do |format| - if @crop.save - format.html { redirect_to @crop, notice: "Crop was successfully created." } - format.json { render :show, status: :created, location: @crop } - else - format.html { render :new, status: :unprocessable_entity } - format.json { render json: @crop.errors, status: :unprocessable_entity } - end + if @crop.save + redirect_to @crop, notice: "Crop was successfully created." + else + render :new, status: :unprocessable_entity end end - # PATCH/PUT /crops/1 or /crops/1.json def update respond_to do |format| if @crop.update(crop_params) @@ -47,7 +37,6 @@ class CropsController < ApplicationController end end - # DELETE /crops/1 or /crops/1.json def destroy @crop.destroy! @@ -58,13 +47,12 @@ class CropsController < ApplicationController end private - # Use callbacks to share common setup or constraints between actions. - def set_crop - @crop = Crop.find(params.expect(:id)) - end - # Only allow a list of trusted parameters through. - def crop_params - params.expect(crop: [ :name, :crop_type, :nno3, :p, :k, :ca, :mg, :s, :na, :cl, :si, :fe, :zn, :b, :mn, :cu, :mo, :nnh4 ]) - end + def set_crop + @crop = Crop.find(params.expect(:id)) + end + + def crop_params + params.expect(crop: [ :name, :crop_type, :nno3, :p, :k, :ca, :mg, :s, :na, :cl, :si, :fe, :zn, :b, :mn, :cu, :mo, :nnh4 ]) + end end diff --git a/app/controllers/rafts_controller.rb b/app/controllers/rafts_controller.rb index 96d99fd..c1c8a72 100644 --- a/app/controllers/rafts_controller.rb +++ b/app/controllers/rafts_controller.rb @@ -1,54 +1,29 @@ class RaftsController < ApplicationController - before_action :set_collections, only: [ :editor ] + before_action :set_raft, only: %i[ edit update ] + before_action :set_crop, only: %i[ edit update ] - def index - redirect_to editor_rafts_path + def edit end - def editor - # @beds, @crops set in before_action - end - - # 1) Assign ALL rafts - def assign_all - crop_id = normalized_crop_id(params[:crop_id]) - Raft.update_all(crop_id: crop_id) # nil ok - redirect_to editor_rafts_path, notice: "All rafts updated." - end - - # 2) Assign all rafts in ONE bed - def assign_bed - bed = Bed.find(params.require(:bed_id)) - crop_id = normalized_crop_id(params[:crop_id]) - bed.rafts.update_all(crop_id: crop_id) - redirect_to editor_rafts_path, notice: "Bed ##{bed.location} updated." - end - - # 3) Assign ONE raft - def assign_one - raft = Raft.find(params[:id]) - val = params[:crop_id].to_s - - if val.blank? || val.casecmp("null").zero? - # Skip validations; write NULL directly - raft.update_column(:crop_id, nil) + def update + if @raft.update(raft_params) + redirect_to beds_path, notice: "Raft #{@raft.id} successfully updated." else - raft.update!(crop_id: val) + render :edit, status: :unprocessable_entity end - - redirect_to editor_rafts_path(anchor: "bed-#{raft.bed_id}"), notice: "Raft updated." end private - def set_collections - @beds = Bed.order(:location) + def set_raft + @raft = Raft.find(params[:id]) + end + + def set_crop @crops = Crop.order(:name) end - # Accept "", "null" → nil to allow clearing - def normalized_crop_id(val) - return nil if val.blank? || val.to_s.downcase == "null" - val + def raft_params + params.require(:raft).permit(:location, :bed_id, :crop_id) end end |