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
|
# File 'app/controllers/easy_admin/batch_actions_controller.rb', line 7
def execute
action_class_name = params[:action_class]
execution_mode = params[:execution_mode]
begin
action_class = action_class_name.constantize
action_instance = action_class.new
if execution_mode == 'modal'
action_instance.assign_attributes(batch_action_params(action_class))
unless action_instance.valid?
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.update("notifications",
EasyAdmin::NotificationComponent.new(
type: :error,
message: action_instance.errors.full_messages.join(', '),
title: "Validation Error"
).call
),
]
end
end
return
end
end
action_instance.selected_ids = @selected_records.pluck(:id)
action_instance.current_user = current_admin_user
action_instance.resource_class = @resource_class
action_instance.params = batch_action_params(action_class).to_h
result = action_instance.perform
respond_to do |format|
format.turbo_stream do
if result[:success]
streams = [
turbo_stream.update("notifications",
EasyAdmin::NotificationComponent.new(
type: :success,
message: result[:message] || 'Action completed successfully',
title: "Success"
).call
)
]
@selected_records.each do |record|
record.reload
streams << turbo_stream.replace(
helpers.dom_id(record),
EasyAdmin::Resources::TableRowComponent.new(
record: record,
resource_class: @resource_class
).call
)
end
render turbo_stream: streams
else
render turbo_stream: [
turbo_stream.update("notifications",
EasyAdmin::NotificationComponent.new(
type: :error,
message: result[:message] || 'Action failed',
title: "Error"
).call
),
]
end
end
end
rescue NameError => e
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.update("notifications",
EasyAdmin::NotificationComponent.new(
type: :error,
message: 'Invalid action class',
title: "Error"
).call
)
end
end
end
end
|