blob: b54f799b65c43fec4c950f46f21b7f2526b854d6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
class CharacterItem < ApplicationRecord
belongs_to :character
belongs_to :item
validates :quantity, presence: true
after_initialize do
if self.new_record?
self.quantity ||= 0
end
end
after_save :destroy_if_zero_quantity
scope :ordered_by_item_name, -> { includes(:item).order("items.name") }
private
def destroy_if_zero_quantity
if self.quantity == 0
destroy
elsif self.quantity < 0
# TODO: Can improve this (at the least, with reporting, later).
raise ItemQuantityError
end
end
end
|