tabletop-companion/app/models/dice_roll.rb

29 lines
718 B
Ruby

# frozen_string_literal: true
class DiceRoll < ApplicationRecord
belongs_to :rollable, polymorphic: true
belongs_to :table
validates :result, presence: true,
numericality: { only_integer: true }
after_create_commit do
broadcast_append_to table, target: "events"
broadcast_append_to table, target: "table-notifications",
partial: "shared/notification", locals: { object: self }
end
def display_text
text = <<~TEXT
#{rollable.character.name} rolled #{rollable.name}:
<strong>#{result}</strong>
(#{dice_text})
TEXT
text.html_safe
end
def dice_text
dice.map { |d| "D#{d[0]}: #{d[1]}" }.join(", ")
end
end