Class: Jets::Controller::Renderers::TemplateRenderer
- Inherits:
-
BaseRenderer
- Object
- BaseRenderer
- Jets::Controller::Renderers::TemplateRenderer
show all
- Defined in:
- lib/jets/controller/renderers/template_renderer.rb
Instance Attribute Summary
Attributes inherited from BaseRenderer
#controller
Instance Method Summary
collapse
#initialize, #render_aws_proxy
Instance Method Details
#controller_instance_variables ⇒ Object
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(/^@/,'') vars[k] = @controller.instance_variable_get(v)
vars
end
instance_vars[:event] = event
instance_vars
end
|
#default_template_name ⇒ Object
25
26
27
|
# File 'lib/jets/controller/renderers/template_renderer.rb', line 25
def default_template_name
"#{template_namespace}/#{@controller.meth}"
end
|
#render ⇒ Object
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
renderer = ActionController::Base.renderer.new(renderer_options)
body = renderer.render(render_options)
@options[:body] = body
render_aws_proxy(@options)
end
|
#render_options ⇒ Object
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
template = @options[:template]
if template and !template.include?('/')
template = "#{template_namespace}/#{template}"
end
template ||= default_template_name
@options[:template] = template if @options[:template]
render_options = {
template: template, 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_options ⇒ Object
default options:
https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/renderer.rb
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 = ["origin"]
if origin
uri = URI.parse(origin)
https = uri.scheme == "https"
end
options = {
http_host: ["host"],
https: https,
}
@options[:method] = event["httpMethod"].downcase if event["httpMethod"]
options
end
|
#setup_action_controller ⇒ Object
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"
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_webpacker ⇒ Object
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_namespace ⇒ Object
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
|