Skip to content

Commit

Permalink
Prevent constants leaking
Browse files Browse the repository at this point in the history
Dependency tracking constants were actually defined in global scope.
  • Loading branch information
rwz committed Jan 2, 2014
1 parent 86c9afd commit 3544b28
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions lib/jbuilder/dependency_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3544b28

Please sign in to comment.