diff options
Diffstat (limited to 'actionview/lib/action_view/template')
-rw-r--r-- | actionview/lib/action_view/template/error.rb | 9 | ||||
-rw-r--r-- | actionview/lib/action_view/template/handlers.rb | 8 | ||||
-rw-r--r-- | actionview/lib/action_view/template/handlers/builder.rb | 10 | ||||
-rw-r--r-- | actionview/lib/action_view/template/handlers/erb.rb | 23 | ||||
-rw-r--r-- | actionview/lib/action_view/template/html.rb | 4 | ||||
-rw-r--r-- | actionview/lib/action_view/template/resolver.rb | 152 | ||||
-rw-r--r-- | actionview/lib/action_view/template/text.rb | 4 | ||||
-rw-r--r-- | actionview/lib/action_view/template/types.rb | 2 |
8 files changed, 107 insertions, 105 deletions
diff --git a/actionview/lib/action_view/template/error.rb b/actionview/lib/action_view/template/error.rb index 3f38c3d2b9..b95e5236a0 100644 --- a/actionview/lib/action_view/template/error.rb +++ b/actionview/lib/action_view/template/error.rb @@ -1,4 +1,5 @@ require "active_support/core_ext/enumerable" +require "active_support/core_ext/regexp" module ActionView # = Action View Errors @@ -35,10 +36,10 @@ module ActionView prefixes = Array(prefixes) template_type = if partial "partial" - elsif path =~ /layouts/i - 'layout' + elsif /layouts/i.match?(path) + "layout" else - 'template' + "template" end if partial && path.present? @@ -130,7 +131,7 @@ module ActionView if line_number "on line ##{line_number} of " else - 'in ' + "in " end + file_name end diff --git a/actionview/lib/action_view/template/handlers.rb b/actionview/lib/action_view/template/handlers.rb index ad4c353608..f4301f6f07 100644 --- a/actionview/lib/action_view/template/handlers.rb +++ b/actionview/lib/action_view/template/handlers.rb @@ -2,10 +2,10 @@ module ActionView #:nodoc: # = Action View Template Handlers class Template module Handlers #:nodoc: - autoload :Raw, 'action_view/template/handlers/raw' - autoload :ERB, 'action_view/template/handlers/erb' - autoload :Html, 'action_view/template/handlers/html' - autoload :Builder, 'action_view/template/handlers/builder' + autoload :Raw, "action_view/template/handlers/raw" + autoload :ERB, "action_view/template/handlers/erb" + autoload :Html, "action_view/template/handlers/html" + autoload :Builder, "action_view/template/handlers/builder" def self.extended(base) base.register_default_template_handler :raw, Raw.new diff --git a/actionview/lib/action_view/template/handlers/builder.rb b/actionview/lib/action_view/template/handlers/builder.rb index d90b0c6378..e08a5b5db8 100644 --- a/actionview/lib/action_view/template/handlers/builder.rb +++ b/actionview/lib/action_view/template/handlers/builder.rb @@ -15,12 +15,12 @@ module ActionView protected - def require_engine - @required ||= begin - require "builder" - true + def require_engine + @required ||= begin + require "builder" + true + end end - end end end end diff --git a/actionview/lib/action_view/template/handlers/erb.rb b/actionview/lib/action_view/template/handlers/erb.rb index 85a100ed4c..6f07de1813 100644 --- a/actionview/lib/action_view/template/handlers/erb.rb +++ b/actionview/lib/action_view/template/handlers/erb.rb @@ -1,4 +1,5 @@ -require 'erubis' +require "erubis" +require "active_support/core_ext/regexp" module ActionView class Template @@ -28,7 +29,7 @@ module ActionView # We override to always treat <%== as escaped. def add_expr(src, code, indicator) case indicator - when '==' + when "==" add_expr_escaped(src, code) else super @@ -39,16 +40,16 @@ module ActionView def add_expr_literal(src, code) flush_newline_if_pending(src) - if code =~ BLOCK_EXPR - src << '@output_buffer.append= ' << code + if BLOCK_EXPR.match?(code) + src << "@output_buffer.append= " << code else - src << '@output_buffer.append=(' << code << ');' + src << "@output_buffer.append=(" << code << ");" end end def add_expr_escaped(src, code) flush_newline_if_pending(src) - if code =~ BLOCK_EXPR + if BLOCK_EXPR.match?(code) src << "@output_buffer.safe_expr_append= " << code else src << "@output_buffer.safe_expr_append=(" << code << ");" @@ -62,7 +63,7 @@ module ActionView def add_postamble(src) flush_newline_if_pending(src) - src << '@output_buffer.to_s' + src << "@output_buffer.to_s" end def flush_newline_if_pending(src) @@ -77,7 +78,7 @@ module ActionView # Specify trim mode for the ERB compiler. Defaults to '-'. # See ERB documentation for suitable values. class_attribute :erb_trim_mode - self.erb_trim_mode = '-' + self.erb_trim_mode = "-" # Default implementation used. class_attribute :erb_implementation @@ -108,7 +109,7 @@ module ActionView # expression template_source = template.source.dup.force_encoding(Encoding::ASCII_8BIT) - erb = template_source.gsub(ENCODING_TAG, '') + erb = template_source.gsub(ENCODING_TAG, "") encoding = $2 erb.force_encoding valid_encoding(template.source.dup, encoding) @@ -118,8 +119,8 @@ module ActionView self.class.erb_implementation.new( erb, - :escape => (self.class.escape_whitelist.include? template.type), - :trim => (self.class.erb_trim_mode == "-") + escape: (self.class.escape_whitelist.include? template.type), + trim: (self.class.erb_trim_mode == "-") ).src end diff --git a/actionview/lib/action_view/template/html.rb b/actionview/lib/action_view/template/html.rb index 0321f819b5..ddbd2ea36a 100644 --- a/actionview/lib/action_view/template/html.rb +++ b/actionview/lib/action_view/template/html.rb @@ -11,11 +11,11 @@ module ActionView #:nodoc: end def identifier - 'html template' + "html template" end def inspect - 'html template' + "html template" end def to_str diff --git a/actionview/lib/action_view/template/resolver.rb b/actionview/lib/action_view/template/resolver.rb index bf68e93c58..33d60be144 100644 --- a/actionview/lib/action_view/template/resolver.rb +++ b/actionview/lib/action_view/template/resolver.rb @@ -37,7 +37,7 @@ module ActionView class Cache #:nodoc: class SmallCache < Concurrent::Map def initialize(options = {}) - super(options.merge(:initial_capacity => 2)) + super(options.merge(initial_capacity: 2)) end end @@ -107,22 +107,22 @@ module ActionView private - def canonical_no_templates(templates) - templates.empty? ? NO_TEMPLATES : templates - end - - def templates_have_changed?(cached_templates, fresh_templates) - # if either the old or new template list is empty, we don't need to (and can't) - # compare modification times, and instead just check whether the lists are different - if cached_templates.blank? || fresh_templates.blank? - return fresh_templates.blank? != cached_templates.blank? + def canonical_no_templates(templates) + templates.empty? ? NO_TEMPLATES : templates end - cached_templates_max_updated_at = cached_templates.map(&:updated_at).max + def templates_have_changed?(cached_templates, fresh_templates) + # if either the old or new template list is empty, we don't need to (and can't) + # compare modification times, and instead just check whether the lists are different + if cached_templates.blank? || fresh_templates.blank? + return fresh_templates.blank? != cached_templates.blank? + end - # if a template has changed, it will be now be newer than all the cached templates - fresh_templates.any? { |t| t.updated_at > cached_templates_max_updated_at } - end + cached_templates_max_updated_at = cached_templates.map(&:updated_at).max + + # if a template has changed, it will be now be newer than all the cached templates + fresh_templates.any? { |t| t.updated_at > cached_templates_max_updated_at } + end end cattr_accessor :caching @@ -204,7 +204,7 @@ module ActionView # An abstract class that implements a Resolver with path semantics. class PathResolver < Resolver #:nodoc: - EXTENSIONS = { :locale => ".", :formats => ".", :variants => "+", :handlers => "." } + EXTENSIONS = { locale: ".", formats: ".", variants: "+", handlers: "." } DEFAULT_PATTERN = ":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}" def initialize(pattern=nil) @@ -214,93 +214,93 @@ module ActionView private - def find_templates(name, prefix, partial, details, outside_app_allowed = false) - path = Path.build(name, prefix, partial) - query(path, details, details[:formats], outside_app_allowed) - end + def find_templates(name, prefix, partial, details, outside_app_allowed = false) + path = Path.build(name, prefix, partial) + query(path, details, details[:formats], outside_app_allowed) + end - def query(path, details, formats, outside_app_allowed) - query = build_query(path, details) + def query(path, details, formats, outside_app_allowed) + query = build_query(path, details) - template_paths = find_template_paths(query) - template_paths = reject_files_external_to_app(template_paths) unless outside_app_allowed + template_paths = find_template_paths(query) + template_paths = reject_files_external_to_app(template_paths) unless outside_app_allowed - template_paths.map do |template| - handler, format, variant = extract_handler_and_format_and_variant(template, formats) - contents = File.binread(template) + template_paths.map do |template| + handler, format, variant = extract_handler_and_format_and_variant(template, formats) + contents = File.binread(template) - Template.new(contents, File.expand_path(template), handler, - :virtual_path => path.virtual, - :format => format, - :variant => variant, - :updated_at => mtime(template) - ) + Template.new(contents, File.expand_path(template), handler, + virtual_path: path.virtual, + format: format, + variant: variant, + updated_at: mtime(template) + ) + end end - end - def reject_files_external_to_app(files) - files.reject { |filename| !inside_path?(@path, filename) } - end + def reject_files_external_to_app(files) + files.reject { |filename| !inside_path?(@path, filename) } + end - def find_template_paths(query) - Dir[query].uniq.reject do |filename| - File.directory?(filename) || - # deals with case-insensitive file systems. - !File.fnmatch(query, filename, File::FNM_EXTGLOB) + def find_template_paths(query) + Dir[query].uniq.reject do |filename| + File.directory?(filename) || + # deals with case-insensitive file systems. + !File.fnmatch(query, filename, File::FNM_EXTGLOB) + end end - end - def inside_path?(path, filename) - filename = File.expand_path(filename) - path = File.join(path, '') - filename.start_with?(path) - end + def inside_path?(path, filename) + filename = File.expand_path(filename) + path = File.join(path, "") + filename.start_with?(path) + end # Helper for building query glob string based on resolver's pattern. - def build_query(path, details) - query = @pattern.dup + def build_query(path, details) + query = @pattern.dup - prefix = path.prefix.empty? ? '' : "#{escape_entry(path.prefix)}\\1" - query.gsub!(/:prefix(\/)?/, prefix) + prefix = path.prefix.empty? ? "" : "#{escape_entry(path.prefix)}\\1" + query.gsub!(/:prefix(\/)?/, prefix) - partial = escape_entry(path.partial? ? "_#{path.name}" : path.name) - query.gsub!(/:action/, partial) + partial = escape_entry(path.partial? ? "_#{path.name}" : path.name) + query.gsub!(/:action/, partial) - details.each do |ext, candidates| - if ext == :variants && candidates == :any - query.gsub!(/:#{ext}/, "*") - else - query.gsub!(/:#{ext}/, "{#{candidates.compact.uniq.join(',')}}") + details.each do |ext, candidates| + if ext == :variants && candidates == :any + query.gsub!(/:#{ext}/, "*") + else + query.gsub!(/:#{ext}/, "{#{candidates.compact.uniq.join(',')}}") + end end - end - File.expand_path(query, @path) - end + File.expand_path(query, @path) + end - def escape_entry(entry) - entry.gsub(/[*?{}\[\]]/, '\\\\\\&'.freeze) - end + def escape_entry(entry) + entry.gsub(/[*?{}\[\]]/, '\\\\\\&'.freeze) + end # Returns the file mtime from the filesystem. - def mtime(p) - File.mtime(p) - end + def mtime(p) + File.mtime(p) + end # Extract handler, formats and variant from path. If a format cannot be found neither # from the path, or the handler, we should return the array of formats given # to the resolver. - def extract_handler_and_format_and_variant(path, default_formats) - pieces = File.basename(path).split('.'.freeze) - pieces.shift + def extract_handler_and_format_and_variant(path, default_formats) + pieces = File.basename(path).split(".".freeze) + pieces.shift - extension = pieces.pop + extension = pieces.pop - handler = Template.handler_for_extension(extension) - format, variant = pieces.last.split(EXTENSIONS[:variants], 2) if pieces.last - format &&= Template::Types[format] + handler = Template.handler_for_extension(extension) + format, variant = pieces.last.split(EXTENSIONS[:variants], 2) if pieces.last + format &&= Template::Types[format] - [handler, format, variant] - end + [handler, format, variant] + end end # A resolver that loads files from the filesystem. It allows setting your own diff --git a/actionview/lib/action_view/template/text.rb b/actionview/lib/action_view/template/text.rb index 04f5b8d17a..898593e702 100644 --- a/actionview/lib/action_view/template/text.rb +++ b/actionview/lib/action_view/template/text.rb @@ -11,11 +11,11 @@ module ActionView #:nodoc: end def identifier - 'text template' + "text template" end def inspect - 'text template' + "text template" end def to_str diff --git a/actionview/lib/action_view/template/types.rb b/actionview/lib/action_view/template/types.rb index b32567cd66..21959a3798 100644 --- a/actionview/lib/action_view/template/types.rb +++ b/actionview/lib/action_view/template/types.rb @@ -1,4 +1,4 @@ -require 'active_support/core_ext/module/attribute_accessors' +require "active_support/core_ext/module/attribute_accessors" module ActionView class Template |