diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-23 00:52:34 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-23 00:52:34 +0000 |
commit | 4ff4afa57949e6a7a4d3d200531932207f97da8e (patch) | |
tree | 3546444b022426e7324b667fe8662c88ed6b0775 /railties/lib | |
parent | 4ce65f434a29460aebcd2eaa511d85f0ff6bb18a (diff) | |
download | rails-4ff4afa57949e6a7a4d3d200531932207f97da8e.tar.gz rails-4ff4afa57949e6a7a4d3d200531932207f97da8e.tar.bz2 rails-4ff4afa57949e6a7a4d3d200531932207f97da8e.zip |
Added protection for creating a model through the generators with a name of an existing class, like Thread or Date. It'll even offer you a synonym using wordnet.princeton.edu as a look-up. No, I'm not kidding :) [Florian Gross]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@263 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/lib')
-rw-r--r-- | railties/lib/rails_generator.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/railties/lib/rails_generator.rb b/railties/lib/rails_generator.rb index 9e64f5f4c5..38b8daee49 100644 --- a/railties/lib/rails_generator.rb +++ b/railties/lib/rails_generator.rb @@ -114,6 +114,33 @@ module Rails @args = args end + # Checks whether the class name that was assigned to this generator + # would cause a collision with a Class, Module or other constant + # that is already used up by Ruby or RubyOnRails. + def collision_with_builtin? + builtin = Object.const_get(full_class_name) rescue nil + type = case builtin + when Class: "Class" + when Module: "Module" + else "Constant" + end + + if builtin then + "Sorry, you can't have a #{self.class.generator_name} named\n" + + "'#{full_class_name}' because Ruby or RubyOnRails already has\n" + + "a #{type} with that name. Please rerun the generator with a\n" + + "different name." + end + end + + # Returns the complete name that the resulting Class would have. + # Used in collision_with_builtin(). The default guess is that it is + # the same as class_name. Override this in your generator in case + # it is wrong. + def full_class_name + class_name + end + protected # Look up another generator with the same arguments. def generator(name) |