diff options
Diffstat (limited to 'app/controllers/rafts_controller.rb')
-rw-r--r-- | app/controllers/rafts_controller.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/app/controllers/rafts_controller.rb b/app/controllers/rafts_controller.rb new file mode 100644 index 0000000..96d99fd --- /dev/null +++ b/app/controllers/rafts_controller.rb @@ -0,0 +1,54 @@ +class RaftsController < ApplicationController + before_action :set_collections, only: [ :editor ] + + def index + redirect_to editor_rafts_path + 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) + else + raft.update!(crop_id: val) + end + + redirect_to editor_rafts_path(anchor: "bed-#{raft.bed_id}"), notice: "Raft updated." + end + + private + + def set_collections + @beds = Bed.order(:location) + @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 + end +end |