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

55 lines
1.5 KiB
Ruby

# 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
@editable = ActiveModel::Type::Boolean.new.cast(params[:editable])
if params[:table_id].present?
@table = Current.user.tables.find(params[:table_id])
@characters = Current.user.characters.where(table: @table)
render layout: "table"
end
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)
@editable = true
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