Class: LcpRuby::CustomFieldsController

Inherits:
ApplicationController show all
Includes:
FormActionExecution
Defined in:
app/controllers/lcp_ruby/custom_fields_controller.rb

Constant Summary

Constants included from FormActionExecution

FormActionExecution::VALID_FORM_ACTION_REDIRECTS

Constants included from LcpRuby::Controller::BearerAuthentication

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FormActionExecution

#apply_set_fields!, #current_form_actions, #dispatch_deferred_events, #execute_form_action_pipeline, #form_action_dialog_behavior, #form_action_redirect_path, #pipeline_flash_message, #pipeline_redirect_path, #resolve_form_action

Class Method Details

.controller_pathObject

Reuse resources views (index, show, new, edit, _form, etc.)



6
7
8
# File 'app/controllers/lcp_ruby/custom_fields_controller.rb', line 6

def self.controller_path
  "lcp_ruby/resources"
end

Instance Method Details

#bulk_updateObject



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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/controllers/lcp_ruby/custom_fields_controller.rb', line 107

def bulk_update
  authorize @cfd_model_class, :update?
  definitions_params = params[:definitions] || {}
  errors = []
  # Track processed records for re-rendering on error (preserves user input)
  processed_definitions = []

  ActiveRecord::Base.transaction do
    position_counter = 0

    definitions_params.each do |key, defn_params|
      # Skip the hidden template row used by JS for "Add Field"
      next if key == "NEW_RECORD"

      defn_params = defn_params.permit(*bulk_permitted_fields)

      if defn_params[:_remove] == "1"
        # Destroy existing records marked for removal; skip new (unsaved) ones
        if defn_params[:id].present?
          record = @cfd_model_class
            .where(target_model: @parent_model_definition.name)
            .find_by(id: defn_params[:id])
          record&.destroy!
        end
        next
      end

      if defn_params[:id].present?
        record = @cfd_model_class
          .where(target_model: @parent_model_definition.name)
          .find_by(id: defn_params[:id])
        next unless record

        record.assign_attributes(defn_params.except(:id, :_remove))
        record.target_model = @parent_model_definition.name
        record.position = position_counter
        unless record.save
          errors.concat(record.errors.full_messages.map { |m| "#{record.field_name || 'Row'}: #{m}" })
        end
      else
        record = @cfd_model_class.new(defn_params.except(:id, :_remove))
        record.target_model = @parent_model_definition.name
        record.position = position_counter
        unless record.save
          errors.concat(record.errors.full_messages.map { |m| "New field: #{m}" })
        end
      end

      processed_definitions << record
      position_counter += 1
    end

    if errors.any?
      raise ActiveRecord::Rollback
    end
  end

  if errors.any?
    # Re-render with the user's submitted data (not reverted DB state)
    @definitions = processed_definitions
    @layout_builder = Presenter::LayoutBuilder.new(current_presenter, current_model_definition)
    @manage_field_options = build_manage_field_options
    flash.now[:alert] = errors.join("; ")
    render "lcp_ruby/custom_fields/manage", status: :unprocessable_content
  else
    redirect_to custom_fields_index_path,
                notice: I18n.t("lcp_ruby.custom_fields.flash.bulk_updated",
                               default: "Custom fields updated successfully.")
  end
end

#createObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/lcp_ruby/custom_fields_controller.rb', line 45

def create
  @record = @cfd_model_class.new(permitted_params)
  @record.target_model = @parent_model_definition.name
  authorize @record

  if @record.save
    redirect_to custom_fields_show_path,
                notice: I18n.t("lcp_ruby.custom_fields.flash.created",
                               default: "Custom field was successfully created.")
  else
    @layout_builder = Presenter::LayoutBuilder.new(current_presenter, current_model_definition)
    @form_actions = current_form_actions(@record)
    render :new, status: :unprocessable_content
  end
end

#destroyObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/lcp_ruby/custom_fields_controller.rb', line 84

def destroy
  authorize @record
  @record.destroy!
  redirect_to custom_fields_index_path,
              notice: I18n.t("lcp_ruby.custom_fields.flash.deleted",
                             default: "Custom field was successfully deleted.")
rescue ActiveRecord::RecordNotDestroyed => e
  redirect_to resource_path(@record),
              alert: I18n.t("lcp_ruby.custom_fields.flash.delete_failed",
                            errors: e.record.errors.full_messages.join(", "),
                            default: "Could not delete custom field: %{errors}")
end

#editObject



61
62
63
64
65
# File 'app/controllers/lcp_ruby/custom_fields_controller.rb', line 61

def edit
  authorize @record
  @layout_builder = Presenter::LayoutBuilder.new(current_presenter, current_model_definition)
  @form_actions = current_form_actions(@record)
end

#indexObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/lcp_ruby/custom_fields_controller.rb', line 13

def index
  authorize @cfd_model_class
  scope = policy_scope(@cfd_model_class).where(target_model: @parent_model_definition.name)
  scope = apply_search(scope)
  scope = apply_sort(scope)

  @records = scope.page(params[:page]).per(current_presenter.per_page)

  @column_set = Presenter::ColumnSet.new(current_presenter, current_evaluator)
  @fk_map = {}
  @action_set = Presenter::ActionSet.new(current_presenter, current_evaluator)
  @field_resolver = Presenter::FieldValueResolver.new(current_model_definition, current_evaluator)
  @manage_path = manage_custom_fields_path if current_evaluator.can?(:update)
  @slot_locals = { manage_path: @manage_path }
end

#manageObject



97
98
99
100
101
102
103
104
105
# File 'app/controllers/lcp_ruby/custom_fields_controller.rb', line 97

def manage
  authorize @cfd_model_class, :update?
  @definitions = @cfd_model_class
    .where(target_model: @parent_model_definition.name)
    .order(:position)
  @layout_builder = Presenter::LayoutBuilder.new(current_presenter, current_model_definition)
  @manage_field_options = build_manage_field_options
  render "lcp_ruby/custom_fields/manage"
end

#newObject



37
38
39
40
41
42
43
# File 'app/controllers/lcp_ruby/custom_fields_controller.rb', line 37

def new
  @record = @cfd_model_class.new(target_model: @parent_model_definition.name)
  authorize @record
  apply_presenter_defaults(@record)
  @layout_builder = Presenter::LayoutBuilder.new(current_presenter, current_model_definition)
  @form_actions = current_form_actions(@record)
end

#showObject



29
30
31
32
33
34
35
# File 'app/controllers/lcp_ruby/custom_fields_controller.rb', line 29

def show
  authorize @record
  @layout_builder = Presenter::LayoutBuilder.new(current_presenter, current_model_definition)
  @column_set = Presenter::ColumnSet.new(current_presenter, current_evaluator)
  @action_set = Presenter::ActionSet.new(current_presenter, current_evaluator)
  @field_resolver = Presenter::FieldValueResolver.new(current_model_definition, current_evaluator)
end

#updateObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/lcp_ruby/custom_fields_controller.rb', line 67

def update
  authorize @record
  @record.assign_attributes(permitted_params)
  # Prevent target_model tampering
  @record.target_model = @parent_model_definition.name

  if @record.save
    redirect_to custom_fields_show_path,
                notice: I18n.t("lcp_ruby.custom_fields.flash.updated",
                               default: "Custom field was successfully updated.")
  else
    @layout_builder = Presenter::LayoutBuilder.new(current_presenter, current_model_definition)
    @form_actions = current_form_actions(@record)
    render :edit, status: :unprocessable_content
  end
end