Class: Railspress::Admin::FocalPointsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/railspress/admin/focal_points_controller.rb

Instance Method Summary collapse

Instance Method Details

#updateObject

PATCH /admin/focal_points/:id Handles focal point updates, image changes, and image removal



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
# File 'app/controllers/railspress/admin/focal_points_controller.rb', line 10

def update
  image_changed = false
  image_removed = false

  # Handle image removal
  if params[:remove_image] == "1"
    @record.send(@attachment_name).purge
    image_removed = true
  # Handle image change (only if not removing)
  elsif params[:image].present?
    @record.send("#{@attachment_name}=", params[:image])
    if @record.save
      image_changed = true
    else
      return render_error(@record.errors.full_messages.join(", "))
    end
  end

  # If image was changed or removed, redirect to refresh the page
  if image_changed || image_removed
    return redirect_to_record(notice: image_removed ? "Image removed" : "Image updated")
  end

  # Otherwise, update focal point and return turbo frame
  if @focal_point.update(focal_point_params)
    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: turbo_stream.replace(
          helpers.dom_id(@record, "image_section_#{@attachment_name}"),
          partial: "railspress/admin/shared/image_section_compact",
          locals: {
            record: @record,
            attachment_name: @attachment_name.to_sym,
            flash_message: "Focal point saved"
          }
        )
      end
      format.html do
        redirect_to_record(notice: "Focal point saved")
      end
    end
  else
    render_error(@focal_point.errors.full_messages.join(", "))
  end
end