tabletop-companion/app/models/stat.rb

38 lines
1.1 KiB
Ruby

# frozen_string_literal: true
class Stat < ApplicationRecord
has_one :character_sheet_feature, as: :featurable, dependent: :destroy
has_one :character_sheet_section, through: :character_sheet_feature
accepts_nested_attributes_for :character_sheet_feature, allow_destroy: true
has_one :character, through: :character_sheet_section
has_many :dice_rolls, as: :rollable, dependent: :destroy
validates :name, presence: true,
length: { maximum: 100 }
validates :min_allowed, numericality: true, allow_nil: true
validates :max_allowed, numericality: true, allow_nil: true
validates :value, presence: true,
numericality: true
validate :validate_roll_command
def rollable?
roll_command.present?
end
def roll(table)
roller = DiceRoller.new(roll_command, stat: self)
result = roller.roll
dice_rolls.create(result: roller.roll, table:, dice: roller.dice)
result
end
private
def validate_roll_command
return if roll_command.blank?
DiceRoller.new(roll_command).valid?
end
end