Class: RailsForge::Refactors::RefactorController

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

Overview

RefactorController class extracts services from fat controller methods

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.



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

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

Instance Method Details

#refactor(controller_name) ⇒ Object

Refactor a specific controller



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/railsforge/refactors/refactor_controller.rb', line 18

def refactor(controller_name)
  controller_file = find_controller_file(controller_name)
  raise RefactorControllerError, "Controller not found" unless controller_file

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

  if long_methods.empty?
    puts "No methods to extract"
    return { extracted: [] }
  end

  puts "Found #{long_methods.count} method(s) to extract"

  extracted = []
  long_methods.each do |method|
    service = extract_to_service(controller_name, method)
    extracted << service if service
  end

  { extracted: extracted }
end