lib/jets/controller/rendering/rack_renderer.rb in jets-1.9.32 vs lib/jets/controller/rendering/rack_renderer.rb in jets-2.0.0
- old
+ new
@@ -24,33 +24,24 @@
headers = @options[:headers] || {}
set_content_type!(status, headers)
# x-jets-base64 to convert this Rack triplet to a API Gateway hash structure later
headers["x-jets-base64"] = base64 ? 'yes' : 'no' # headers values must be Strings
- # Rails rendering does heavy lifting
if drop_content_info?(status)
body = StringIO.new
else
-
+ # Rails rendering does heavy lifting
+ # _prefixes provided by jets/overrides/rails/action_controller.rb
+ ActionController::Base._prefixes = @controller.controller_paths
renderer = ActionController::Base.renderer.new(renderer_options)
body = renderer.render(render_options)
body = StringIO.new(body)
end
[status, headers, body] # triplet
end
- # Example: posts/index
- def default_template_name
- "#{template_namespace}/#{@controller.meth}"
- end
-
- # PostsController => "posts" is the namespace
- def template_namespace
- @controller.class.to_s.sub('Controller','').underscore.pluralize
- end
-
# default options:
# https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/renderer.rb#L41-L47
def renderer_options
options = {
# script_name: "", # unfortunately doesnt seem to effect relative_url_root like desired
@@ -72,10 +63,45 @@
# Note @options[:method] uses @options vs options on purpose
@options[:method] = event["httpMethod"].downcase if event["httpMethod"]
options
end
+ def render_options
+ # normalize 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,
+ # prefixes: ["posts"],
+ }
+ 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
+
+ # Example: posts/index
+ def default_template_name
+ "#{template_namespace}/#{@controller.meth}"
+ end
+
+ # PostsController => "posts" is the namespace
+ def template_namespace
+ @controller.class.to_s.sub('Controller','').underscore.pluralize
+ end
+
# Takes headers and adds HTTP_ to front of the keys because that is what rack
# does to the headers passed from a request. This seems to be the standard
# when testing with curl and inspecting the headers in a Rack app. Example:
# https://gist.github.com/tongueroo/94f22f6c261c8999e4f4f776547e2ee3
#
@@ -108,41 +134,21 @@
results[rack_key] = v
end
results
end
- 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
-
+ # Pass controller instance variables from jets-based controller to ActionView scope
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
+ # jets internal variables
+ # So ActionView has access back to the jets controller
+ instance_vars[:_jets] = { controller: @controller }
instance_vars
end
private
# From jets/controller/response.rb
@@ -197,10 +203,11 @@
# Load helpers
# Assign local variable because scope in the `:action_view do` block changes
app_helper_classes = find_app_helper_classes
ActiveSupport.on_load :action_view do
- include ApplicationHelper # include first
+ include Jets::Router::Helpers # internal routes helpers
+ include ApplicationHelper # include first
app_helper_classes.each do |helper_class|
include helper_class
end
end