-
-
Notifications
You must be signed in to change notification settings - Fork 573
Expand file tree
/
Copy pathkit.rb
More file actions
52 lines (45 loc) · 1.57 KB
/
kit.rb
File metadata and controls
52 lines (45 loc) · 1.57 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
51
52
# == Schema Information
#
# Table name: kits
#
# id :bigint not null, primary key
# active :boolean default(TRUE)
# name :string not null
# value_in_cents :integer default(0)
# visible_to_partners :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :integer not null
#
class Kit < ApplicationRecord
has_paper_trail
include Filterable
include Valuable
belongs_to :organization
has_one :kit_item, dependent: :restrict_with_exception
scope :active, -> { where(active: true) }
scope :alphabetized, -> { order(:name) }
scope :by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
validates :name, presence: true
validates :name, uniqueness: { scope: :organization }
# @param inventory [View::Inventory]
# @return [Boolean]
def can_deactivate?(inventory = nil)
inventory ||= View::Inventory.new(organization_id)
inventory.quantity_for(item_id: kit_item.id).zero?
end
def deactivate
update!(active: false)
kit_item.update!(active: false)
end
# Kits can't reactivate if they have any inactive items, because now whenever they are allocated
# or deallocated, we are changing inventory for inactive items (which we don't allow).
# @return [Boolean]
def can_reactivate?
kit_item.line_items.joins(:item).where(items: { active: false }).none?
end
def reactivate
update!(active: true)
kit_item.update!(active: true)
end
end