diff options
author | Aaron Patterson <tenderlove@github.com> | 2019-03-30 13:33:52 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-30 13:33:52 -0700 |
commit | 2bf55179813190f2a79509383e21c2b43e72f4c2 (patch) | |
tree | 220eca9ebaa2e54df78eb1d4430ab8fbc39dcddc /actionview/lib/action_view | |
parent | 197fe2222e553354823fd7685e9d174281e33c3c (diff) | |
parent | c7820d8124c854760a4d288334f185de2fb99446 (diff) | |
download | rails-2bf55179813190f2a79509383e21c2b43e72f4c2.tar.gz rails-2bf55179813190f2a79509383e21c2b43e72f4c2.tar.bz2 rails-2bf55179813190f2a79509383e21c2b43e72f4c2.zip |
Merge pull request #35688 from jhawthorn/render_file_rfc
RFC: Introduce Template::File
Diffstat (limited to 'actionview/lib/action_view')
-rw-r--r-- | actionview/lib/action_view/file_template.rb | 2 | ||||
-rw-r--r-- | actionview/lib/action_view/renderer/template_renderer.rb | 7 | ||||
-rw-r--r-- | actionview/lib/action_view/template.rb | 3 | ||||
-rw-r--r-- | actionview/lib/action_view/template/error.rb | 2 | ||||
-rw-r--r-- | actionview/lib/action_view/template/file.rb | 28 |
5 files changed, 38 insertions, 4 deletions
diff --git a/actionview/lib/action_view/file_template.rb b/actionview/lib/action_view/file_template.rb index dea02176eb..e0dc7da3b6 100644 --- a/actionview/lib/action_view/file_template.rb +++ b/actionview/lib/action_view/file_template.rb @@ -11,7 +11,7 @@ module ActionView end def source - File.binread @filename + ::File.binread @filename end def refresh(_) diff --git a/actionview/lib/action_view/renderer/template_renderer.rb b/actionview/lib/action_view/renderer/template_renderer.rb index 9548fe12c4..60cb2ceafa 100644 --- a/actionview/lib/action_view/renderer/template_renderer.rb +++ b/actionview/lib/action_view/renderer/template_renderer.rb @@ -26,7 +26,12 @@ module ActionView elsif options.key?(:html) Template::HTML.new(options[:html], formats.first) elsif options.key?(:file) - @lookup_context.with_fallbacks.find_file(options[:file], nil, false, keys, @details) + if File.exist?(options[:file]) + Template::File.new(options[:file]) + else + ActiveSupport::Deprecation.warn "render file: should be given the absolute path to a file" + @lookup_context.with_fallbacks.find_file(options[:file], nil, false, keys, @details) + end elsif options.key?(:inline) handler = Template.handler_for_extension(options[:type] || "erb") format = if handler.respond_to?(:default_format) diff --git a/actionview/lib/action_view/template.rb b/actionview/lib/action_view/template.rb index 7bc5e86edb..ebe52532d6 100644 --- a/actionview/lib/action_view/template.rb +++ b/actionview/lib/action_view/template.rb @@ -113,6 +113,7 @@ module ActionView eager_autoload do autoload :Error + autoload :File autoload :Handlers autoload :HTML autoload :Inline @@ -139,7 +140,7 @@ module ActionView @virtual_path = virtual_path @variable = if @virtual_path - base = @virtual_path[-1] == "/" ? "" : File.basename(@virtual_path) + base = @virtual_path[-1] == "/" ? "" : ::File.basename(@virtual_path) base =~ /\A_?(.*?)(?:\.\w+)*\z/ $1.to_sym end diff --git a/actionview/lib/action_view/template/error.rb b/actionview/lib/action_view/template/error.rb index d0ea03e228..aace0be04d 100644 --- a/actionview/lib/action_view/template/error.rb +++ b/actionview/lib/action_view/template/error.rb @@ -104,7 +104,7 @@ module ActionView def line_number @line_number ||= if file_name - regexp = /#{Regexp.escape File.basename(file_name)}:(\d+)/ + regexp = /#{Regexp.escape ::File.basename(file_name)}:(\d+)/ $1 if message =~ regexp || backtrace.find { |line| line =~ regexp } end end diff --git a/actionview/lib/action_view/template/file.rb b/actionview/lib/action_view/template/file.rb new file mode 100644 index 0000000000..487e5735cf --- /dev/null +++ b/actionview/lib/action_view/template/file.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module ActionView #:nodoc: + # = Action View File Template + class Template #:nodoc: + class File #:nodoc: + attr_accessor :type, :format + + def initialize(filename) + @filename = filename.to_s + extname = ::File.extname(filename).delete(".") + @type = Template::Types[extname] || Template::Types[:text] + @format = @type.symbol + end + + def identifier + @filename + end + + def render(*args) + ::File.read(@filename) + end + + def formats; Array(format); end + deprecate :formats + end + end +end |