Class: Jets::Controller::Renderers::TemplateRenderer
- Inherits:
-
BaseRenderer
- Object
- BaseRenderer
- Jets::Controller::Renderers::TemplateRenderer
- Defined in:
- lib/jets/controller/renderers/template_renderer.rb
Instance Attribute Summary
Attributes inherited from BaseRenderer
Class Method Summary collapse
-
.find_app_helper_classes ⇒ Object
Does not include ApplicationHelper, will include ApplicationHelper explicitly first.
- .setup! ⇒ Object
- .setup_webpacker ⇒ Object
Instance Method Summary collapse
- #controller_instance_variables ⇒ Object
-
#default_template_name ⇒ Object
Example: posts/index.
- #render ⇒ Object
- #render_options ⇒ Object
- #renderer_options ⇒ Object
-
#template_namespace ⇒ Object
PostsController => “posts” is the namespace.
Methods inherited from BaseRenderer
#initialize, #render_aws_proxy
Constructor Details
This class inherits a constructor from Jets::Controller::Renderers::BaseRenderer
Class Method Details
.find_app_helper_classes ⇒ Object
Does not include ApplicationHelper, will include ApplicationHelper explicitly first.
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/jets/controller/renderers/template_renderer.rb', line 95 def find_app_helper_classes klasses = [] expression = "#{Jets.root}app/helpers/**/*" Dir.glob(expression).each do |path| next unless File.file?(path) class_name = path.sub("#{Jets.root}app/helpers/","").sub(/\.rb/,'') unless class_name == "application_helper" klasses << class_name.classify.constantize # autoload end end klasses end |
.setup! ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/jets/controller/renderers/template_renderer.rb', line 75 def setup! require "action_controller" require "jets/rails_overrides" # Load helpers # Assign local variable because scoe in the `:action_view do` changes app_helper_classes = find_app_helper_classes ActiveSupport.on_load :action_view do include ApplicationHelper # include first app_helper_classes.each do |helper_class| include helper_class end end ActionController::Base.append_view_path("#{Jets.root}app/views") setup_webpacker if Jets.webpacker? end |
.setup_webpacker ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/jets/controller/renderers/template_renderer.rb', line 108 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 |
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(/^@/,'') # @var => var vars[k] = @controller.instance_variable_get(v) vars end instance_vars[:event] = event instance_vars end |
#default_template_name ⇒ Object
Example: posts/index
23 24 25 |
# File 'lib/jets/controller/renderers/template_renderer.rb', line 23 def default_template_name "#{template_namespace}/#{@controller.meth}" end |
#render ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/jets/controller/renderers/template_renderer.rb', line 13 def render # Rails rendering does heavy lifting renderer = ActionController::Base.renderer.new() body = renderer.render() [:body] = body # important to set as it was originally nil render_aws_proxy() end |
#render_options ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/jets/controller/renderers/template_renderer.rb', line 50 def # nomralize the template option template = [:template] if template and !template.include?('/') template = "#{template_namespace}/#{template}" end template ||= default_template_name # ready to override @options[:template] [:template] = template if [:template] = { 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: [:layout], assigns: controller_instance_variables, } types = %w[json inline plain file xml body action].map(&:to_sym) types.each do |type| [type] = [type] if [type] end end |
#renderer_options ⇒ Object
default options:
https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/renderer.rb#L41-L47
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/jets/controller/renderers/template_renderer.rb', line 34 def origin = headers["origin"] if origin uri = URI.parse(origin) https = uri.scheme == "https" end = { http_host: headers["host"], https: https, # script_name: "", # input: "" } [:method] = event["httpMethod"].downcase if event["httpMethod"] end |
#template_namespace ⇒ Object
PostsController => “posts” is the namespace
28 29 30 |
# File 'lib/jets/controller/renderers/template_renderer.rb', line 28 def template_namespace @controller.class.to_s.sub('Controller','').underscore.pluralize end |