Module: LcpRuby::FormActionExecution

Extended by:
ActiveSupport::Concern
Included in:
CustomFieldsController, DialogsController, ResourcesController
Defined in:
app/controllers/concerns/lcp_ruby/form_action_execution.rb

Constant Summary collapse

VALID_FORM_ACTION_REDIRECTS =
%w[index show edit new].freeze

Instance Method Summary collapse

Instance Method Details

#apply_set_fields!(record, form_action_config) ⇒ Object

Applies set_fields values from the form action config onto the record. Called AFTER assign_attributes(permitted_params) so configurator values override user input. Sets the workflow transition flag to bypass the before_save guard for workflow fields, since set_fields is configurator-controlled (not user-edited).



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/concerns/lcp_ruby/form_action_execution.rb', line 22

def apply_set_fields!(record, form_action_config)
  set_fields = form_action_config["set_fields"]
  return unless set_fields.is_a?(Hash)

  set_fields.each do |field_name, expression|
    value = Workflow::ValueResolver.resolve(expression, record: record, user: current_user)
    if record.respond_to?("#{field_name}=")
      record.send("#{field_name}=", value)
      # Mark workflow transition active so before_save guard allows the change
      Workflow::TransitionExecutor.set_transition_active(record)
    end
  end
end

#current_form_actions(record) ⇒ Object

Returns filtered form actions for the view.



61
62
63
# File 'app/controllers/concerns/lcp_ruby/form_action_execution.rb', line 61

def current_form_actions(record)
  form_action_set(record).form_actions(record)
end

#dispatch_deferred_events(pipeline_result) ⇒ Object

Dispatches deferred events collected during pipeline execution. Must be called AFTER the pipeline transaction has committed.



88
89
90
91
92
93
94
# File 'app/controllers/concerns/lcp_ruby/form_action_execution.rb', line 88

def dispatch_deferred_events(pipeline_result)
  return unless pipeline_result.respond_to?(:deferred_events)

  pipeline_result.deferred_events.each do |event|
    Events::Dispatcher.dispatch(**event)
  end
end

#execute_form_action_pipeline(record, form_action_config) ⇒ Actions::FormActionPipeline::PipelineResult

Executes the form action pipeline for a record. The record must already have user params and set_fields applied. For simple [“save”] pipelines, delegates to record.save directly.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/concerns/lcp_ruby/form_action_execution.rb', line 69

def execute_form_action_pipeline(record, form_action_config)
  pipeline = form_action_config["pipeline"]

  if pipeline == [ "save" ] || pipeline.nil?
    return simple_save_result(record)
  end

  Actions::FormActionPipeline.execute(
    record, pipeline,
    user: current_user,
    evaluator: current_evaluator,
    model_name: current_model_definition.name,
    model_class: @model_class,
    params: {}
  )
end

#form_action_dialog_behavior(form_action_config) ⇒ Object

Returns the dialog behavior string for dialog context. Form action dialog_behavior defaults to “close” per spec. Falls back to params for backward compatibility with non-form-action dialogs.



56
57
58
# File 'app/controllers/concerns/lcp_ruby/form_action_execution.rb', line 56

def form_action_dialog_behavior(form_action_config)
  form_action_config["dialog_behavior"] || params[:on_success] || "close"
end

#form_action_redirect_path(crud_action, form_action_config, record) ⇒ Object

Resolves the redirect path based on the form action config. Supports string targets (show, edit, new, index) and hash targets (association redirect, cross-presenter redirect with defaults). Falls back to presenter’s redirect_after, then to show.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/concerns/lcp_ruby/form_action_execution.rb', line 40

def form_action_redirect_path(crud_action, form_action_config, record)
  target = form_action_config["redirect"]

  case target
  when String
    resolve_string_redirect(target, record) || redirect_path_for(crud_action, record)
  when Hash
    resolve_hash_redirect(target, record) || redirect_path_for(crud_action, record)
  else
    redirect_path_for(crud_action, record)
  end
end

#pipeline_flash_message(crud_action, pipeline_result) ⇒ Object

Resolves flash message considering pipeline action results.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/concerns/lcp_ruby/form_action_execution.rb', line 107

def pipeline_flash_message(crud_action, pipeline_result)
  if pipeline_result&.message.present?
    return pipeline_result.message
  end

  model_label = current_model_definition.resolved_label
  case crud_action
  when "create"
    I18n.t("lcp_ruby.flash.created", model: model_label,
      default: "%{model} was successfully created.")
  when "update"
    I18n.t("lcp_ruby.flash.updated", model: model_label,
      default: "%{model} was successfully updated.")
  end
end

#pipeline_redirect_path(crud_action, form_action_config, record, pipeline_result) ⇒ Object

Resolves redirect path considering pipeline action results. Priority: action result redirect_to > form action config redirect > presenter redirect_after



98
99
100
101
102
103
104
# File 'app/controllers/concerns/lcp_ruby/form_action_execution.rb', line 98

def pipeline_redirect_path(crud_action, form_action_config, record, pipeline_result)
  if pipeline_result&.redirect_to.present?
    return pipeline_result.redirect_to
  end

  form_action_redirect_path(crud_action, form_action_config, record)
end

#resolve_form_action(record) ⇒ Object

Returns the resolved form action config hash for the submitted _form_action param. Falls back to the default save config if the param is missing or unauthorized.



9
10
11
12
13
14
15
16
# File 'app/controllers/concerns/lcp_ruby/form_action_execution.rb', line 9

def resolve_form_action(record)
  action_name = params[:_form_action].to_s
  return default_save_form_action unless action_name.present?

  authorized = form_action_set(record).form_actions(record)
  config = authorized.find { |a| a["name"] == action_name }
  config || default_save_form_action
end