Module: RailsForge::RefactorAnalyzer

Defined in:
lib/railsforge/refactor_analyzer.rb

Overview

RefactorAnalyzer module handles refactoring suggestions and code extraction

Defined Under Namespace

Classes: RefactorError

Constant Summary collapse

CONTROLLER_MAX_LINES =

Configuration thresholds

150
CONTROLLER_MAX_METHODS =
10
MODEL_MAX_LINES =
200
MODEL_MAX_METHOD_LINES =
15

Class Method Summary collapse

Class Method Details

.analyze_controller_file(file_path) ⇒ Hash

Analyzes a controller file

Parameters:

  • file_path (String)

    Path to controller file

Returns:

  • (Hash)

    Analysis results



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
84
85
86
87
88
# File 'lib/railsforge/refactor_analyzer.rb', line 53

def self.analyze_controller_file(file_path)
  content = File.read(file_path)
  lines = content.lines.count
  methods = extract_methods(content)

  issues = []
  suggestions = []

  if lines > CONTROLLER_MAX_LINES
    issues << "Controller exceeds #{CONTROLLER_MAX_LINES} lines (currently #{lines})"
    suggestions << "Consider moving business logic into a Service object"
  end

  if methods.count > CONTROLLER_MAX_METHODS
    issues << "Controller has #{methods.count} methods (recommended: #{CONTROLLER_MAX_METHODS} or less)"
    suggestions << "Consider extracting some actions into separate controllers or using a Service"
  end

  # Find long methods that could be extracted
  methods.each do |method|
    if method[:lines] > MODEL_MAX_METHOD_LINES
      suggestions << "Method `#{method[:name]}` has #{method[:lines]} lines - consider extracting to Service"
    end
  end

  {
    type: :controller,
    file: File.basename(file_path),
    path: file_path,
    lines: lines,
    methods: methods,
    issues: issues,
    suggestions: suggestions,
    needs_refactoring: issues.any? || suggestions.any?
  }
end

.analyze_controllers(base_path = nil) ⇒ Array<Hash>

Analyzes controllers for refactoring opportunities

Parameters:

  • base_path (String) (defaults to: nil)

    Rails app root path

Returns:

  • (Array<Hash>)

    Array of refactoring suggestions

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/railsforge/refactor_analyzer.rb', line 16

def self.analyze_controllers(base_path = nil)
  base_path ||= find_rails_app_path
  raise RefactorError, "Not in a Rails application directory" unless base_path

  controllers_dir = File.join(base_path, "app", "controllers")
  return [] unless Dir.exist?(controllers_dir)

  results = []
  Dir.glob(File.join(controllers_dir, "**", "*_controller.rb")).each do |file|
    result = analyze_controller_file(file)
    results << result if result[:needs_refactoring]
  end
  results
end

.analyze_model_file(file_path) ⇒ Hash

Analyzes a model file

Parameters:

  • file_path (String)

    Path to model file

Returns:

  • (Hash)

    Analysis results



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/railsforge/refactor_analyzer.rb', line 93

def self.analyze_model_file(file_path)
  content = File.read(file_path)
  lines = content.lines.count
  methods = extract_methods(content)

  issues = []
  suggestions = []

  if lines > MODEL_MAX_LINES
    issues << "Model exceeds #{MODEL_MAX_LINES} lines (currently #{lines})"
    suggestions << "Consider extracting scopes into Query objects or validations to a Form"
  end

  # Find long methods
  methods.each do |method|
    if method[:lines] > MODEL_MAX_METHOD_LINES
      suggestions << "Method `#{method[:name]}` has #{method[:lines]} lines - consider extracting to a Service"
    end
  end

  {
    type: :model,
    file: File.basename(file_path),
    path: file_path,
    lines: lines,
    methods: methods,
    issues: issues,
    suggestions: suggestions,
    needs_refactoring: issues.any? || suggestions.any?
  }
end

.analyze_models(base_path = nil) ⇒ Array<Hash>

Analyzes models for refactoring opportunities

Parameters:

  • base_path (String) (defaults to: nil)

    Rails app root path

Returns:

  • (Array<Hash>)

    Array of refactoring suggestions

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/railsforge/refactor_analyzer.rb', line 34

def self.analyze_models(base_path = nil)
  base_path ||= find_rails_app_path
  raise RefactorError, "Not in a Rails application directory" unless base_path

  models_dir = File.join(base_path, "app", "models")
  return [] unless Dir.exist?(models_dir)

  results = []
  Dir.glob(File.join(models_dir, "**", "*.rb")).each do |file|
    next if file.end_with?("_application.rb")
    result = analyze_model_file(file)
    results << result if result[:needs_refactoring]
  end
  results
end

.count_method_lines(content, method_name, start_line) ⇒ Integer

Counts lines in a method

Parameters:

  • content (String)

    Ruby code content

  • method_name (String)

    Method name

  • start_line (Integer)

    Starting line number

Returns:

  • (Integer)

    Number of lines



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/railsforge/refactor_analyzer.rb', line 148

def self.count_method_lines(content, method_name, start_line)
  # Find the end of the method
  lines = content.lines
  end_pos = content.length

  # Look for next def or class or end
  rest = content.lines[start_line..-1].join
  if rest =~ /\n\s*def\s+(self\.)?[a-z_]/i
    end_pos = $~.begin(0)
  elsif rest =~ /\n\s*(class|module)\s+/
    end_pos = $~.begin(0)
  elsif rest =~ /\n\s*end\s*$/
    end_pos = $~.begin(0)
  end

  content[start_line..end_pos].lines.count
end

.extract_methods(content) ⇒ Array<Hash>

Extracts method names and line counts from content

Parameters:

  • content (String)

    Ruby code content

Returns:

  • (Array<Hash>)

    Array of method info



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/railsforge/refactor_analyzer.rb', line 128

def self.extract_methods(content)
  methods = []

  # Match def method_name or def self.method_name
  content.scan(/def\s+(self\.)?([a-z_][a-zA-Z_]*)/) do |prefix, name|
    methods << {
      name: name,
      is_class_method: prefix == "self.",
      lines: 1  # Simplified - just mark as present
    }
  end

  methods
end

.extract_to_service(file_path, method_names, service_name) ⇒ Hash

Extracts code from a controller/model and creates a service

Parameters:

  • file_path (String)

    Source file path

  • method_names (Array<String>)

    Methods to extract

  • service_name (String)

    Name for new service

Returns:

  • (Hash)

    Results



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/railsforge/refactor_analyzer.rb', line 336

def self.extract_to_service(file_path, method_names, service_name)
  content = File.read(file_path)

  extracted_logic = []
  method_names.each do |method_name|
    # Find method in content
    if content.include?("def #{method_name}")
      # Extract method and its body
      method_match = content.match(/def #{method_name}.*?(\n\s*end\n)/m)
      extracted_logic << method_match[0] if method_match
    end
  end

  base_path = find_rails_app_path
  generate_service(service_name, extracted_logic.join("\n"), base_path)
  generate_service_spec(service_name, base_path)

  {
    service: "app/services/#{service_name.underscore}_service.rb",
    spec: "spec/services/#{service_name.underscore}_service_spec.rb"
  }
end

.find_rails_app_pathString?

Finds Rails app root path

Returns:

  • (String, nil)

    Rails app path



390
391
392
393
394
395
396
397
398
399
# File 'lib/railsforge/refactor_analyzer.rb', line 390

def self.find_rails_app_path
  path = Dir.pwd
  10.times do
    return path if File.exist?(File.join(path, "config", "application.rb"))
    parent = File.dirname(path)
    break if parent == path
    path = parent
  end
  nil
end

.generate_query(name, scope, base_path = nil) ⇒ String

Generates a query file for extracted logic

Parameters:

  • name (String)

    Query name

  • scope (String)

    Scope logic

  • base_path (String) (defaults to: nil)

    Rails app root

Returns:

  • (String)

    Path to created file

Raises:



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/railsforge/refactor_analyzer.rb', line 214

def self.generate_query(name, scope, base_path = nil)
  base_path ||= find_rails_app_path
  raise RefactorError, "Not in a Rails application directory" unless base_path

  query_dir = File.join(base_path, "app", "queries")
  FileUtils.mkdir_p(query_dir)

  file_name = "find_#{name.underscore}.rb"
  file_path = File.join(query_dir, file_name)

  if File.exist?(file_path)
    puts "  Skipping query (already exists)"
    return file_path
  end

  content = "    # Query class for \#{name}\n    # Extracted scope/query logic\n    #\n    # Usage:\n    #   Find\#{name}.call\n    class Find\#{name}\n      def initialize(scope: nil)\n        @scope = scope || \#{name}.all\n      end\n\n      def call\n        # Extracted scope:\n        # \#{scope.gsub(\"\\n\", \"\\n            # \")}\n        @scope\n      end\n    end\n  RUBY\n\n  File.write(file_path, content)\n  puts \"  Created app/queries/\#{file_name}\"\n  file_path\nend\n"

.generate_query_spec(name, base_path = nil) ⇒ String

Generates an RSpec test for a query

Parameters:

  • name (String)

    Query name

  • base_path (String) (defaults to: nil)

    Rails app root

Returns:

  • (String)

    Path to created file

Raises:



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/railsforge/refactor_analyzer.rb', line 296

def self.generate_query_spec(name, base_path = nil)
  base_path ||= find_rails_app_path
  raise RefactorError, "Not in a Rails application directory" unless base_path

  spec_dir = File.join(base_path, "spec", "queries")
  FileUtils.mkdir_p(spec_dir)

  file_name = "find_#{name.underscore}_spec.rb"
  file_path = File.join(spec_dir, file_name)

  if File.exist?(file_path)
    puts "  Skipping spec (already exists)"
    return file_path
  end

  content = "    require 'rails_helper'\n\n    RSpec.describe Find\#{name} do\n      let(:scope) { \#{name}.all }\n      subject { described_class.new(scope: scope) }\n\n      describe '#call' do\n        it 'returns scope' do\n          expect(subject.call).to eq(scope)\n        end\n      end\n    end\n  RUBY\n\n  File.write(file_path, content)\n  puts \"  Created spec/queries/\#{file_name}\"\n  file_path\nend\n"

.generate_service(name, logic, base_path = nil) ⇒ String

Generates a service file for extracted logic

Parameters:

  • name (String)

    Service name

  • logic (String)

    Logic to extract

  • base_path (String) (defaults to: nil)

    Rails app root

Returns:

  • (String)

    Path to created file

Raises:



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/railsforge/refactor_analyzer.rb', line 171

def self.generate_service(name, logic, base_path = nil)
  base_path ||= find_rails_app_path
  raise RefactorError, "Not in a Rails application directory" unless base_path

  service_dir = File.join(base_path, "app", "services")
  FileUtils.mkdir_p(service_dir)

  file_name = "#{name.underscore}_service.rb"
  file_path = File.join(service_dir, file_name)

  if File.exist?(file_path)
    puts "  Skipping service (already exists)"
    return file_path
  end

  content = "    # Service class for \#{name}\n    # Extracted from controller/model logic\n    #\n    # Usage:\n    #   \#{name}Service.call(params)\n    class \#{name}Service\n      def initialize(**args)\n        @args = args\n      end\n\n      def call\n        # Extracted logic:\n        # \#{logic.gsub(\"\\n\", \"\\n            # \")}\n      end\n    end\n  RUBY\n\n  File.write(file_path, content)\n  puts \"  Created app/services/\#{file_name}\"\n  file_path\nend\n"

.generate_service_spec(name, base_path = nil) ⇒ String

Generates an RSpec test for a service

Parameters:

  • name (String)

    Service name

  • base_path (String) (defaults to: nil)

    Rails app root

Returns:

  • (String)

    Path to created file

Raises:



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/railsforge/refactor_analyzer.rb', line 257

def self.generate_service_spec(name, base_path = nil)
  base_path ||= find_rails_app_path
  raise RefactorError, "Not in a Rails application directory" unless base_path

  spec_dir = File.join(base_path, "spec", "services")
  FileUtils.mkdir_p(spec_dir)

  file_name = "#{name.underscore}_service_spec.rb"
  file_path = File.join(spec_dir, file_name)

  if File.exist?(file_path)
    puts "  Skipping spec (already exists)"
    return file_path
  end

  content = "    require 'rails_helper'\n\n    RSpec.describe \#{name}Service do\n      let(:params) { {} }\n      subject { described_class.new(params) }\n\n      describe '#call' do\n        it 'returns successful result' do\n          expect(subject.call).to be_truthy\n        end\n      end\n    end\n  RUBY\n\n  File.write(file_path, content)\n  puts \"  Created spec/services/\#{file_name}\"\n  file_path\nend\n"

Prints refactoring report

Parameters:

  • results (Array<Hash>)

    Analysis results



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/railsforge/refactor_analyzer.rb', line 361

def self.print_report(results)
  return puts "\n✓ No refactoring needed!" if results.empty?

  puts "\n" + "=" * 60
  puts "REFACTORING REPORT"
  puts "=" * 60

  results.each do |result|
    puts "\n📁 #{result[:file]} (#{result[:type]})"
    puts "   Lines: #{result[:lines]}"

    if result[:issues].any?
      puts "\n   ⚠️  Issues:"
      result[:issues].each { |issue| puts "      • #{issue}" }
    end

    if result[:suggestions].any?
      puts "\n   💡 Suggestions:"
      result[:suggestions].each { |sug| puts "      • #{sug}" }
    end
  end

  puts "\n" + "=" * 60
  puts "Total files needing refactoring: #{results.count}"
  puts "=" * 60
end