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

Improve path parsing in _default_render_options #2115

Merged
merged 5 commits into from May 1, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 29 additions & 8 deletions lib/rspec/rails/example/view_example_group.rb
Expand Up @@ -129,14 +129,35 @@ def rendered.body

def _default_render_options
if ::Rails::VERSION::STRING >= '3.2'
# pluck the handler, format, and locale out of, eg, posts/index.de.html.haml
template, *components = _default_file_to_render.split('.')
handler, format, locale = *components.reverse

render_options = { :template => template }
render_options[:handlers] = [handler] if handler
render_options[:formats] = [format.to_sym] if format
render_options[:locales] = [locale] if locale
formats = if ActionView::Template::Types.respond_to?(:symbols)
ActionView::Template::Types.symbols
else
[:html, :text, :js, :css, :xml, :json].map(&:to_s)
end.map { |x| Regexp.escape(x) }.join("|")

handlers = ActionView::Template::Handlers.extensions.map { |x| Regexp.escape(x) }.join("|")
locales = "[a-z]{2}(?:-[A-Z]{2})?"
variants = "[^.]*"
path_regex = %r{
\A
(?<template>.*?)
(?:\.(?<locale>#{locales}))??
(?:\.(?<format>#{formats}))??
(?:\+(?<variant>#{variants}))??
(?:\.(?<handler>#{handlers}))?
\z
}x

# This regex should always find a match.
# Worst case, everything will be nil, and :template will just be
# the original string.
match = path_regex.match(_default_file_to_render)

render_options = { :template => match[:template] }
render_options[:handlers] = [match[:handler]] if match[:handler]
render_options[:formats] = [match[:format].to_sym] if match[:format]
render_options[:locales] = [match[:locale]] if match[:locale]
render_options[:variants] = [match[:variant]] if match[:variant]

render_options
else
Expand Down
29 changes: 22 additions & 7 deletions spec/rspec/rails/example/view_example_group_spec.rb
Expand Up @@ -137,17 +137,32 @@ def _assigns
expect(view_spec.received.first).to eq([{:template => "widgets/new"},{}, nil])
end

it "converts the filename components into render options" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html.erb" }
view_spec.render

if ::Rails::VERSION::STRING >= '3.2'
if ::Rails::VERSION::STRING >= '3.2'
it "converts the filename components into render options" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html.erb" }
view_spec.render
expect(view_spec.received.first).to eq([{:template => "widgets/new", :locales=>['en'], :formats=>[:html], :handlers=>['erb']}, {}, nil])
else
end

it "converts the filename with variant into render options" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html+fancy.erb" }
view_spec.render
expect(view_spec.received.first).to eq([{:template => "widgets/new", :locales=>['en'], :formats=>[:html], :handlers=>['erb'], variants: ['fancy']}, {}, nil])
end

it "converts the filename without format into render options" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.erb" }
view_spec.render
expect(view_spec.received.first).to eq([{:template => "widgets/new", :locales=>['en'], :handlers=>['erb']}, {}, nil])
end
else
it "uses the filename as a template" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html.erb" }
view_spec.render
expect(view_spec.received.first).to eq([{:template => "widgets/new.en.html.erb"}, {}, nil])
end
end
end
end

context "given a string" do
it "sends string as the first arg to render" do
Expand Down