tabletop-companion/app/controllers/stats_controller.rb

63 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class StatsController < ApplicationController
before_action :set_section, only: [ :new, :create ]
before_action :set_character, only: [ :new, :create ]
before_action :set_stat, only: [ :show, :edit, :update, :destroy ]
def new
@stat = @section.stats.new
@stat.build_character_sheet_feature
end
def create
@stat = @section.stats.new(stat_params)
@editable = true
unless @stat.save
render :new, status: :unprocessable_entity
end
end
def show
end
def edit
end
def update
@editable = ActiveModel::Type::Boolean.new.cast(params[:editable])
@stat.update!(stat_params)
end
def destroy
@id = helpers.dom_id(@stat)
@stat.destroy
end
private
def set_character
@character = @section.character
end
def set_section
@section = Current.user.character_sheet_sections.find(params[:character_sheet_section_id])
end
def set_stat
@stat = Current.user.stats.find(params[:id])
end
def stat_params
params.require(:stat).permit(
:name,
:value,
:min_allowed,
:max_allowed,
:roll_command,
character_sheet_feature_attributes: [
:id, :featurable_id, :featurable_type, :character_sheet_section_id, :_destroy,
],
)
end
end