tabletop-companion/app/controllers/character_sheet_sections_co...

49 lines
1.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class CharacterSheetSectionsController < ApplicationController
before_action :set_character, only: [ :index, :new, :create ]
before_action :set_section, only: [ :destroy ]
def index
@sections = @character.character_sheet_sections.top_level
2024-06-10 17:02:09 +00:00
@editable = ActiveModel::Type::Boolean.new.cast(params[:editable])
end
def new
if params[:parent_section_id].present?
@parent_section = @character.character_sheet_sections.find_by(id: params[:parent_section_id])
end
@section = @character.character_sheet_sections.new
end
def create
@section = @character.character_sheet_sections.new(character_sheet_section_params)
unless @section.save
@parent_section = @section.parent_section
render :new, status: :unprocessable_entity
end
end
def destroy
@id = helpers.dom_id(@section)
@section.destroy
end
private
def set_character
@character = Current.user.characters.find(params[:character_id])
end
def set_section
@section = Current.user.character_sheet_sections.find(params[:id])
end
def character_sheet_section_params
params.require(:character_sheet_section).permit(
:name,
:parent_section_id,
)
end
end