Class: LcpRuby::ActionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/lcp_ruby/actions_controller.rb

Constant Summary

Constants included from Controller::BearerAuthentication

Controller::BearerAuthentication::BASIC_PREFIX_LENGTH, Controller::BearerAuthentication::BEARER_PREFIX_LENGTH

Instance Method Summary collapse

Methods included from Controller::Authorization

#current_evaluator

Instance Method Details

#execute_batchObject



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]

  # Authorize
  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)

  # Reject filter mode when select_all_filter is false
  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

  # Reject ID selection exceeding max_explicit_ids
  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

  # Check min_selection / max_selection (ID mode only)
  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

  # Check max_batch_records limit (uses count, no record loading)
  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

  # Create batch_operation record if result_log is enabled
  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

  # Determine execution mode (reuses record_count, no extra query)
  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

  # Load records for inline execution
  records = resolve_batch_records

  # Execute inline
  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

#execute_collectionObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/lcp_ruby/actions_controller.rb', line 27

def execute_collection
  ensure_action_execution_allowed!
  authorize @model_class, :index?
  action_key = "#{current_presenter.model}/#{params[:action_name]}"

  result = Actions::ActionExecutor.new(action_key, {
    current_user: current_user,
    params: action_params,
    model_class: @model_class
  }).execute

  handle_result(result)
end

#execute_singleObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/lcp_ruby/actions_controller.rb', line 3

def execute_single
  record = load_action_record!(params[:id])
  action_name = params[:action_name]

  # Check if this single action defines a pipeline
  single_config = find_single_action_config(action_name)

  if single_config && single_config["pipeline"]
    execute_single_pipeline(record, single_config)
  else
    ensure_action_execution_allowed!
    action_key = "#{current_presenter.model}/#{action_name}"

    result = Actions::ActionExecutor.new(action_key, {
      record: record,
      current_user: current_user,
      params: action_params,
      model_class: @model_class
    }).execute

    handle_result(result)
  end
end