tabletop-companion/app/models/dice_roll.rb

29 lines
718 B
Ruby
Raw Permalink Normal View History

2024-06-13 07:41:45 +00:00
# frozen_string_literal: true
class DiceRoll < ApplicationRecord
belongs_to :rollable, polymorphic: true
belongs_to :table
validates :result, presence: true,
numericality: { only_integer: true }
2024-06-13 12:24:54 +00:00
2024-06-13 16:04:25 +00:00
after_create_commit do
broadcast_append_to table, target: "events"
2024-06-14 07:39:38 +00:00
broadcast_append_to table, target: "table-notifications",
partial: "shared/notification", locals: { object: self }
2024-06-13 16:04:25 +00:00
end
2024-06-13 12:24:54 +00:00
def display_text
2024-06-20 07:26:33 +00:00
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(", ")
2024-06-13 12:24:54 +00:00
end
2024-06-13 07:41:45 +00:00
end