Class: ActsAsCalculatorEditor::RenderTemplatePreview

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_calculator_editor/render_template_preview.rb

Overview

Preview a template body that has never been saved.

The core gem's HTTP preview needs a persisted record because its controller does Template.find(params[:id]) โ€” but RenderTemplate itself does not: it reads only #body and short-circuits to whatever template: it was handed (docs/core-gem-contract.md ยง4.3). So an unsaved Template.new is enough, which beats "save an early version first" โ€” no throwaway version rows, no partial state โ€” while still exercising the real Liquid sandbox with all its resource limits rather than a second copy of it.

Failures come back as ActsAsCalculator::TemplateRenderError (with a line number, since RenderLiquid parses in strict mode) or UnsafeAssignError for a context value Liquid will not accept. Both are 422s the preview pane shows; neither is a 500.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body:, format: ActsAsCalculator::Template::HTML, context: {}, result: nil) ⇒ RenderTemplatePreview

Returns a new instance of RenderTemplatePreview.



22
23
24
25
26
27
# File 'lib/acts_as_calculator_editor/render_template_preview.rb', line 22

def initialize(body:, format: ActsAsCalculator::Template::HTML, context: {}, result: nil)
  @body = body.to_s
  @format = format.presence || ActsAsCalculator::Template::HTML
  @context = cast_context(context)
  @result = result || BuildSampleResult.()
end

Class Method Details

.callObject



18
19
20
# File 'lib/acts_as_calculator_editor/render_template_preview.rb', line 18

def self.call(...)
  new(...).call
end

Instance Method Details

#callObject



29
30
31
# File 'lib/acts_as_calculator_editor/render_template_preview.rb', line 29

def call
  ActsAsCalculator::RenderTemplate.(template:, result:, context:)
end