Class: Jets::Controller::Renderers::TemplateRenderer

Inherits:
BaseRenderer
  • Object
show all
Defined in:
lib/jets/controller/renderers/template_renderer.rb

Instance Attribute Summary

Attributes inherited from BaseRenderer

#controller

Instance Method Summary collapse

Methods inherited from BaseRenderer

#initialize, #render_aws_proxy

Constructor Details

This class inherits a constructor from Jets::Controller::Renderers::BaseRenderer

Instance Method Details

#controller_instance_variablesObject



3
4
5
6
7
8
9
10
11
# File 'lib/jets/controller/renderers/template_renderer.rb', line 3

def controller_instance_variables
  instance_vars = @controller.instance_variables.inject({}) do |vars, v|
    k = v.to_s.sub(/^@/,'') # @var => var
    vars[k] = @controller.instance_variable_get(v)
    vars
  end
  instance_vars[:event] = event
  instance_vars
end

#default_template_nameObject

Example: posts/index



25
26
27
# File 'lib/jets/controller/renderers/template_renderer.rb', line 25

def default_template_name
  "#{template_namespace}/#{@controller.meth}"
end

#renderObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/jets/controller/renderers/template_renderer.rb', line 13

def render
  setup_action_controller # setup only when necessary

  # Rails rendering does heavy lifting
  renderer = ActionController::Base.renderer.new(renderer_options)
  body = renderer.render(render_options)
  @options[:body] = body # important to set as it was originally nil

  render_aws_proxy(@options)
end

#render_optionsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jets/controller/renderers/template_renderer.rb', line 52

def render_options
  # nomralize the template option
  template = @options[:template]
  if template and !template.include?('/')
    template = "#{template_namespace}/#{template}"
  end
  template ||= default_template_name
  # ready to override @options[:template]
  @options[:template] = template if @options[:template]

  render_options = {
    template: template, # weird: template needs to be set no matter because it
      # sets the name which is used in lookup_context.rb:209:in `normalize_name'
    layout: @options[:layout],
    assigns: controller_instance_variables,
  }
  types = %w[json inline plain file xml body action].map(&:to_sym)
  types.each do |type|
    render_options[type] = @options[type] if @options[type]
  end

  render_options
end

#renderer_optionsObject

default options:

https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/renderer.rb#L41-L47


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jets/controller/renderers/template_renderer.rb', line 36

def renderer_options
  origin = headers["origin"]
  if origin
    uri = URI.parse(origin)
    https = uri.scheme == "https"
  end
  options = {
    http_host: headers["host"],
    https: https,
    # script_name: "",
    # input: ""
  }
  @options[:method] = event["httpMethod"].downcase if event["httpMethod"]
  options
end

#setup_action_controllerObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/jets/controller/renderers/template_renderer.rb', line 76

def setup_action_controller
  require "action_controller"
  require "jets/rails_overrides"

  # laod helpers
  helper_class = self.class.name.to_s.sub("Controller", "Helper")
  helper_path = "#{Jets.root}app/helpers/#{helper_class.underscore}.rb"
  ActiveSupport.on_load :action_view do
    include ApplicationHelper
    include helper_class.constantize if File.exist?(helper_path)
  end

  ActionController::Base.append_view_path("#{Jets.root}app/views")

  setup_webpacker if Jets.webpacker?
end

#setup_webpackerObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jets/controller/renderers/template_renderer.rb', line 93

def setup_webpacker
  require 'webpacker'
  require 'webpacker/helper'

  ActiveSupport.on_load :action_controller do
    ActionController::Base.helper Webpacker::Helper
  end

  ActiveSupport.on_load :action_view do
    include Webpacker::Helper
  end
end

#template_namespaceObject

PostsController => “posts” is the namespace



30
31
32
# File 'lib/jets/controller/renderers/template_renderer.rb', line 30

def template_namespace
  @controller.class.to_s.sub('Controller','').underscore.pluralize
end