1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add ActionView::Template::Sources::File

This commit is contained in:
John Hawthorn 2019-04-04 12:45:30 -07:00
parent b8e6594181
commit db1830a7ec
4 changed files with 38 additions and 24 deletions

View file

@ -5,29 +5,8 @@ require "action_view/template"
module ActionView module ActionView
class FileTemplate < Template class FileTemplate < Template
def initialize(filename, handler, details) def initialize(filename, handler, details)
@filename = filename source = ActionView::Template::Sources::File.new(filename)
super(source, filename, handler, details)
super(nil, filename, handler, details)
end
def source
File.binread @filename
end
def refresh(_)
self
end
# Exceptions are marshalled when using the parallel test runner with DRb, so we need
# to ensure that references to the template object can be marshalled as well. This means forgoing
# the marshalling of the compiler mutex and instantiating that again on unmarshalling.
def marshal_dump # :nodoc:
[ @identifier, @handler, @compiled, @locals, @virtual_path, @updated_at, @format, @variant ]
end
def marshal_load(array) # :nodoc:
@identifier, @handler, @compiled, @locals, @virtual_path, @updated_at, @format, @variant = *array
@compile_mutex = Mutex.new
end end
end end
end end

View file

@ -117,13 +117,14 @@ module ActionView
autoload :Handlers autoload :Handlers
autoload :HTML autoload :HTML
autoload :Inline autoload :Inline
autoload :Sources
autoload :Text autoload :Text
autoload :Types autoload :Types
end end
extend Template::Handlers extend Template::Handlers
attr_reader :source, :identifier, :handler, :original_encoding, :updated_at attr_reader :identifier, :handler, :original_encoding, :updated_at
attr_reader :variable, :format, :variant, :locals, :virtual_path attr_reader :variable, :format, :variant, :locals, :virtual_path
def initialize(source, identifier, handler, format: nil, variant: nil, locals: nil, virtual_path: nil, updated_at: nil) def initialize(source, identifier, handler, format: nil, variant: nil, locals: nil, virtual_path: nil, updated_at: nil)
@ -217,6 +218,10 @@ module ActionView
"#<#{self.class.name} #{short_identifier} locals=#{@locals.inspect}>" "#<#{self.class.name} #{short_identifier} locals=#{@locals.inspect}>"
end end
def source
@source.to_s
end
# This method is responsible for properly setting the encoding of the # This method is responsible for properly setting the encoding of the
# source. Until this point, we assume that the source is BINARY data. # source. Until this point, we assume that the source is BINARY data.
# If no additional information is supplied, we assume the encoding is # If no additional information is supplied, we assume the encoding is

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
module ActionView
class Template
module Sources
extend ActiveSupport::Autoload
eager_autoload do
autoload :File
end
end
end
end

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
module ActionView
class Template
module Sources
class File
def initialize(filename)
@filename = filename
end
def to_s
::File.binread @filename
end
end
end
end
end