-
-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathitem_request.rb
More file actions
50 lines (46 loc) · 1.61 KB
/
item_request.rb
File metadata and controls
50 lines (46 loc) · 1.61 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# == Schema Information
#
# Table name: item_requests
#
# id :bigint not null, primary key
# name :string
# partner_key :string
# quantity :string
# request_unit :string
# created_at :datetime not null
# updated_at :datetime not null
# item_id :integer
# old_partner_request_id :integer
# partner_request_id :bigint
#
module Partners
class ItemRequest < Base
has_paper_trail
belongs_to :request, class_name: '::Request', foreign_key: :partner_request_id, inverse_of: :item_requests
belongs_to :item
has_many :child_item_requests, dependent: :destroy
has_many :children, through: :child_item_requests
validates :quantity, presence: true
validates :quantity, numericality: {
only_integer: true,
greater_than_or_equal_to: 1,
message: "quantity must be a whole number greater than or equal to 1"
}
validates :name, presence: true
validate :request_unit_is_supported
def request_unit_is_supported
return if request_unit.blank? || request_unit == "-1" # nothing selected
names = item.request_units.map(&:name)
unless names.include?(request_unit)
errors.add(:base, "#{request_unit} is not a supported unit type")
end
end
def name_with_unit(quantity_override = nil)
if Flipper.enabled?(:enable_packs) && request_unit.present?
"#{item&.name || name} - #{request_unit.pluralize(quantity_override || quantity.to_i)}"
else
item&.name || name
end
end
end
end