65 lines
1.8 KiB
Ruby
65 lines
1.8 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
|
|
@templates = Template.game_system(@character.game_system)
|
|
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
|
|
@templates = Template.game_system(@character.game_system)
|
|
if params[:template_id].present?
|
|
@section = CharacterSheetSection.new_from_template(
|
|
template: Template.find(params[:template_id]),
|
|
params: character_sheet_section_params,
|
|
character: @character,
|
|
)
|
|
else
|
|
@section = @character.character_sheet_sections.new(character_sheet_section_params)
|
|
end
|
|
@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
|