Class: Woods::Extractors::PunditExtractor

Inherits:
Object
  • Object
show all
Includes:
SharedDependencyScanner, SharedUtilityMethods
Defined in:
lib/woods/extractors/pundit_extractor.rb

Overview

PunditExtractor handles Pundit authorization policy extraction.

Specifically targets Pundit convention: classes in ‘app/policies/` that inherit from ApplicationPolicy or follow Pundit patterns (user/record attrs, action? methods). This is distinct from the generic PolicyExtractor which handles domain eligibility policies.

Examples:

extractor = PunditExtractor.new
units = extractor.extract_all
post_policy = units.find { |u| u.identifier == "PostPolicy" }

Constant Summary collapse

PUNDIT_DIRECTORIES =

Directories to scan for Pundit policies

%w[
  app/policies
].freeze
PUNDIT_ACTIONS =

Standard Pundit action methods

%w[index? show? create? new? update? edit? destroy?].freeze

Constants included from SharedDependencyScanner

SharedDependencyScanner::FORM_ACTION_HELPER, SharedDependencyScanner::ROUTE_HELPER_PATTERN

Instance Method Summary collapse

Methods included from SharedDependencyScanner

#extract_constantize_targets, #scan_common_dependencies, #scan_form_dependencies, #scan_job_dependencies, #scan_mailer_dependencies, #scan_model_dependencies, #scan_navigation_dependencies, #scan_service_dependencies

Methods included from SharedUtilityMethods

#app_source?, #condition_label, #count_loc, #detect_entry_points, #extract_action_filter_actions, #extract_callback_conditions, #extract_class_methods, #extract_custom_errors, #extract_initialize_params, #extract_namespace, #extract_parent_class, #extract_public_methods, #resolve_source_location, #skip_file?

Constructor Details

#initializePunditExtractor

Returns a new instance of PunditExtractor.



32
33
34
35
# File 'lib/woods/extractors/pundit_extractor.rb', line 32

def initialize
  @directories = PUNDIT_DIRECTORIES.map { |d| Rails.root.join(d) }
                                   .select(&:directory?)
end

Instance Method Details

#extract_allArray<ExtractedUnit>

Extract all Pundit policy classes

Returns:



40
41
42
43
44
45
46
# File 'lib/woods/extractors/pundit_extractor.rb', line 40

def extract_all
  @directories.flat_map do |dir|
    Dir[dir.join('**/*.rb')].filter_map do |file|
      extract_pundit_file(file)
    end
  end
end

#extract_pundit_file(file_path) ⇒ ExtractedUnit?

Extract a single Pundit policy file

Parameters:

  • file_path (String)

    Path to the policy file

Returns:

  • (ExtractedUnit, nil)

    The extracted unit or nil if not a Pundit policy



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/woods/extractors/pundit_extractor.rb', line 52

def extract_pundit_file(file_path)
  source = File.read(file_path)
  class_name = extract_class_name(file_path, source)

  return nil unless class_name
  return nil unless pundit_policy?(source)

  unit = ExtractedUnit.new(
    type: :pundit_policy,
    identifier: class_name,
    file_path: file_path
  )

  unit.namespace = extract_namespace(class_name)
  unit.source_code = annotate_source(source, class_name)
  unit. = (source, class_name)
  unit.dependencies = extract_dependencies(source, class_name)

  unit
rescue StandardError => e
  Rails.logger.error("Failed to extract Pundit policy #{file_path}: #{e.message}")
  nil
end