Class: Zed::Rails::Jumper::CLI::ControllerFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/zed/rails/jumper/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(view_file, rails_root) ⇒ ControllerFinder

Returns a new instance of ControllerFinder.



216
217
218
219
# File 'lib/zed/rails/jumper/cli.rb', line 216

def initialize(view_file, rails_root)
  @view_file = Pathname.new(view_file)
  @rails_root = Pathname.new(rails_root)
end

Instance Method Details

#find_controller_and_actionObject



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
252
253
# File 'lib/zed/rails/jumper/cli.rb', line 221

def find_controller_and_action
  # Extract the path relative to app/views
  # e.g., app/views/users/archives/show.html.erb -> users/archives/show
  relative_path = @view_file.relative_path_from(@rails_root.join("app", "views"))
  return nil unless relative_path.to_s.match?(/\.(erb|jbuilder|builder)$/)
  
  # Remove all possible Rails view extensions
  path_without_ext = relative_path.to_s.sub(/\.(html|json|js|xml)?\.(erb|jbuilder|builder)$/, '').sub(/\.(erb|jbuilder|builder)$/, '')
  path_parts = path_without_ext.split('/')
  
  return nil if path_parts.empty?
  
  # The last part is the action name
  action_name = path_parts.pop
  # The remaining parts form the controller path
  controller_parts = path_parts
  
  return nil if controller_parts.empty?
  
  # Build the controller file path
  # e.g., users/archives -> app/controllers/users/archives_controller.rb
  controller_path = controller_parts.join('/')
  controller_file = @rails_root.join("app", "controllers", "#{controller_path}_controller.rb")
  
  return nil unless controller_file.exist?

  # Optionally, check if the method exists in the controller
  if method_defined_in_controller?(controller_file, action_name)
    { controller_file: controller_file.to_s, action_name: action_name }
  else
    { controller_file: controller_file.to_s, action_name: action_name, warning: "Method not found in controller" }
  end
end