tabletop-companion/app/controllers/dice_rolls_controller.rb

33 lines
737 B
Ruby

# frozen_string_literal: true
class DiceRollsController < ApplicationController
before_action :set_table
def create
rollable = dice_roll_params[:rollable_type].constantize.find(dice_roll_params[:rollable_id])
return head :bad_request if rollable.roll_command.blank?
roller = DiceRoller.new(rollable.roll_command, stat: rollable)
@table.dice_rolls.create!(
rollable:,
result: roller.roll,
dice: roller.dice,
)
head :ok
end
private
def set_table
@table = Current.user.owned_tables.find(params[:table_id])
end
def dice_roll_params
params.require(:dice_roll).permit(
:rollable_type,
:rollable_id,
:dice_roll_type_id,
)
end
end