From 3544b288b63f504f46fa8aafd1d17ee198d77536 Mon Sep 17 00:00:00 2001 From: Pavel Pravosud Date: Thu, 2 Jan 2014 14:12:02 -0500 Subject: [PATCH] Prevent constants leaking Dependency tracking constants were actually defined in global scope. --- lib/jbuilder/dependency_tracker.rb | 74 +++++++++++++++--------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/lib/jbuilder/dependency_tracker.rb b/lib/jbuilder/dependency_tracker.rb index 7c6260e..c937dff 100644 --- a/lib/jbuilder/dependency_tracker.rb +++ b/lib/jbuilder/dependency_tracker.rb @@ -15,45 +15,47 @@ end if dependency_tracker - dependency_tracking_module = Module.new do - # Matches: - # json.partial! "messages/message" - # json.partial!('messages/message') - # - DIRECT_RENDERS = / - \w+\.partial! # json.partial! - \(?\s* # optional parenthesis - (['"])([^'"]+)\1 # quoted value - /x - - # Matches: - # json.partial! partial: "comments/comment" - # json.comments @post.comments, partial: "comments/comment", as: :comment - # json.array! @posts, partial: "posts/post", as: :post - # = render partial: "account" - # - INDIRECT_RENDERS = / - (?::partial\s*=>|partial:) # partial: or :partial => - \s* # optional whitespace - (['"])([^'"]+)\1 # quoted value - /x - - def dependencies - direct_dependencies + indirect_dependencies + explicit_dependencies - end - - private - - def direct_dependencies - source.scan(DIRECT_RENDERS).map(&:second) - end - - def indirect_dependencies - source.scan(INDIRECT_RENDERS).map(&:second) + class Jbuilder + module DependencyTrackerMethods + # Matches: + # json.partial! "messages/message" + # json.partial!('messages/message') + # + DIRECT_RENDERS = / + \w+\.partial! # json.partial! + \(?\s* # optional parenthesis + (['"])([^'"]+)\1 # quoted value + /x + + # Matches: + # json.partial! partial: "comments/comment" + # json.comments @post.comments, partial: "comments/comment", as: :comment + # json.array! @posts, partial: "posts/post", as: :post + # = render partial: "account" + # + INDIRECT_RENDERS = / + (?::partial\s*=>|partial:) # partial: or :partial => + \s* # optional whitespace + (['"])([^'"]+)\1 # quoted value + /x + + def dependencies + direct_dependencies + indirect_dependencies + explicit_dependencies + end + + private + + def direct_dependencies + source.scan(DIRECT_RENDERS).map(&:second) + end + + def indirect_dependencies + source.scan(INDIRECT_RENDERS).map(&:second) + end end end ::Jbuilder::DependencyTracker = Class.new(dependency_tracker::ERBTracker) - ::Jbuilder::DependencyTracker.send :include, dependency_tracking_module + ::Jbuilder::DependencyTracker.send :include, ::Jbuilder::DependencyTrackerMethods dependency_tracker.register_tracker :jbuilder, ::Jbuilder::DependencyTracker end