Check whether the given class names are already taken by Ruby or Rails. In the future, expand to check other
namespaces such as the rest of the user’s app.
# File railties/lib/rails_generator/commands.rb, line 168
def class_collisions(*class_names)
class_names.flatten.each do |class_name|
# Convert to string to allow symbol arguments.
class_name = class_name.to_s
# Skip empty strings.
next if class_name.strip.empty?
# Split the class from its module nesting.
nesting = class_name.split('::')
name = nesting.pop
# Extract the last Module in the nesting.
last = nesting.inject(Object) { |last, nest|
break unless last.const_defined?(nest)
last.const_get(nest)
}
# If the last Module exists, check whether the given
# class exists and raise a collision if so.
if last and last.const_defined?(name.camelize)
raise_class_collision(class_name)
end
end
end