Rename ActionView::Template::Path ActionView::Resolver

上级 7ac9f290
......@@ -33,21 +33,21 @@ def self.load_all!
[Base, InlineTemplate, TemplateError]
end
autoload :Base, 'action_view/base'
autoload :Helpers, 'action_view/helpers'
autoload :InlineTemplate, 'action_view/template/inline'
autoload :Partials, 'action_view/render/partials'
autoload :Path, 'action_view/template/path'
autoload :PathSet, 'action_view/paths'
autoload :Rendering, 'action_view/render/rendering'
autoload :Renderable, 'action_view/template/renderable'
autoload :Base, 'action_view/base'
autoload :Helpers, 'action_view/helpers'
autoload :InlineTemplate, 'action_view/template/inline'
autoload :Partials, 'action_view/render/partials'
autoload :Resolver, 'action_view/template/path'
autoload :PathSet, 'action_view/paths'
autoload :Rendering, 'action_view/render/rendering'
autoload :Renderable, 'action_view/template/renderable'
autoload :RenderablePartial, 'action_view/template/partial'
autoload :Template, 'action_view/template/template'
autoload :TemplateError, 'action_view/template/error'
autoload :TemplateHandler, 'action_view/template/handler'
autoload :TemplateHandlers, 'action_view/template/handlers'
autoload :TextTemplate, 'action_view/template/text'
autoload :Helpers, 'action_view/helpers'
autoload :Template, 'action_view/template/template'
autoload :TemplateError, 'action_view/template/error'
autoload :TemplateHandler, 'action_view/template/handler'
autoload :TemplateHandlers, 'action_view/template/handlers'
autoload :TextTemplate, 'action_view/template/text'
autoload :Helpers, 'action_view/helpers'
end
class ERB
......
......@@ -3,7 +3,7 @@ class PathSet < Array #:nodoc:
def self.type_cast(obj)
if obj.is_a?(String)
cache = !defined?(Rails) || !Rails.respond_to?(:configuration) || Rails.configuration.cache_classes
Template::FileSystemPathWithFallback.new(obj, :cache => cache)
FileSystemResolverWithFallback.new(obj, :cache => cache)
else
obj
end
......
require "pathname"
require "action_view/template/template"
module ActionView
class Template
# Abstract super class
class Path
def initialize(options)
@cache = options[:cache]
@cached = {}
end
# Normalizes the arguments and passes it on to find_template
def find_by_parts(*args)
find_all_by_parts(*args).first
end
def find_all_by_parts(name, details = {}, prefix = nil, partial = nil)
details[:locales] = [I18n.locale]
name = name.to_s.gsub(handler_matcher, '').split("/")
find_templates(name.pop, details, [prefix, *name].compact.join("/"), partial)
end
# Abstract superclass
class Resolver
def initialize(options)
@cache = options[:cache]
@cached = {}
end
# Normalizes the arguments and passes it on to find_template
def find_by_parts(*args)
find_all_by_parts(*args).first
end
private
# This is what child classes implement. No defaults are needed
# because Path guarentees that the arguments are present and
# normalized.
def find_templates(name, details, prefix, partial)
raise NotImplementedError
end
def valid_handlers
@valid_handlers ||= TemplateHandlers.extensions
end
def handler_matcher
@handler_matcher ||= begin
e = valid_handlers.join('|')
/\.(?:#{e})$/
end
end
def handler_glob
e = TemplateHandlers.extensions.map{|h| ".#{h},"}.join
"{#{e}}"
end
def formats_glob
@formats_glob ||= begin
'{' + Mime::SET.symbols.map { |l| ".#{l}," }.join + '}'
end
end
def cached(key)
return yield unless @cache
return @cached[key] if @cached.key?(key)
@cached[key] = yield
end
def find_all_by_parts(name, details = {}, prefix = nil, partial = nil)
details[:locales] = [I18n.locale]
name = name.to_s.gsub(handler_matcher, '').split("/")
find_templates(name.pop, details, [prefix, *name].compact.join("/"), partial)
end
class FileSystemPath < Path
private
# This is what child classes implement. No defaults are needed
# because Resolver guarantees that the arguments are present and
# normalized.
def find_templates(name, details, prefix, partial)
raise NotImplementedError
end
def initialize(path, options = {})
raise ArgumentError, "path already is a Path class" if path.is_a?(Path)
super(options)
@path = Pathname.new(path).expand_path
def valid_handlers
@valid_handlers ||= TemplateHandlers.extensions
end
def handler_matcher
@handler_matcher ||= begin
e = valid_handlers.join('|')
/\.(?:#{e})$/
end
end
# TODO: This is the currently needed API. Make this suck less
# ==== <suck>
attr_reader :path
def handler_glob
e = TemplateHandlers.extensions.map{|h| ".#{h},"}.join
"{#{e}}"
end
def to_s
path.to_s
def formats_glob
@formats_glob ||= begin
'{' + Mime::SET.symbols.map { |l| ".#{l}," }.join + '}'
end
end
def to_str
path.to_s
end
def cached(key)
return yield unless @cache
return @cached[key] if @cached.key?(key)
@cached[key] = yield
end
end
def ==(path)
to_str == path.to_str
end
class FileSystemResolver < Resolver
def eql?(path)
to_str == path.to_str
end
# ==== </suck>
def find_templates(name, details, prefix, partial, root = "#{@path}/")
if glob = details_to_glob(name, details, prefix, partial, root)
cached(glob) do
Dir[glob].map do |path|
next if File.directory?(path)
source = File.read(path)
identifier = Pathname.new(path).expand_path.to_s
Template.new(source, identifier, *path_to_details(path))
end.compact
end
def initialize(path, options = {})
raise ArgumentError, "path already is a Resolver class" if path.is_a?(Resolver)
super(options)
@path = Pathname.new(path).expand_path
end
# TODO: This is the currently needed API. Make this suck less
# ==== <suck>
attr_reader :path
def to_s
path.to_s
end
def to_str
path.to_s
end
def ==(path)
to_str == path.to_str
end
def eql?(path)
to_str == path.to_str
end
# ==== </suck>
def find_templates(name, details, prefix, partial, root = "#{@path}/")
if glob = details_to_glob(name, details, prefix, partial, root)
cached(glob) do
Dir[glob].map do |path|
next if File.directory?(path)
source = File.read(path)
identifier = Pathname.new(path).expand_path.to_s
Template.new(source, identifier, *path_to_details(path))
end.compact
end
end
private
end
# :api: plugin
def details_to_glob(name, details, prefix, partial, root)
path = ""
path << "#{prefix}/" unless prefix.empty?
path << (partial ? "_#{name}" : name)
extensions = ""
[:locales, :formats].each do |k|
extensions << if exts = details[k]
'{' + exts.map {|e| ".#{e},"}.join + '}'
else
k == :formats ? formats_glob : ''
end
private
# :api: plugin
def details_to_glob(name, details, prefix, partial, root)
path = ""
path << "#{prefix}/" unless prefix.empty?
path << (partial ? "_#{name}" : name)
extensions = ""
[:locales, :formats].each do |k|
extensions << if exts = details[k]
'{' + exts.map {|e| ".#{e},"}.join + '}'
else
k == :formats ? formats_glob : ''
end
"#{root}#{path}#{extensions}#{handler_glob}"
end
# TODO: fix me
# :api: plugin
def path_to_details(path)
# [:erb, :format => :html, :locale => :en, :partial => true/false]
if m = path.match(%r'/(_)?[\w-]+(\.[\w-]+)*\.(\w+)$')
partial = m[1] == '_'
details = (m[2]||"").split('.').reject { |e| e.empty? }
handler = Template.handler_class_for_extension(m[3])
format = Mime[details.last] && details.pop.to_sym
locale = details.last && details.pop.to_sym
return handler, :format => format, :locale => locale, :partial => partial
end
end
"#{root}#{path}#{extensions}#{handler_glob}"
end
class FileSystemPathWithFallback < FileSystemPath
def find_templates(name, details, prefix, partial)
templates = super
return super(name, details, prefix, partial, '') if templates.empty?
templates
# TODO: fix me
# :api: plugin
def path_to_details(path)
# [:erb, :format => :html, :locale => :en, :partial => true/false]
if m = path.match(%r'/(_)?[\w-]+(\.[\w-]+)*\.(\w+)$')
partial = m[1] == '_'
details = (m[2]||"").split('.').reject { |e| e.empty? }
handler = Template.handler_class_for_extension(m[3])
format = Mime[details.last] && details.pop.to_sym
locale = details.last && details.pop.to_sym
return handler, :format => format, :locale => locale, :partial => partial
end
end
end
class FileSystemResolverWithFallback < FileSystemResolver
def find_templates(name, details, prefix, partial)
templates = super
return super(name, details, prefix, partial, '') if templates.empty?
templates
end
end
end
\ No newline at end of file
......@@ -9,7 +9,7 @@ class Base < AbstractController::Base
include AbstractController::Renderer
include AbstractController::Layouts
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"layouts/hello.erb" => "With String <%= yield %>",
"layouts/hello_override.erb" => "With Override <%= yield %>",
"layouts/abstract_controller_tests/layouts/with_string_implied_child.erb" =>
......
module ActionView #:nodoc:
class Template
class FixturePath < Path
class FixtureResolver < Resolver
def initialize(hash = {}, options = {})
super(options)
@hash = hash
......@@ -65,40 +64,4 @@ def path_to_details(path)
end
end
end
# class FixtureTemplate < Template
# class FixturePath < Template::Path
# def initialize(hash = {})
# @hash = {}
#
# hash.each do |k, v|
# @hash[k.sub(/\.\w+$/, '')] = FixtureTemplate.new(v, k.split("/").last, self)
# end
#
# super("fixtures://root")
# end
#
# def find_template(path)
# @hash[path]
# end
# end
#
# def initialize(body, *args)
# @body = body
# super(*args)
# end
#
# def source
# @body
# end
#
# private
#
# def find_full_path(path, load_paths)
# return '/', path
# end
#
# end
end
end
\ No newline at end of file
......@@ -19,7 +19,7 @@ def set_on_render
class ImpliedController < ActionController::Base
# Template's mime type is used if no content_type is specified
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"content_type/implied/i_am_html_erb.html.erb" => "Hello world!",
"content_type/implied/i_am_xml_erb.xml.erb" => "<xml>Hello world!</xml>",
"content_type/implied/i_am_html_builder.html.builder" => "xml.p 'Hello'",
......
......@@ -2,7 +2,7 @@
module Etags
class BasicController < ActionController::Base
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"etags/basic/base.html.erb" => "Hello from without_layout.html.erb",
"layouts/etags.html.erb" => "teh <%= yield %> tagz"
)]
......
......@@ -3,7 +3,7 @@
module RenderAction
# This has no layout and it works
class BasicController < ActionController::Base
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"render_action/basic/hello_world.html.erb" => "Hello world!"
)]
......@@ -117,7 +117,7 @@ module RenderActionWithApplicationLayout
# # ==== Render actions with layouts ====
class BasicController < ::ApplicationController
# Set the view path to an application view structure with layouts
self.view_paths = self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = self.view_paths = [ActionView::FixtureResolver.new(
"render_action_with_application_layout/basic/hello_world.html.erb" => "Hello World!",
"render_action_with_application_layout/basic/hello.html.builder" => "xml.p 'Omg'",
"layouts/application.html.erb" => "OHAI <%= yield %> KTHXBAI",
......@@ -202,7 +202,7 @@ class TestLayout < SimpleRouteCase
module RenderActionWithControllerLayout
class BasicController < ActionController::Base
self.view_paths = self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = self.view_paths = [ActionView::FixtureResolver.new(
"render_action_with_controller_layout/basic/hello_world.html.erb" => "Hello World!",
"layouts/render_action_with_controller_layout/basic.html.erb" => "With Controller Layout! <%= yield %> KTHXBAI"
)]
......@@ -263,7 +263,7 @@ class ControllerLayoutTest < SimpleRouteCase
module RenderActionWithBothLayouts
class BasicController < ActionController::Base
self.view_paths = [ActionView::Template::FixturePath.new({
self.view_paths = [ActionView::FixtureResolver.new({
"render_action_with_both_layouts/basic/hello_world.html.erb" => "Hello World!",
"layouts/application.html.erb" => "OHAI <%= yield %> KTHXBAI",
"layouts/render_action_with_both_layouts/basic.html.erb" => "With Controller Layout! <%= yield %> KTHXBAI"
......
......@@ -2,7 +2,7 @@
module RenderImplicitAction
class SimpleController < ::ApplicationController
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"render_implicit_action/simple/hello_world.html.erb" => "Hello world!",
"render_implicit_action/simple/hyphen-ated.html.erb" => "Hello hyphen-ated!"
)]
......
......@@ -2,7 +2,7 @@
module ControllerLayouts
class ImplicitController < ::ApplicationController
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"layouts/application.html.erb" => "OMG <%= yield %> KTHXBAI",
"layouts/override.html.erb" => "Override! <%= yield %>",
"basic.html.erb" => "Hello world!",
......@@ -26,7 +26,7 @@ def builder_override
end
class ImplicitNameController < ::ApplicationController
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"layouts/controller_layouts/implicit_name.html.erb" => "OMGIMPLICIT <%= yield %> KTHXBAI",
"basic.html.erb" => "Hello world!"
)]
......@@ -68,7 +68,7 @@ class LayoutOptionsTest < SimpleRouteCase
end
class MismatchFormatController < ::ApplicationController
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"layouts/application.html.erb" => "<html><%= yield %></html>",
"controller_layouts/mismatch_format/index.js.rjs" => "page[:test].omg",
"controller_layouts/mismatch_format/implicit.rjs" => "page[:test].omg"
......
......@@ -4,7 +4,7 @@ module RenderPartial
class BasicController < ActionController::Base
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"render_partial/basic/_basic.html.erb" => "OMG!",
"render_partial/basic/basic.html.erb" => "<%= @test_unchanged = 'goodbye' %><%= render :partial => 'basic' %><%= @test_unchanged %>"
)]
......
......@@ -4,7 +4,7 @@ module RenderRjs
class BasicController < ActionController::Base
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"render_rjs/basic/index.js.rjs" => "page[:customer].replace_html render(:partial => 'customer')",
"render_rjs/basic/index_html.js.rjs" => "page[:customer].replace_html :partial => 'customer'",
"render_rjs/basic/_customer.js.erb" => "JS Partial",
......
......@@ -3,7 +3,7 @@
module RenderTemplate
class WithoutLayoutController < ActionController::Base
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"test/basic.html.erb" => "Hello from basic.html.erb",
"shared.html.erb" => "Elastica",
"locals.html.erb" => "The secret is <%= secret %>",
......@@ -79,7 +79,7 @@ class TestWithoutLayout < SimpleRouteCase
end
class WithLayoutController < ::ApplicationController
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"test/basic.html.erb" => "Hello from basic.html.erb",
"shared.html.erb" => "Elastica",
"layouts/application.html.erb" => "<%= yield %>, I'm here!",
......@@ -148,7 +148,7 @@ class TestWithLayout < SimpleRouteCase
module Compatibility
class WithoutLayoutController < ActionController::Base
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"test/basic.html.erb" => "Hello from basic.html.erb",
"shared.html.erb" => "Elastica"
)]
......
......@@ -2,7 +2,7 @@
module Render
class BlankRenderController < ActionController::Base
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"render/blank_render/index.html.erb" => "Hello world!",
"render/blank_render/access_request.html.erb" => "The request: <%= request.method.to_s.upcase %>",
"render/blank_render/access_action_name.html.erb" => "Action Name: <%= action_name %>",
......
......@@ -2,7 +2,7 @@
module RenderText
class SimpleController < ActionController::Base
self.view_paths = [ActionView::Template::FixturePath.new]
self.view_paths = [ActionView::FixtureResolver.new]
def index
render :text => "hello david"
......@@ -10,7 +10,7 @@ def index
end
class WithLayoutController < ::ApplicationController
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"layouts/application.html.erb" => "<%= yield %>, I'm here!",
"layouts/greetings.html.erb" => "<%= yield %>, I wish thee well.",
"layouts/ivar.html.erb" => "<%= yield %>, <%= @ivar %>"
......
......@@ -4,7 +4,7 @@ module RenderXml
# This has no layout and it works
class BasicController < ActionController::Base
self.view_paths = [ActionView::Template::FixturePath.new(
self.view_paths = [ActionView::FixtureResolver.new(
"render_xml/basic/with_render_erb" => "Hello world!"
)]
end
......
......@@ -41,7 +41,7 @@ def render_with_cache(*args)
end
def render_without_cache(*args)
path = ActionView::Template::FileSystemPathWithFallback.new(FIXTURE_LOAD_PATH)
path = ActionView::FileSystemResolverWithFallback.new(FIXTURE_LOAD_PATH)
view_paths = ActionView::Base.process_view_paths(path)
ActionView::Base.new(view_paths, {}).render(*args)
end
......
......@@ -255,7 +255,7 @@ class CachedViewRenderTest < ActiveSupport::TestCase
# Ensure view path cache is primed
def setup
view_paths = ActionController::Base.view_paths
assert_equal ActionView::Template::FileSystemPathWithFallback, view_paths.first.class
assert_equal ActionView::FileSystemResolverWithFallback, view_paths.first.class
setup_view(view_paths)
end
end
......@@ -266,9 +266,9 @@ class LazyViewRenderTest < ActiveSupport::TestCase
# Test the same thing as above, but make sure the view path
# is not eager loaded
def setup
path = ActionView::Template::FileSystemPathWithFallback.new(FIXTURE_LOAD_PATH)
path = ActionView::FileSystemResolverWithFallback.new(FIXTURE_LOAD_PATH)
view_paths = ActionView::Base.process_view_paths(path)
assert_equal ActionView::Template::FileSystemPathWithFallback, view_paths.first.class
assert_equal ActionView::FileSystemResolverWithFallback, view_paths.first.class
setup_view(view_paths)
end
end
......@@ -401,7 +401,7 @@ def load_observers
def load_view_paths
if configuration.frameworks.include?(:action_view)
if configuration.cache_classes
view_path = ActionView::Template::FileSystemPath.new(configuration.view_path)
view_path = ActionView::FileSystemResolverWithFallback.new(configuration.view_path)
ActionController::Base.view_paths = view_path if configuration.frameworks.include?(:action_controller)
ActionMailer::Base.template_root = view_path if configuration.frameworks.include?(:action_mailer)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册