Class: Helios::Press::Admin::BlocksController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/helios/press/admin/blocks_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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
# File 'app/controllers/helios/press/admin/blocks_controller.rb', line 8

def create
  target_position = block_params[:position].to_i

  # Shift positions of blocks at and after the new position
  @post.blocks.where("position >= ?", target_position).update_all("position = position + 1")

  @block = @post.blocks.build(block_params.except(:position))
  @block.position = target_position

  if @block.save
    # Handle image uploads for image_container blocks
    if @block.image_container? && params[:images].present?
      params[:images].each do |key, image_data|
        file = image_data[:file]
        next unless file.present?

        position = key.to_i
        block_image = @block.block_images.build(position: position)
        block_image.file.attach(file)
        block_image.save!
      end
    end

    # Handle video upload for video_container blocks (when helios-videos is present)
    if @block.video_container? && params[:video_signed_id].present? && Helios::Press.videos_enabled?
      video = Helios::Videos::Video.new(name: params[:video_name], block: @block)
      video.video_file.attach(params[:video_signed_id])
      video.save!
    end

    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: turbo_stream.replace(
          "blocks-container",
          partial: "helios/press/admin/posts/blocks_container",
          locals: { post: @post.reload }
        )
      end
    end
  else
    head :unprocessable_entity
  end
end

#destroyObject



69
70
71
72
73
74
75
76
# File 'app/controllers/helios/press/admin/blocks_controller.rb', line 69

def destroy
  position = @block.position
  @block.destroy

  @post.blocks.where("position > ?", position).update_all("position = position - 1")

  head :ok
end

#reorderObject



78
79
80
81
82
83
84
85
# File 'app/controllers/helios/press/admin/blocks_controller.rb', line 78

def reorder
  block_ids = params[:block_ids]
  block_ids.each_with_index do |block_id, index|
    @post.blocks.find(block_id).update_column(:position, index)
  end

  head :ok
end

#updateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/helios/press/admin/blocks_controller.rb', line 52

def update
  if @block.update(block_params)
    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: turbo_stream.replace(
          "block-#{@block.id}",
          partial: "helios/press/admin/blocks/block",
          locals: { block: @block, post: @post }
        )
      end
      format.html { head :ok }
    end
  else
    head :unprocessable_entity
  end
end