tabletop-companion/app/controllers/counters_controller.rb

53 lines
1.2 KiB
Ruby

# frozen_string_literal: true
class CountersController < ApplicationController
before_action :set_section, only: [ :new, :create ]
before_action :set_character, only: [ :new, :create ]
before_action :set_counter, only: [ :update, :destroy ]
def new
@counter = @section.counters.new
@counter.build_character_sheet_feature
end
def create
@counter = @section.counters.new(counter_params)
@editable = true
unless @counter.save
render :new, status: :unprocessable_entity
end
end
def update
@counter.update(counter_params)
end
def destroy
@id = helpers.dom_id(@counter)
@counter.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_counter
@counter = Current.user.counters.find(params[:id])
end
def counter_params
params.require(:counter).permit(
:name,
:value,
character_sheet_feature_attributes: [
:id, :featurable_id, :featurable_type, :character_sheet_section_id, :_destroy,
],
)
end
end