Class: ActsAsCalculatorEditor::FormulasController

Inherits:
ApplicationController show all
Defined in:
app/controllers/acts_as_calculator_editor/formulas_controller.rb

Overview

Identity only — key and scope. There is no expression field anywhere in this controller, because "edit a formula" is not a thing this data model supports: everything mutable about a formula lives on its versions (docs/core-gem-contract.md §2.1), which is what FormulaVersionsController is for.

Constant Summary collapse

AUDITED =

The audit trail outranks a delete. FormulaVersion has_many :runs, dependent: :restrict_with_error, so this delete was always going to fail — the view says so before the operator clicks it, and RecordNotDestroyed's own message ("Failed to destroy the record") is not worth showing anyone.

"This formula has versions with recorded calculation runs. The audit trail is " \
"never deleted to make room for a delete, so the formula stays. Retire its " \
"active version instead if it should stop being used."

Instance Method Summary collapse

Instance Method Details

#createObject

Formula.create! rather than a Decree: identity rows have no effective-dating, no supersede and no version numbering to get wrong, and the core gem's own controller creates them the same way. The Decree rule exists for calculator_formula_versions, calculator_templates and calculator_lookup_table_entries — the tables where a hand-rolled write would bypass real logic.



39
40
41
42
43
44
45
46
# File 'app/controllers/acts_as_calculator_editor/formulas_controller.rb', line 39

def create
  @formula = ActsAsCalculator::Formula.new(**formula_attributes)

  rescuing_core_errors(:new) do
    @formula.save!
    redirect_to formula_path(@formula), notice: "Created formula #{@formula.key}."
  end
end

#destroyObject



59
60
61
62
63
64
65
66
67
# File 'app/controllers/acts_as_calculator_editor/formulas_controller.rb', line 59

def destroy
  formula.destroy!
  redirect_to formulas_path, notice: "Deleted formula #{formula.key}."
rescue ::ActiveRecord::RecordNotDestroyed
  @formula = formula
  @versions = versions
  @error_message = AUDITED
  render :show, status: :conflict
end

#editObject



30
31
32
# File 'app/controllers/acts_as_calculator_editor/formulas_controller.rb', line 30

def edit
  @formula = formula
end

#indexObject



17
18
19
# File 'app/controllers/acts_as_calculator_editor/formulas_controller.rb', line 17

def index
  @formulas = filter_by_key_and_scope(ActsAsCalculator::Formula.order(:scope, :key, :id))
end

#newObject



26
27
28
# File 'app/controllers/acts_as_calculator_editor/formulas_controller.rb', line 26

def new
  @formula = ActsAsCalculator::Formula.new(scope: ActsAsCalculator::DEFAULT_SCOPE)
end

#showObject



21
22
23
24
# File 'app/controllers/acts_as_calculator_editor/formulas_controller.rb', line 21

def show
  @formula = formula
  @versions = versions
end

#updateObject

Re-owning or re-scoping a formula silently re-targets every future calculation of that key (contract §2.7). v1 exposes key and scope only, and no owner field at all.



50
51
52
53
54
55
56
57
# File 'app/controllers/acts_as_calculator_editor/formulas_controller.rb', line 50

def update
  @formula = formula

  rescuing_core_errors(:edit) do
    @formula.update!(**formula_attributes)
    redirect_to formula_path(@formula), notice: "Updated formula #{@formula.key}."
  end
end