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

65 lines
1.8 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])
2024-06-13 12:39:50 +00:00
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
2024-07-03 16:28:09 +00:00
@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
2024-07-03 16:28:09 +00:00
@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
2024-06-10 19:39:46 +00:00
@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