Class: RailsForge::RefactorController

Inherits:
Object
  • Object
show all
Defined in:
lib/railsforge/refactor_controller.rb

Defined Under Namespace

Classes: RefactorControllerError

Constant Summary collapse

MIN_METHOD_LINES =
15

Instance Method Summary collapse

Constructor Details

#initialize(base_path = nil) ⇒ RefactorController

Returns a new instance of RefactorController.



13
14
15
16
# File 'lib/railsforge/refactor_controller.rb', line 13

def initialize(base_path = nil)
  @base_path = base_path || find_rails_app_path
  raise RefactorControllerError, "Not in a Rails application directory" unless @base_path
end

Instance Method Details

#preview(controller_name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/railsforge/refactor_controller.rb', line 68

def preview(controller_name)
  controller_file = find_controller_file(controller_name)

  unless controller_file
    raise RefactorControllerError, "Controller '#{controller_name}' not found"
  end

  content = File.read(controller_file)
  find_long_methods(content)
end

#refactor(controller_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/railsforge/refactor_controller.rb', line 21

def refactor(controller_name)
  controller_file = find_controller_file(controller_name)

  unless controller_file
    raise RefactorControllerError, "Controller '#{controller_name}' not found"
  end

  puts "Analyzing controller: #{controller_name}"
  puts ""

  # Read controller content
  content = File.read(controller_file)

  # Find methods that can be extracted
  long_methods = find_long_methods(content)

  if long_methods.empty?
    puts "No methods found that need extraction (min #{MIN_METHOD_LINES} lines)"
    return { extracted: [], controller: controller_name }
  end

  puts "Found #{long_methods.count} method(s) to extract:"
  long_methods.each do |method|
    puts "  - #{method[:name]} (#{method[:lines]} lines)"
  end
  puts ""

  # Extract each method to a service
  extracted = []
  long_methods.each do |method|
    service = extract_method_to_service(controller_name, method)
    if service
      update_controller_method(controller_file, method, service)
      extracted << service
    end
  end

  puts "Extraction complete!"
  puts "  Created #{extracted.count} service(s)"
  puts ""

  { extracted: extracted, controller: controller_name, methods: long_methods }
end