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
144
145
146
147
148
149
150
151
152
|
# File 'app/controllers/lcp_ruby/actions_controller.rb', line 41
def execute_batch
action_name = params[:action_name]
if built_in_batch_action?(action_name)
authorize_built_in_batch(action_name)
else
unless current_evaluator.can_execute_action?(action_name)
raise Pundit::NotAuthorizedError, "not allowed to execute action #{action_name}"
end
end
batch_action_config = find_batch_action_config(action_name)
if params[:selection_mode] == "filter"
select_all = batch_action_config&.dig("select_all_filter")
if select_all == false
return handle_result(Actions::Result.new(
success: false,
message: I18n.t("lcp_ruby.batch.result.filter_not_allowed",
default: "This action does not support 'select all matching filter' mode."),
redirect_to: nil, data: nil, errors: []
))
end
end
if params[:selection_mode] != "filter"
ids = params[:ids] || []
max_ids = LcpRuby.configuration.max_explicit_ids
if max_ids && ids.length > max_ids
return handle_result(Actions::Result.new(
success: false,
message: I18n.t("lcp_ruby.batch.result.too_many_ids",
count: ids.length, max: max_ids,
default: "Too many records selected (%{count}). Maximum is %{max}."),
redirect_to: nil, data: nil, errors: []
))
end
end
if params[:selection_mode] != "filter"
selection_count = (params[:ids] || []).length
min_sel = batch_action_config&.dig("min_selection")
max_sel = batch_action_config&.dig("max_selection")
if min_sel && selection_count < min_sel
return handle_result(Actions::Result.new(
success: false,
message: I18n.t("lcp_ruby.batch.result.too_few",
count: selection_count, min: min_sel,
default: "Too few records selected (%{count}). Minimum is %{min}."),
redirect_to: nil, data: nil, errors: []
))
end
if max_sel && selection_count > max_sel
return handle_result(Actions::Result.new(
success: false,
message: I18n.t("lcp_ruby.batch.result.selection_too_many",
count: selection_count, max: max_sel,
default: "Too many records selected (%{count}). Maximum is %{max}."),
redirect_to: nil, data: nil, errors: []
))
end
end
max = batch_action_config&.dig("max_batch_records")
record_count = batch_record_count
if max && record_count > max
return handle_result(Actions::Result.new(
success: false,
message: I18n.t("lcp_ruby.batch.result.too_many",
count: record_count, max: max,
default: "Too many records (%{count}). Maximum is %{max}."),
redirect_to: nil, data: nil, errors: []
))
end
batch_op = nil
if batch_action_config&.dig("result_log") && batch_operation_model_available?
batch_op = create_batch_operation(action_name, params[:selection_mode])
end
execution_mode = resolve_execution_mode(batch_action_config, record_count)
if execution_mode == "background"
return enqueue_background_batch(action_name, batch_action_config, batch_op)
end
records = resolve_batch_records
if built_in_batch_action?(action_name)
result = execute_built_in_batch(action_name, records, batch_operation: batch_op)
broadcast_model_change(current_model_definition.name)
else
action_key = find_batch_action_key
result = BatchActions::CustomActionDispatcher.new(
action_key: action_key, records: records,
current_user: current_user, params: action_params,
model_class: @model_class, model_definition: current_model_definition,
batch_operation: batch_op
).call
end
handle_result(result)
end
|