method
render_call_options
v8.1.1 -
Show latest stable
- Class:
ActionView::RenderParser::PrismRenderParser
render_call_options(node)private
Accept a call node and return a hash of options for the render call. If it doesn’t match the expected format, return nil.
# File actionview/lib/action_view/render_parser/prism_render_parser.rb, line 43
def render_call_options(node)
# We are only looking for calls to render or render_to_string.
name = node.name.to_sym
return if name != :render && name != :render_to_string
# We are only looking for calls with arguments.
arguments = node.arguments
return unless arguments
arguments = arguments.arguments
length = arguments.length
# Get rid of any parentheses to get directly to the contents.
arguments.map! do |argument|
current = argument
while current.is_a?(Prism::ParenthesesNode) &&
current.body.is_a?(Prism::StatementsNode) &&
current.body.body.length == 1
current = current.body.body.first
end
current
end
# We are only looking for arguments that are either a string with an
# array of locals or a keyword hash with symbol keys.
options =
if (length == 1 || length == 2) && !arguments[0].is_a?(Prism::KeywordHashNode)
{ partial: arguments[0], locals: arguments[1] }
elsif length == 1 &&
arguments[0].is_a?(Prism::KeywordHashNode) &&
arguments[0].elements.all? do |element|
element.is_a?(Prism::AssocNode) && element.key.is_a?(Prism::SymbolNode)
end
arguments[0].elements.to_h do |element|
[element.key.unescaped.to_sym, element.value]
end
end
return unless options
# Here we validate that the options have the keys we expect.
keys = options.keys
return if !keys.intersect?(RENDER_TYPE_KEYS)
return if (keys - ALL_KNOWN_KEYS).any?
# Finally, we can return a valid set of options.
options
end Related methods
- Instance methods
- render_calls
- Private methods
-
render_call_options -
render_call_template