Class: Tng::Analyzers::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/tng/analyzers/controller.rb

Class Method Summary collapse

Class Method Details

.files_in_dir(dir) ⇒ Object



6
7
8
9
10
# File 'lib/tng/analyzers/controller.rb', line 6

def self.files_in_dir(dir)
  dir = File.join(Dir.pwd, "app/controllers") if dir.nil?
  files = Tng::Analyzer::Controller.files_in_dir(dir.to_s)
  Tng::Utils.filter_ignored_files(files)
end

.methods_for_controller(controller_name, file_path = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tng/analyzers/controller.rb', line 46

def self.methods_for_controller(controller_name, file_path = nil)
  raise "controller_name is required" if controller_name.nil?

  begin
    # Load the controller class
    controller_class = controller_name.constantize

    # Get public instance methods (actions) excluding inherited ones
    public_methods = controller_class.public_instance_methods(false)

    # Filter to methods defined in this controller only
    action_methods = public_methods.select do |method_name|
      method = controller_class.instance_method(method_name)
      method.owner == controller_class
    end

    # Fallback to file parsing if reflection fails
    if action_methods.empty? && file_path && File.exist?(file_path)
      source_code = File.read(file_path)
      result = Prism.parse(source_code)
      defined_methods = []

      result.value&.child_nodes&.each do |node|
        next unless node.is_a?(Prism::DefNode)
        defined_methods << node.name.to_s
      end

      action_methods = defined_methods
    end

    # Return method info
    action_methods.map do |method_name|
      {
        name: method_name.to_s
      }
    end
  end
end

.parents_for_controller(controller) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tng/analyzers/controller.rb', line 24

def self.parents_for_controller(controller)
  raise "controller is required" if controller.nil?

  path = controller[:path]
  relative_path = path.gsub(%r{^.*app/controllers/}, "").gsub(".rb", "")
  clean_controller_name = relative_path.split("/").map(&:camelize).join("::")

  begin
    Tng::Analyzer::Controller.parents_for_controller(clean_controller_name)
  rescue NameError => e
    puts "❌ Error analyzing controller #{clean_controller_name}: #{e.message}"
    puts "   Path: #{path}"
    puts "   This might be due to a syntax error in the controller file."
    []
  rescue StandardError => e
    puts "❌ Error analyzing controller #{clean_controller_name}: #{e.message}"
    puts "   Path: #{path}"
    puts "   This might be due to a syntax error in the controller file."
    []
  end
end

.routes_for_controller(file_path) ⇒ Object



12
13
14
15
16
# File 'lib/tng/analyzers/controller.rb', line 12

def self.routes_for_controller(file_path)
  raise "file_path is required" if file_path.nil?

  Tng::Analyzer::Controller.routes_for_controller(file_path)
end

.value_for_controller(file_path) ⇒ Object



18
19
20
21
22
# File 'lib/tng/analyzers/controller.rb', line 18

def self.value_for_controller(file_path)
  raise "file_path is required" if file_path.nil?

  Tng::Analyzer::Controller.parse_controller_file(file_path)
end