-
-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathkits_controller.rb
More file actions
121 lines (101 loc) · 3.53 KB
/
kits_controller.rb
File metadata and controls
121 lines (101 loc) · 3.53 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
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
class KitsController < ApplicationController
def show
redirect_to allocations_kit_path
end
def index
@kits = current_organization.kits.includes(item: {line_items: :item}).class_filter(filter_params)
@inventory = View::Inventory.new(current_organization.id)
unless params[:include_inactive_items]
@kits = @kits.active
end
@selected_filter_name = filter_params[:by_name]
end
def new
load_form_collections
@kit = current_organization.kits.new
@kit.item = current_organization.items.new
@kit.item.line_items.build
end
def create
kit_creation = KitCreateService.new(organization_id: current_organization.id, kit_params: kit_params)
kit_creation.call
if kit_creation.errors.none?
flash[:notice] = "Kit created successfully"
redirect_to kits_path
else
flash.now[:error] = kit_creation.errors
.map { |error| formatted_error_message(error) }
.join(", ")
# Extract kit and item params separately since line_items belong to Item, not Kit
kit_only_params = kit_params.except(:line_items_attributes)
@kit = Kit.new(kit_only_params)
load_form_collections
@kit.item ||= current_organization.items.new(kit_params.slice(:line_items_attributes))
@kit.item.line_items.build if @kit.item.line_items.empty?
render :new
end
end
def deactivate
@kit = current_organization.kits.find(params[:id])
@kit.deactivate
redirect_back(fallback_location: dashboard_path, notice: "Kit has been deactivated!")
end
def reactivate
@kit = current_organization.kits.find(params[:id])
if @kit.can_reactivate?
@kit.reactivate
redirect_back(fallback_location: dashboard_path, notice: "Kit has been reactivated!")
else
redirect_back(fallback_location: dashboard_path, alert: "Cannot reactivate kit - it has inactive items! Please reactivate the items first.")
end
end
def allocations
@kit = current_organization.kits.find(params[:id])
@storage_locations = current_organization.storage_locations.active
@inventory = View::Inventory.new(current_organization.id)
load_form_collections
end
def allocate
@kit = current_organization.kits.find(params[:id])
@storage_location = current_organization.storage_locations.active.find(kit_adjustment_params[:storage_location_id])
@change_by = kit_adjustment_params[:change_by].to_i
begin
if @change_by.positive?
KitAllocateEvent.publish(@kit, @storage_location.id, @change_by)
else
KitDeallocateEvent.publish(@kit, @storage_location.id, -@change_by)
end
rescue => e
flash[:error] = e.message
end
redirect_to allocations_kit_path(id: @kit.id)
end
private
def load_form_collections
@items = current_organization.items.active.alphabetized
end
def kit_params
kit_params = params.require(:kit).permit(
:name,
:visible_to_partners,
:value_in_dollars
)
item_params = params.require(:item)
.permit(line_items_attributes: [:item_id, :quantity, :_destroy])
kit_params.to_h.merge(item_params.to_h)
end
def kit_adjustment_params
params.require(:kit_adjustment).permit(:storage_location_id, :change_by)
end
def filter_params
return {} unless params.key?(:filters)
params.require(:filters).slice(:by_name)
end
def formatted_error_message(error)
if error.attribute.to_s == "inventory"
"Sorry, we weren't able to save the kit. Validation failed: #{error.message}"
else
error.full_message.humanize
end
end
end