Skip to content

Commit

Permalink
Improve path parsing in _default_render_options (#2115)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn authored and Sam Phippen committed May 1, 2019
1 parent cbe8e9d commit b4a2682
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
37 changes: 29 additions & 8 deletions lib/rspec/rails/example/view_example_group.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit b4a2682

Please sign in to comment.