mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Improve did_you_mean partial name correction
Before this change did_you_mean shows partial paths like `animals/_partial`, but adding that to your render call in a view like `<%= render 'animals/_partial' %>` will still be missing as rails will search for the template `animals/__partial`. We can provide the user a easier copy/paste correction if we don't tell them about the underscore. This also reduces the candidates to only return partials if you are looking for a partial and only look for non-templates when you are not looking for a template.
This commit is contained in:
parent
6ebd134a9a
commit
f80038ae7d
2 changed files with 29 additions and 4 deletions
|
@ -27,7 +27,7 @@ module ActionView
|
||||||
end
|
end
|
||||||
|
|
||||||
class MissingTemplate < ActionViewError #:nodoc:
|
class MissingTemplate < ActionViewError #:nodoc:
|
||||||
attr_reader :path, :paths, :prefixes
|
attr_reader :path, :paths, :prefixes, :partial
|
||||||
|
|
||||||
def initialize(paths, path, prefixes, partial, details, *)
|
def initialize(paths, path, prefixes, partial, details, *)
|
||||||
if partial && path.present?
|
if partial && path.present?
|
||||||
|
@ -37,6 +37,7 @@ module ActionView
|
||||||
@path = path
|
@path = path
|
||||||
@paths = paths
|
@paths = paths
|
||||||
@prefixes = Array(prefixes)
|
@prefixes = Array(prefixes)
|
||||||
|
@partial = partial
|
||||||
template_type = if partial
|
template_type = if partial
|
||||||
"partial"
|
"partial"
|
||||||
elsif /layouts/i.match?(path)
|
elsif /layouts/i.match?(path)
|
||||||
|
@ -93,7 +94,13 @@ module ActionView
|
||||||
def corrections
|
def corrections
|
||||||
path = @error.path
|
path = @error.path
|
||||||
prefixes = @error.prefixes
|
prefixes = @error.prefixes
|
||||||
|
|
||||||
candidates = @error.paths.flat_map(&:all_template_paths).uniq
|
candidates = @error.paths.flat_map(&:all_template_paths).uniq
|
||||||
|
if @error.partial
|
||||||
|
candidates = candidates.grep(%r{_[^/]+\z})
|
||||||
|
else
|
||||||
|
candidates = candidates.grep_v(%r{_[^/]+\z})
|
||||||
|
end
|
||||||
|
|
||||||
# Group by possible prefixes
|
# Group by possible prefixes
|
||||||
files_by_dir = candidates.group_by do |x|
|
files_by_dir = candidates.group_by do |x|
|
||||||
|
@ -135,7 +142,11 @@ module ActionView
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
results.to_a
|
if @error.partial
|
||||||
|
results.to_a.map { |res| res.sub(%r{_([^/]+)\z}, "\\1") }
|
||||||
|
else
|
||||||
|
results.to_a
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -626,13 +626,27 @@ module RenderTestCases
|
||||||
if defined?(DidYouMean) && DidYouMean.respond_to?(:correct_error)
|
if defined?(DidYouMean) && DidYouMean.respond_to?(:correct_error)
|
||||||
def test_render_partial_provides_spellcheck
|
def test_render_partial_provides_spellcheck
|
||||||
e = assert_raises(ActionView::MissingTemplate) { @view.render(partial: "test/partail") }
|
e = assert_raises(ActionView::MissingTemplate) { @view.render(partial: "test/partail") }
|
||||||
assert_match %r{Did you mean\? test/_partial\n *test/_partialhtml}, e.message
|
assert_match %r{Did you mean\? test/partial\n *test/partialhtml}, e.message
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_spellcheck_doesnt_list_directories
|
def test_spellcheck_doesnt_list_directories
|
||||||
e = assert_raises(ActionView::MissingTemplate) { @view.render(partial: "test/directory") }
|
e = assert_raises(ActionView::MissingTemplate) { @view.render(partial: "test/directory") }
|
||||||
assert_match %r{Did you mean\?}, e.message
|
assert_match %r{Did you mean\?}, e.message
|
||||||
assert_no_match %r{Did you mean\? test/_directory\n}, e.message # test/hello is a directory
|
assert_no_match %r{Did you mean\? test/directory\n}, e.message # test/hello is a directory
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_spellcheck_only_lists_templates
|
||||||
|
e = assert_raises(ActionView::MissingTemplate) { @view.render(template: "test/partial") }
|
||||||
|
|
||||||
|
assert_match %r{Did you mean\?}, e.message
|
||||||
|
assert_no_match %r{Did you mean\? test/partial\n}, e.message
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_spellcheck_only_lists_partials
|
||||||
|
e = assert_raises(ActionView::MissingTemplate) { @view.render(partial: "test/template") }
|
||||||
|
|
||||||
|
assert_match %r{Did you mean\?}, e.message
|
||||||
|
assert_no_match %r{Did you mean\? test/template\n}, e.message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue