From c665c74f776ae71d65062f2ea6f744690e5bc331 Mon Sep 17 00:00:00 2001 From: Yaw Boakye Date: Thu, 5 Sep 2024 16:31:07 +0100 Subject: [PATCH] use source location to determine collision cause --- railties/lib/rails/generators/base.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb index 2478aaae8cb88..cf3feb7892223 100644 --- a/railties/lib/rails/generators/base.rb +++ b/railties/lib/rails/generators/base.rb @@ -276,13 +276,26 @@ def class_collisions(*class_names) last = extract_last_module(nesting) if last && last.const_defined?(last_name.camelize, false) - raise Error, "The name '#{class_name}' is either already used in your application " \ - "or reserved by Ruby on Rails. Please choose an alternative or use --skip-collision-check " \ - "or --force to skip this check and run this generator again." + source_location, = Object.const_source_location(class_name) + collision_cause = + if defined_in_app?(source_location) + "already used in your application in the file at " \ + "#{source_location.delete_prefix(Rails.root.to_s)[1..]}" + else + "reserved by Ruby on Rails" + end + + raise Error, "The name '#{class_name}' is #{collision_cause}. " \ + "Please choose an alternative or use --skip-collision-check or " \ + "--force to skip this check and run this generator again." end end end + def defined_in_app?(source_location) + source_location.start_with?(Rails.root.to_s) + end + # Takes in an array of nested modules and extracts the last module def extract_last_module(nesting) # :doc: nesting.inject(Object) do |last_module, nest|