Class: ActsAsCalculatorEditor::TemplatesController

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

Overview

Templates get version history, not effective-dating — there are no effective_from /effective_to columns and no UI here should imply otherwise (docs/core-gem-contract.md §2.5). Changing a body publishes a new row (PublishTemplate); undoing one points current back at an older row (PromoteTemplate). Neither is an update, which is why there is no update action.

new?from_id= prefills from an existing template, which is the "edit this template" affordance: it lands as a new version rather than a rewrite of the old one.

Instance Method Summary collapse

Instance Method Details

#createObject



27
28
29
30
31
32
33
34
35
# File 'app/controllers/acts_as_calculator_editor/templates_controller.rb', line 27

def create
  @form = 

  rescuing_core_errors(:new) do
    published = ActsAsCalculator::PublishTemplate.(**template_attributes)
    redirect_to template_path(published),
                notice: "Published version #{published.version_number} of #{published.key}."
  end
end

#destroyObject



37
38
39
40
# File 'app/controllers/acts_as_calculator_editor/templates_controller.rb', line 37

def destroy
  template.destroy!
  redirect_to templates_path, notice: "Deleted #{template.key} version #{template.version_number}."
end

#indexObject



13
14
15
16
# File 'app/controllers/acts_as_calculator_editor/templates_controller.rb', line 13

def index
  @templates = filter_by_key_and_scope(scoped_templates)
  @history = history?
end

#newObject



23
24
25
# File 'app/controllers/acts_as_calculator_editor/templates_controller.rb', line 23

def new
  @form = prefilled_form
end

#previewObject

Renders whatever body was posted, through the real Liquid sandbox, against a synthetic Result — no persisted template, no throwaway version row (contract §4.3). The member route falls back to the stored body so a saved template can be previewed from its own page; the collection route is what the new-template form posts to while typing.

ActsAsCalculator::TemplateRenderError carries Liquid's own line number, so a failure is shown in the preview pane rather than swallowed.



57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/acts_as_calculator_editor/templates_controller.rb', line 57

def preview
  @template = params[:id].present? ? template : nil
  @preview_format = preview_format
  @preview_body = RenderTemplatePreview.(body: preview_source, format: @preview_format,
                                         context: params[:context])
  render :preview
rescue ActsAsCalculator::Error => e
  @preview_error = e.message
  render :preview, status: :unprocessable_entity
end

#promoteObject

Rollback. The row that went wrong stays in the history; current just stops pointing at it.



44
45
46
47
48
# File 'app/controllers/acts_as_calculator_editor/templates_controller.rb', line 44

def promote
  promoted = ActsAsCalculator::PromoteTemplate.(template:)
  redirect_to template_path(promoted),
              notice: "Version #{promoted.version_number} of #{promoted.key} is now current."
end

#showObject



18
19
20
21
# File 'app/controllers/acts_as_calculator_editor/templates_controller.rb', line 18

def show
  @template = template
  @siblings = ActsAsCalculator::Template.version_siblings_of(template).latest_first
end