2024-06-07 15:53:32 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class StatsController < ApplicationController
|
|
|
|
before_action :set_section, only: [ :new, :create ]
|
|
|
|
before_action :set_character, only: [ :new, :create ]
|
2024-06-21 08:09:19 +00:00
|
|
|
before_action :set_stat, only: [ :show, :update, :destroy ]
|
2024-06-07 15:53:32 +00:00
|
|
|
|
|
|
|
def new
|
|
|
|
@stat = @section.stats.new
|
2024-06-17 13:53:09 +00:00
|
|
|
@stat.build_character_sheet_feature
|
2024-06-07 15:53:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@stat = @section.stats.new(stat_params)
|
2024-06-14 11:59:35 +00:00
|
|
|
@editable = true
|
2024-06-07 15:53:32 +00:00
|
|
|
unless @stat.save
|
|
|
|
render :new, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-21 08:09:19 +00:00
|
|
|
def show
|
|
|
|
end
|
|
|
|
|
2024-06-11 10:37:58 +00:00
|
|
|
def update
|
2024-06-21 08:09:19 +00:00
|
|
|
@editable = ActiveModel::Type::Boolean.new.cast(params[:editable])
|
2024-06-11 10:37:58 +00:00
|
|
|
@stat.update(stat_params)
|
|
|
|
end
|
|
|
|
|
2024-06-10 19:39:46 +00:00
|
|
|
def destroy
|
2024-06-11 10:37:58 +00:00
|
|
|
@id = helpers.dom_id(@stat)
|
|
|
|
@stat.destroy
|
2024-06-10 19:39:46 +00:00
|
|
|
end
|
|
|
|
|
2024-06-07 15:53:32 +00:00
|
|
|
private
|
|
|
|
def set_character
|
|
|
|
@character = @section.character
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_section
|
|
|
|
@section = Current.user.character_sheet_sections.find(params[:character_sheet_section_id])
|
|
|
|
end
|
|
|
|
|
2024-06-11 10:37:58 +00:00
|
|
|
def set_stat
|
|
|
|
@stat = Current.user.stats.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2024-06-07 15:53:32 +00:00
|
|
|
def stat_params
|
|
|
|
params.require(:stat).permit(
|
|
|
|
:name,
|
|
|
|
:value,
|
|
|
|
:roll_command,
|
2024-06-17 13:53:09 +00:00
|
|
|
character_sheet_feature_attributes: [
|
|
|
|
:id, :featurable_id, :featurable_type, :character_sheet_section_id, :_destroy,
|
|
|
|
],
|
2024-06-07 15:53:32 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|