app/controllers/maglev/editor/section_blocks_controller.rb
# frozen_string_literal: true module Maglev module Editor class SectionBlocksController < Maglev::Editor::BaseController helper Maglev::Editor::SettingsHelper helper_method :source_lock_version before_action :set_section before_action :set_section_block, only: %i[edit update destroy] def index @blocks = @section.blocks end def show redirect_to edit_editor_section_block_path(params[:section_id], params[:id], maglev_editing_route_context) end def create services.add_section_block.call( page: current_maglev_page, section_id: @section.id, block_type: params[:block_type], parent_id: params[:parent_id], lock_version: params[:lock_version] ) redirect_to_section_blocks_path end def edit; end def update update_section_block flash.now[:notice] = flash_t(:success) end def sort services.sort_section_blocks.call( page: current_maglev_page, section_id: @section.id, block_ids: params[:item_ids], parent_id: params[:parent_id], lock_version: params[:lock_version] ) redirect_to_section_blocks_path end def destroy services.delete_section_block.call( page: current_maglev_page, section_id: params[:section_id], block_id: params[:id] ) redirect_to_section_blocks_path end private def set_section @section = current_maglev_sections.find { |section| section.id == params[:section_id] } redirect_to editor_sections_path_with_context unless @section end def set_section_block @section_block = @section.blocks.find(params[:id]) redirect_to editor_sections_path_with_context unless @section_block end def update_section_block services.update_section_block.call( page: current_maglev_page, section_id: @section.id, block_id: @section_block.id, content: params[:section_block].to_unsafe_h, lock_version: params[:lock_version] ) end def lock_source @section.site_scoped? ? maglev_site : current_maglev_page end def source_lock_version lock_source.lock_version || 0 end def redirect_to_section_blocks_path(success: true) flash = case success when true then { notice: flash_t(:success) } when false then { alert: flash_t(:error) } else {} end path = editor_section_blocks_path(@section.id, **maglev_editing_route_context) redirect_to path, status: :see_other, **flash end def editor_sections_path_with_context editor_sections_path(maglev_editing_route_context) end end end end