Skip to content

Commit

Permalink
Extract WildcardResolver to remove duplication
Browse files Browse the repository at this point in the history
Since both the ERBTracker and RubyTracker now support resolving
interpolated template paths against the view_paths, the logic for this
resolution can be extracted to its own class.
  • Loading branch information
skipkayhil committed Jul 23, 2024
1 parent d5701e5 commit 1a0e710
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 44 deletions.
1 change: 1 addition & 0 deletions actionview/lib/action_view/dependency_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DependencyTracker # :nodoc:

autoload :ERBTracker
autoload :RubyTracker
autoload :WildcardResolver

@trackers = Concurrent::Map.new

Expand Down
24 changes: 3 additions & 21 deletions actionview/lib/action_view/dependency_tracker/erb_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def initialize(name, template, view_paths = nil)
end

def dependencies
render_dependencies + explicit_dependencies
WildcardResolver.new(@view_paths, render_dependencies + explicit_dependencies).resolve
end

attr_reader :name, :template
Expand All @@ -98,9 +98,7 @@ def render_dependencies
add_dependencies(dependencies, arguments, RENDER_ARGUMENTS)
end

wildcards, explicits = dependencies.partition { |dependency| dependency.end_with?("/*") }

(explicits + resolve_directories(wildcards)).uniq
dependencies
end

def add_dependencies(render_dependencies, arguments, pattern)
Expand Down Expand Up @@ -153,24 +151,8 @@ def add_static_dependency(dependencies, dependency, quote_type)
end
end

def resolve_directories(wildcard_dependencies)
return [] unless @view_paths
return [] if wildcard_dependencies.empty?

# Remove trailing "/*"
prefixes = wildcard_dependencies.map { |query| query[0..-3] }

@view_paths.flat_map(&:all_template_paths).uniq.filter_map { |path|
path.to_s if prefixes.include?(path.prefix)
}.sort
end

def explicit_dependencies
dependencies = source.scan(EXPLICIT_DEPENDENCY).flatten.uniq

wildcards, explicits = dependencies.partition { |dependency| dependency.end_with?("/*") }

(explicits + resolve_directories(wildcards)).uniq
source.scan(EXPLICIT_DEPENDENCY).flatten.uniq
end
end
end
Expand Down
26 changes: 3 additions & 23 deletions actionview/lib/action_view/dependency_tracker/ruby_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.call(name, template, view_paths = nil)
end

def dependencies
render_dependencies + explicit_dependencies
WildcardResolver.new(view_paths, render_dependencies + explicit_dependencies).resolve
end

def self.supports_view_paths? # :nodoc:
Expand All @@ -30,33 +30,13 @@ def render_dependencies

compiled_source = template.handler.call(template, template.source)

dependencies = @parser_class.new(@name, compiled_source).render_calls.filter_map do |render_call|
@parser_class.new(@name, compiled_source).render_calls.filter_map do |render_call|
render_call.gsub(%r|/_|, "/")
end

wildcards, explicits = dependencies.partition { |dependency| dependency.end_with?("/*") }

(explicits + resolve_directories(wildcards)).uniq
end

def explicit_dependencies
dependencies = template.source.scan(EXPLICIT_DEPENDENCY).flatten.uniq

wildcards, explicits = dependencies.partition { |dependency| dependency.end_with?("/*") }

(explicits + resolve_directories(wildcards)).uniq
end

def resolve_directories(wildcard_dependencies)
return [] unless view_paths
return [] if wildcard_dependencies.empty?

# Remove trailing "/*"
prefixes = wildcard_dependencies.map { |query| query[0..-3] }

view_paths.flat_map(&:all_template_paths).uniq.filter_map { |path|
path.to_s if prefixes.include?(path.prefix)
}.sort
template.source.scan(EXPLICIT_DEPENDENCY).flatten.uniq
end
end
end
Expand Down
32 changes: 32 additions & 0 deletions actionview/lib/action_view/dependency_tracker/wildcard_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module ActionView
class DependencyTracker # :nodoc:
class WildcardResolver # :nodoc:
def initialize(view_paths, dependencies)
@view_paths = view_paths

@wildcard_dependencies, @explicit_dependencies =
dependencies.partition { |dependency| dependency.end_with?("/*") }
end

def resolve
return explicit_dependencies.uniq if !view_paths || wildcard_dependencies.empty?

(explicit_dependencies + resolved_wildcard_dependencies).uniq
end

private
attr_reader :explicit_dependencies, :wildcard_dependencies, :view_paths

def resolved_wildcard_dependencies
# Remove trailing "/*"
prefixes = wildcard_dependencies.map { |query| query[0..-3] }

view_paths.flat_map(&:all_template_paths).uniq.filter_map { |path|
path.to_s if prefixes.include?(path.prefix)
}.sort
end
end
end
end

0 comments on commit 1a0e710

Please sign in to comment.