Module: NtiReceiptBuilder::TemplatesController

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/concerns/nti_receipt_builder/templates_controller.rb

Overview

The builder's actions, for inclusion in a host controller that owns routing, authorization and tenancy.

class Partner::ReceiptTemplatesController < PartnerController
include NtiReceiptBuilder::TemplatesController

private

def receipt_owner = 
end

Every query runs through receipt_owner, so the tenant boundary stays visible in host code rather than in a configured lambda. Hosts may override any action or navigation hook — a method defined in the including class wins over the one mixed in here.

Constant Summary collapse

VIEW_PREFIX =
'nti_receipt_builder/templates'

Instance Method Summary collapse

Instance Method Details

#_prefixesObject

Append the gem's view prefix so bare render :edit resolves to the engine's templates. The host's own prefixes stay first, so dropping a receipt_templates/edit.html.erb into the host shadows the gem's copy without any configuration.



31
32
33
# File 'app/controllers/concerns/nti_receipt_builder/templates_controller.rb', line 31

def _prefixes
  @_prefixes ||= super + [VIEW_PREFIX]
end

#createObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/concerns/nti_receipt_builder/templates_controller.rb', line 39

def create
  form = TemplateForm.new(receipt_template_params)

  unless form.valid?
    @errors = form.errors.full_messages
    @receipt_template = receipt_templates.new(default_template_attributes)
    return render_create_failure
  end

  @receipt_template = receipt_templates.new(form.attributes)

  if @receipt_template.save
    redirect_to after_create_path(@receipt_template),
                notice: 'Receipt template created! Now build your layout.'
  else
    @errors = @receipt_template.errors.full_messages
    render_create_failure
  end
end

#destroyObject



73
74
75
76
# File 'app/controllers/concerns/nti_receipt_builder/templates_controller.rb', line 73

def destroy
  @receipt_template.destroy
  redirect_to after_destroy_path, notice: 'Receipt template deleted!'
end

#editObject



59
# File 'app/controllers/concerns/nti_receipt_builder/templates_controller.rb', line 59

def edit; end

#newObject



35
36
37
# File 'app/controllers/concerns/nti_receipt_builder/templates_controller.rb', line 35

def new
  @receipt_template = receipt_templates.new(default_template_attributes)
end

#previewObject



78
79
80
# File 'app/controllers/concerns/nti_receipt_builder/templates_controller.rb', line 78

def preview
  @render_result = Renderer.new(template: @receipt_template).call
end


100
101
102
103
104
# File 'app/controllers/concerns/nti_receipt_builder/templates_controller.rb', line 100

def print
  @render_result = Renderer.new(template: @receipt_template).call

  render :print, layout: false
end

#render_previewObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/concerns/nti_receipt_builder/templates_controller.rb', line 82

def render_preview
  form = TemplateForm.new(receipt_template_params)

  @draft = @receipt_template.dup
  @draft.assign_attributes(form.attributes.except(:lock_version))

  if !form.valid?
    @render_result = nil
    @draft_errors = form.errors.full_messages
  elsif @draft.valid?
    @render_result = Renderer.new(template: @draft).call
    @draft_errors = []
  else
    @render_result = nil
    @draft_errors = @draft.errors.full_messages
  end
end

#set_defaultObject



106
107
108
109
110
# File 'app/controllers/concerns/nti_receipt_builder/templates_controller.rb', line 106

def set_default
  SetDefault.new(template: @receipt_template).call

  redirect_to after_set_default_path, notice: 'Default receipt template updated!'
end

#updateObject



61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/concerns/nti_receipt_builder/templates_controller.rb', line 61

def update
  form = TemplateForm.new(receipt_template_params)

  if form.valid?
    persist_update(form)
  else
    @errors = form.errors.full_messages
  end

  render :update, status: @errors.empty? ? :ok : :unprocessable_content
end