Sha256: 9aedc7b7a9713dfb543f77468a59bb15f27dee372bb29603dfc82dbf177542f9

Contents?: true

Size: 1.78 KB

Versions: 9

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module ActionView
  class DependencyTracker # :nodoc:
    class RubyTracker # :nodoc:
      EXPLICIT_DEPENDENCY = /# Template Dependency: (\S+)/

      def self.call(name, template, view_paths = nil)
        new(name, template, view_paths).dependencies
      end

      def dependencies
        render_dependencies + explicit_dependencies
      end

      def self.supports_view_paths? # :nodoc:
        true
      end

      def initialize(name, template, view_paths = nil, parser_class: RenderParser::Default)
        @name, @template, @view_paths = name, template, view_paths
        @parser_class = parser_class
      end

      private
        attr_reader :template, :name, :view_paths

        def render_dependencies
          return [] unless template.source.include?("render")

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

          @parser_class.new(@name, compiled_source).render_calls.filter_map do |render_call|
            next if render_call.end_with?("/_")
            render_call.gsub(%r|/_|, "/")
          end
        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
        end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
actionview-7.2.2 lib/action_view/dependency_tracker/ruby_tracker.rb
actionview-7.2.1.2 lib/action_view/dependency_tracker/ruby_tracker.rb
actionview-7.2.1.1 lib/action_view/dependency_tracker/ruby_tracker.rb
actionview-7.2.1 lib/action_view/dependency_tracker/ruby_tracker.rb
actionview-7.2.0 lib/action_view/dependency_tracker/ruby_tracker.rb
actionview-7.2.0.rc1 lib/action_view/dependency_tracker/ruby_tracker.rb
actionview-7.2.0.beta3 lib/action_view/dependency_tracker/ruby_tracker.rb
actionview-7.2.0.beta2 lib/action_view/dependency_tracker/ruby_tracker.rb
actionview-7.2.0.beta1 lib/action_view/dependency_tracker/ruby_tracker.rb