Skip to content

Commit

Permalink
use source location to determine collision cause
Browse files Browse the repository at this point in the history
  • Loading branch information
yawboakye committed Sep 5, 2024
1 parent d437ae3 commit c665c74
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions railties/lib/rails/generators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit c665c74

Please sign in to comment.