Class: Spina::Blocks::Admin::BlocksController

Inherits:
Admin::AdminController
  • Object
show all
Defined in:
app/controllers/spina/blocks/admin/blocks_controller.rb

Instance Method Summary collapse

Instance Method Details

#blocks_dataObject



97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/spina/blocks/admin/blocks_controller.rb', line 97

def blocks_data
  blocks = Spina::Blocks::Block.active.sorted
  template_titles = build_template_titles
  render(json: blocks.map do |b|
    {
      id: b.id,
      name: b.name,
      templateName: b.block_template.to_s,
      templateTitle: template_titles[b.block_template.to_s] || b.block_template.to_s.titleize,
    }
  end)
end

#createObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/spina/blocks/admin/blocks_controller.rb', line 37

def create
  @block = Spina::Blocks::Block.new(block_params)
  if @block.save
    if params[:modal]
      redirect_to(spina.edit_modal_blocks_admin_block_url(@block))
    else
      redirect_to(spina.edit_blocks_admin_block_url(@block))
    end
  else
    @block_templates = creatable_block_templates
    if params[:modal]
      @modal = true
      render(turbo_stream: turbo_stream.update(
        helpers.dom_id(@block, :new_block_modal_form),
        partial: "new_block_modal_form",
      ))
    else
      render(turbo_stream: turbo_stream.update(
        helpers.dom_id(@block, :new_block_form),
        partial: "new_block_form",
      ))
    end
  end
end

#destroyObject



119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/spina/blocks/admin/blocks_controller.rb', line 119

def destroy
  unless @block.deletable?
    flash[:error] = I18n.t("spina.blocks.cannot_be_deleted")
    redirect_to(spina.edit_blocks_admin_block_url(@block))
    return
  end

  flash[:info] = I18n.t("spina.blocks.deleted")
  @block.destroy
  redirect_to(spina.blocks_admin_blocks_url)
end

#editObject



62
63
64
65
# File 'app/controllers/spina/blocks/admin/blocks_controller.rb', line 62

def edit
  add_breadcrumb(I18n.t("spina.blocks.title"), spina.blocks_admin_blocks_path, class: "text-gray-400")
  add_breadcrumb(@block.name)
end

#edit_contentObject



67
68
69
70
71
# File 'app/controllers/spina/blocks/admin/blocks_controller.rb', line 67

def edit_content
  @parts = current_theme.block_templates&.find do |bt|
    bt[:name].to_s == @block.block_template.to_s
  end&.dig(:parts) || []
end

#edit_modalObject



73
74
# File 'app/controllers/spina/blocks/admin/blocks_controller.rb', line 73

def edit_modal
end

#indexObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/spina/blocks/admin/blocks_controller.rb', line 15

def index
  add_breadcrumb(I18n.t("spina.blocks.title"), spina.blocks_admin_blocks_path)

  @block_templates = current_theme.try(:block_templates) || []

  if params[:filter] == "layout"
    @current_filter = "layout"
    @blocks = Spina::Blocks::Block.undeletable.sorted
  elsif params[:block_template].present?
    @current_block_template = params[:block_template]
    @blocks = Spina::Blocks::Block.where(block_template: @current_block_template).sorted
  else
    @blocks = Spina::Blocks::Block.sorted
  end
end

#newObject



31
32
33
34
35
# File 'app/controllers/spina/blocks/admin/blocks_controller.rb', line 31

def new
  @block_templates = creatable_block_templates
  @block = Spina::Blocks::Block.new(block_template: params[:block_template])
  @modal = params[:modal]
end

#sortObject



110
111
112
113
114
115
116
117
# File 'app/controllers/spina/blocks/admin/blocks_controller.rb', line 110

def sort
  params[:ids].each.with_index do |id, index|
    Spina::Blocks::Block.where(id: id).update_all(position: index + 1)
  end

  flash.now[:info] = I18n.t("spina.blocks.sorting_saved")
  render_flash
end

#updateObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/spina/blocks/admin/blocks_controller.rb', line 76

def update
  Mobility.locale = @locale
  if @block.update(block_params)
    if params[:modal]
      render(turbo_stream: turbo_stream.update("modal", ""))
    else
      flash[:success] = I18n.t("spina.blocks.saved")
      redirect_to(spina.edit_blocks_admin_block_url(@block, params: { locale: @locale }))
    end
  elsif params[:modal]
    flash.now[:error] = I18n.t("spina.blocks.couldnt_be_saved")
    render(:edit_modal, status: :unprocessable_entity)
  else
    add_breadcrumb(I18n.t("spina.blocks.title"), spina.blocks_admin_blocks_path, class: "text-gray-400")
    Mobility.locale = I18n.locale
    add_breadcrumb(@block.name)
    flash.now[:error] = I18n.t("spina.blocks.couldnt_be_saved")
    render(:edit, status: :unprocessable_entity)
  end
end