Permalink
Browse files

Avoid scanning multiple render calls as a single match.

Each chunk of text coming after `render` is now handled individually as a possible list of arguments.
  • Loading branch information...
1 parent c2afa05 commit ccbba3ff50a7c2d6523f55f7821aabdb89fc5d45 @britto britto committed Dec 22, 2013
Showing with 45 additions and 21 deletions.
  1. +19 −17 actionview/lib/action_view/dependency_tracker.rb
  2. +26 −4 actionview/test/template/dependency_tracker_test.rb
@@ -54,21 +54,20 @@ class ERBTracker # :nodoc:
/x
# Matches:
- # render partial: "comments/comment", collection: commentable.comments
- # render "comments/comments"
- # render 'comments/comments'
- # render('comments/comments')
+ # partial: "comments/comment", collection: @all_comments => "comments/comment"
+ # (object: @single_comment, partial: "comments/comment") => "comments/comment"
#
- # render(@topic) => render("topics/topic")
- # render(topics) => render("topics/topic")
- # render(message.topics) => render("topics/topic")
- RENDER_DEPENDENCY = /
- \brender\b # render, the whole word
- \s*\(?\s* # optional opening paren surrounded by spaces
- (?:
- (?:.*?#{PARTIAL_HASH_KEY})? # optional hash, up to the partial key declaration
- (?:#{STRING}|#{VARIABLE_OR_METHOD_CHAIN}) # finally, the dependency name of interest
- )
+ # "comments/comments"
+ # 'comments/comments'
+ # ('comments/comments')
+ #
+ # (@topic) => "topics/topic"
+ # topics => "topics/topic"
+ # (message.topics) => "topics/topic"
+ RENDER_ARGUMENTS = /\A
+ (?:\s*\(?\s*) # optional opening paren surrounded by spaces
+ (?:.*?#{PARTIAL_HASH_KEY})? # optional hash, up to the partial key declaration
+ (?:#{STRING}|#{VARIABLE_OR_METHOD_CHAIN}) # finally, the dependency name of interest
/xm
def self.call(name, template)
@@ -98,10 +97,13 @@ def directory
def render_dependencies
render_dependencies = []
+ render_calls = source.split(/\brender\b/).drop(1)
- source.scan(RENDER_DEPENDENCY) do
- add_dynamic_dependency(render_dependencies, Regexp.last_match[:dynamic])
- add_static_dependency(render_dependencies, Regexp.last_match[:static])
+ render_calls.each do |arguments|
+ arguments.scan(RENDER_ARGUMENTS) do
+ add_dynamic_dependency(render_dependencies, Regexp.last_match[:dynamic])
+ add_static_dependency(render_dependencies, Regexp.last_match[:static])
+ end
end
render_dependencies.uniq
@@ -147,12 +147,34 @@ def test_finds_dependencies_with_special_characters
end
def test_finds_dependencies_with_quotes_within
- template = FakeTemplate.new("
- <%# render \"single/quote's\" %>
- <%# render 'double/quote\"s' %>
- ", :erb)
+ template = FakeTemplate.new(%{
+ <%# render "single/quote's" %>
+ <%# render 'double/quote"s' %>
+ }, :erb)
+
tracker = make_tracker("quotes/_single_and_double", template)
assert_equal ["single/quote's", 'double/quote"s'], tracker.dependencies
end
+
+ def test_finds_dependencies_with_extra_spaces
+ template = FakeTemplate.new(%{
+ <%= render "header" %>
+ <%= render partial: "form" %>
+ <%= render @message %>
+ <%= render ( @message.events ) %>
+ <%= render :collection => @message.comments,
+ :partial => "comments/comment" %>
+ }, :erb)
+
+ tracker = make_tracker("spaces/_extra", template)
+
+ assert_equal [
+ "spaces/header",
+ "spaces/form",
+ "messages/message",
+ "events/event",
+ "comments/comment"
+ ], tracker.dependencies
+ end
end

0 comments on commit ccbba3f

Please sign in to comment.