Among other things, this method is responsible for properly setting the
encoding of the compiled template.
If the template engine handles encodings, we send the encoded String to the engine without further processing.
This allows the template engine to support additional mechanisms for
specifying the encoding. For instance, ERB
supports <%# encoding: %>
Otherwise, after we figure out the correct encoding, we then encode the source into
Encoding.default_internal. In general, this means that templates
will be UTF-8 inside of Rails, regardless of the
original source encoding.
# File actionview/lib/action_view/template.rb, line 501
def compile(mod)
begin
mod.module_eval(compiled_source, identifier, offset)
rescue SyntaxError
# Account for when code in the template is not syntactically valid; e.g. if we're using
# ERB and the user writes <%= foo( %>, attempting to call a helper `foo` and interpolate
# the result into the template, but missing an end parenthesis.
raise SyntaxErrorInTemplate.new(self, encode!)
end
return unless strict_locals?
parameters = mod.instance_method(method_name).parameters
parameters -= [[:req, :local_assigns], [:req, :output_buffer]]
# Check compiled method parameters to ensure that only kwargs
# were provided as strict locals, preventing `locals: (foo, *foo)` etc
# and allowing `locals: (foo:)`.
non_kwarg_parameters = parameters.select do |parameter|
![:keyreq, :key, :keyrest, :nokey].include?(parameter[0])
end
non_kwarg_parameters.pop if non_kwarg_parameters.last == %(block _)
unless non_kwarg_parameters.empty?
mod.undef_method(method_name)
raise ArgumentError.new(
"#{non_kwarg_parameters.map { |_, name| "`#{name}`" }.to_sentence} set as non-keyword " "#{'argument'.pluralize(non_kwarg_parameters.length)} for #{short_identifier}. " "Locals can only be set as keyword arguments."
)
end
unless parameters.any? { |type, _| type == :keyrest }
parameters.map!(&:last)
parameters.sort!
@strict_local_keys = parameters.freeze
end
end