-
-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathitemizable.rb
143 lines (114 loc) · 4.16 KB
/
itemizable.rb
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Creates a veritable powerhouse.
# This module provides Duck Typed behaviors for anything that shuttles LINE ITEMS (not items)
# throughout the system. e.g. things that `has_many :line_items` -- this provides
# all the logic about how those kinds of things behave.
module Itemizable
extend ActiveSupport::Concern
included do
# So we previously had `dependent:destroy` but that was deleting the `line_items`
# before they could be also destroyed in the `storage_location`. This does the same
# thing, but defers the deletion until after other stuff has been done.
after_destroy do
line_items.each(&:destroy)
end
# @return [Boolean]
def has_inactive_item?
inactive_items.any?
end
# @return [Array<Item>] or [Item::ActiveRecord_Relation]
def inactive_items
line_items.map(&:item).select { |i| !i.active? }
end
has_many :line_items, as: :itemizable, inverse_of: :itemizable do
def assign_insufficiency_errors(insufficiency_hash)
insufficiency_hash = insufficiency_hash.index_by { |i| i[:item_id] }
each do |line_item|
next unless insufficiency = insufficiency_hash[line_item.item_id]
line_item.errors.add(:quantity, :insufficient, message: "too high. Change to #{insufficiency[:quantity_on_hand]} or less")
end
end
def combine!
# Bail if there's nothing
return if size.zero?
# First we'll collect all the line_items that are used
combined = {}
parent_id = first.itemizable_id
each do |line_item|
next unless line_item.valid?
next unless line_item.quantity != 0
combined[line_item.item_id] ||= 0
combined[line_item.item_id] += line_item.quantity
end
# Delete all the existing ones in this association -- this
# method aliases to `delete_all`
clear
# And now recreate a new array of line_items using the corrected totals
combined.each do |item_id, qty|
build(quantity: qty, item_id: item_id, itemizable_id: parent_id)
end
end
def quantities_by_category
results = {}
# Gather the quantities first
each do |li|
next if li.quantity.zero?
results[li.item.item_category_id] ||= 0
results[li.item.item_category_id] += li.quantity
end
results
end
def quantities_by_name
results = {}
each do |li|
next if li.quantity.zero?
results[li.id] = { item_id: li.item.id, name: li.item.name, quantity: li.quantity }
end
results
end
def sorted
includes(:item).order("items.name")
end
def total
pluck(:quantity).compact.sum
end
def total_value
sum(&:value_per_line_item)
end
end
has_many :items, through: :line_items
has_many :inactive_items, -> { inactive }, through: :line_items, source: :item
accepts_nested_attributes_for :line_items,
allow_destroy: true,
reject_if: proc { |l| l[:item_id].blank? || l[:quantity].blank? }
has_many :item_categories, through: :items
# Anything using line_items should not be OK with an invalid line_item
validates_associated :line_items
end
def value_per_itemizable
line_items.sum(&:value_per_line_item)
end
def total_quantity
line_items.total
end
def line_item_values
line_items.map do |l|
item = Item.find(l.item_id)
{ item_id: item.id, name: item.name, quantity: l.quantity, active: item.active }.with_indifferent_access
end
end
private
# From Controller parameters
def line_items_attributes(params)
Array.wrap(params[:line_items_attributes]&.values)
end
def line_items_quantity_is_at_least(threshold)
return if respond_to?(:storage_location) && storage_location.nil?
line_items.each do |line_item|
next unless line_item.item
next if line_item.quantity.nil? || line_item.quantity >= threshold
errors.add(:inventory,
"#{line_item.item.name}'s quantity " \
"needs to be at least #{threshold}")
end
end
end