Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Resolver::PathParser #39361

Merged
merged 6 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions actionmailer/lib/action_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ def self.eager_load!
ActiveSupport.on_load(:action_view) do
ActionView::Base.default_formats ||= Mime::SET.symbols
ActionView::Template::Types.delegate_to Mime
ActionView::LookupContext::DetailsKey.clear
end
1 change: 1 addition & 0 deletions actionpack/lib/action_dispatch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,5 @@ module Session
ActiveSupport.on_load(:action_view) do
ActionView::Base.default_formats ||= Mime::SET.symbols
ActionView::Template::Types.delegate_to Mime
ActionView::LookupContext::DetailsKey.clear
end
55 changes: 44 additions & 11 deletions actionview/lib/action_view/template/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,44 @@ def to_str
alias :to_s :to_str
end

class PathParser # :nodoc:
def initialize
@regex = build_path_regex
end

def build_path_regex
handlers = Template::Handlers.extensions.map { |x| Regexp.escape(x) }.join("|")
formats = Template::Types.symbols.map { |x| Regexp.escape(x) }.join("|")
locales = "[a-z]{2}(?:-[A-Z]{2})?"
variants = "[^.]*"

%r{
\A
(?:(?<prefix>.*)/)?
(?<partial>_)?
(?<action>.*?)
(?:\.(?<locale>#{locales}))??
(?:\.(?<format>#{formats}))??
(?:\+(?<variant>#{variants}))??
(?:\.(?<handler>#{handlers}))?
\z
}x
end

def parse(path)
match = @regex.match(path)
{
prefix: match[:prefix] || "",
action: match[:action],
partial: !!match[:partial],
locale: match[:locale]&.to_sym,
handler: match[:handler]&.to_sym,
format: match[:format]&.to_sym,
variant: match[:variant]
}
end
end

# Threadsafe template cache
class Cache #:nodoc:
class SmallCache < Concurrent::Map
Expand Down Expand Up @@ -172,11 +210,13 @@ def initialize(pattern = nil)
@pattern = DEFAULT_PATTERN
end
@unbound_templates = Concurrent::Map.new
@path_parser = PathParser.new
super()
end

def clear_cache
@unbound_templates.clear
@path_parser = PathParser.new
super()
end

Expand Down Expand Up @@ -278,18 +318,11 @@ def escape_entry(entry)
# 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)
pieces = File.basename(path).split(".")
pieces.shift
details = @path_parser.parse(path)

extension = pieces.pop

handler = Template.handler_for_extension(extension)
format, variant = pieces.last.split(EXTENSIONS[:variants], 2) if pieces.last
format = if format
Template::Types[format]&.ref
elsif handler.respond_to?(:default_format) # default_format can return nil
handler.default_format
end
handler = Template.handler_for_extension(details[:handler])
format = details[:format] || handler.try(:default_format)
variant = details[:variant]

# Template::Types[format] and handler.default_format can return nil
[handler, format, variant]
Expand Down
4 changes: 4 additions & 0 deletions actionview/test/actionpack/controller/layout_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ def self._implied_layout_name; to_s.underscore.gsub(/_controller$/, "") ; end
module TemplateHandlerHelper
def with_template_handler(*extensions, handler)
ActionView::Template.register_template_handler(*extensions, handler)
ActionController::Base.view_paths.paths.each(&:clear_cache)
ActionView::LookupContext::DetailsKey.clear
yield
ensure
ActionView::Template.unregister_template_handler(*extensions)
ActionController::Base.view_paths.paths.each(&:clear_cache)
ActionView::LookupContext::DetailsKey.clear
end
end

Expand Down
69 changes: 65 additions & 4 deletions actionview/test/template/resolver_shared_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,83 @@ def test_templates_with_optional_locale_shares_common_object

def test_templates_sort_by_formats_json_first
with_file "test/hello_world.html.erb", "Hello HTML!"
with_file "test/hello_world.json.jbuilder", "Hello JSON!"
with_file "test/hello_world.json.builder", "Hello JSON!"

templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:json, :html], variants: :any, handlers: [:erb, :jbuilder])
templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:json, :html], variants: :any, handlers: [:erb, :builder])

assert_equal 2, templates.size
assert_equal "Hello JSON!", templates[0].source
assert_equal :json, templates[0].format
assert_equal "Hello HTML!", templates[1].source
assert_equal :html, templates[1].format
end

def test_templates_sort_by_formats_html_first
with_file "test/hello_world.html.erb", "Hello HTML!"
with_file "test/hello_world.json.jbuilder", "Hello JSON!"
with_file "test/hello_world.json.builder", "Hello JSON!"

templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:html, :json], variants: :any, handlers: [:erb, :jbuilder])
templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:html, :json], variants: :any, handlers: [:erb, :builder])

assert_equal 2, templates.size
assert_equal "Hello HTML!", templates[0].source
assert_equal :html, templates[0].format
assert_equal "Hello JSON!", templates[1].source
assert_equal :json, templates[1].format
end

def test_templates_with_variant
with_file "test/hello_world.html+mobile.erb", "Hello HTML!"

templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:html, :json], variants: :any, handlers: [:erb, :builder])

assert_equal 1, templates.size
assert_equal "Hello HTML!", templates[0].source
assert_kind_of ActionView::Template::Handlers::ERB, templates[0].handler
assert_equal :html, templates[0].format
assert_equal "mobile", templates[0].variant
end

def test_finds_variants_in_order
with_file "test/hello_world.html+tricorder.erb", "Hello Spock!"
with_file "test/hello_world.html+lcars.erb", "Hello Geordi!"

tricorder = context.find("hello_world", "test", false, [], { variants: [:tricorder] })
lcars = context.find("hello_world", "test", false, [], { variants: [:lcars] })

assert_equal "Hello Spock!", tricorder.source
assert_equal "tricorder", tricorder.variant
assert_equal "Hello Geordi!", lcars.source
assert_equal "lcars", lcars.variant

templates = context.find_all("hello_world", "test", false, [], { variants: [:tricorder, :lcars] })
assert_equal [tricorder, lcars], templates

templates = context.find_all("hello_world", "test", false, [], { variants: [:lcars, :tricorder] })
assert_equal [lcars, tricorder], templates
end

def test_templates_no_format_with_variant
with_file "test/hello_world+mobile.erb", "Hello HTML!"

templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:html, :json], variants: :any, handlers: [:erb, :builder])

assert_equal 1, templates.size
assert_equal "Hello HTML!", templates[0].source
assert_kind_of ActionView::Template::Handlers::ERB, templates[0].handler
assert_nil templates[0].format
assert_equal "mobile", templates[0].variant
end

def test_templates_no_format_or_handler_with_variant
with_file "test/hello_world+mobile", "Hello HTML!"

templates = resolver.find_all("hello_world", "test", false, locale: [], formats: [:html, :json], variants: :any, handlers: [:erb, :builder])

assert_equal 1, templates.size
assert_equal "Hello HTML!", templates[0].source
assert_kind_of ActionView::Template::Handlers::Raw, templates[0].handler
assert_nil templates[0].format
assert_equal "mobile", templates[0].variant
end

def test_virtual_path_is_preserved_with_dot
Expand Down
2 changes: 1 addition & 1 deletion railties/test/application/per_request_digest_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def index
end

test "template digests are cleared before a request" do
assert_called(ActionView::LookupContext::DetailsKey, :clear) do
assert_called(ActionView::LookupContext::DetailsKey, :clear, times: 3) do
get "/customers"
assert_equal 200, last_response.status
end
Expand Down
42 changes: 42 additions & 0 deletions railties/test/application/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,47 @@ def show
get "/pages/foo.bar"
assert_equal 200, last_response.status
end

test "New formats and handlers are detected from initializers" do
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
root to: 'pages#show'
end
RUBY

app_file "app/controllers/pages_controller.rb", <<-RUBY
class PagesController < ApplicationController
layout false

def show
render :show, formats: [:awesome], handlers: [:rubby]
end
end
RUBY

app_file "app/views/pages/show.awesome.rubby", <<-RUBY
{
format: @current_template.format,
handler: @current_template.handler
}.inspect
RUBY

app_file "config/initializers/mime_types.rb", <<-RUBY
Mime::Type.register "text/awesome", :awesome
RUBY

app_file "config/initializers/template_handlers.rb", <<-RUBY
module RubbyHandler
def self.call(_, source)
source
end
end
ActionView::Template.register_template_handler(:rubby, RubbyHandler)
RUBY

get "/"
assert_equal 200, last_response.status
assert_equal "{:format=>:awesome, :handler=>RubbyHandler}", last_response.body
end
end
end