summaryrefslogtreecommitdiff
path: root/app/controllers/rafts_controller.rb
blob: 96d99fd361479a845d74d6100c8902444b935bc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
Copyright 2019--2025 Marius PETER