diff options
author | José Valim <jose.valim@gmail.com> | 2009-06-25 11:56:18 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-06-25 11:56:18 +0200 |
commit | ed33c29a4e2a07c2a738ec13775c5cc0d7867b50 (patch) | |
tree | bd3c7fde46d7366adc7d87982c509fa5d9c55933 /railties/lib | |
parent | 4573fd2e06ee9b5a68f84f085f0a10c9ad6f129b (diff) | |
download | rails-ed33c29a4e2a07c2a738ec13775c5cc0d7867b50.tar.gz rails-ed33c29a4e2a07c2a738ec13775c5cc0d7867b50.tar.bz2 rails-ed33c29a4e2a07c2a738ec13775c5cc0d7867b50.zip |
Added class collision checks.
Diffstat (limited to 'railties/lib')
4 files changed, 49 insertions, 8 deletions
diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb index e7d0ac80eb..d4bce83161 100644 --- a/railties/lib/generators/base.rb +++ b/railties/lib/generators/base.rb @@ -29,6 +29,39 @@ module Rails protected + # 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. + # + def class_collisions(*class_names) + return unless behavior == :invoke + + class_names.flatten.each do |class_name| + class_name = class_name.to_s + next if class_name.strip.empty? + + # Split the class from its module nesting + nesting = class_name.split('::') + last_name = nesting.pop + + # Hack to limit const_defined? to non-inherited on 1.9 + extra = [] + extra << false unless Object.method(:const_defined?).arity == 1 + + # Extract the last Module in the nesting + last = nesting.inject(Object) do |last, nest| + break unless last.const_defined?(nest, *extra) + last.const_get(nest) + end + + if last && last.const_defined?(last_name.camelize, *extra) + raise Error, "The name '#{class_name}' is either already used in your application " << + "or reserved by Ruby on Rails. Please choose an alternative and run " << + "this generator again." + end + end + end + # Use Rails default banner. # def self.banner diff --git a/railties/lib/generators/rails/metal/metal_generator.rb b/railties/lib/generators/rails/metal/metal_generator.rb index ba062b30be..f8833ecec3 100644 --- a/railties/lib/generators/rails/metal/metal_generator.rb +++ b/railties/lib/generators/rails/metal/metal_generator.rb @@ -1,7 +1,13 @@ -module Rails::Generators - class MetalGenerator < NamedBase - def create_file - template "metal.rb", "app/metal/#{file_name}.rb" +module Rails + module Generators + class MetalGenerator < NamedBase + def check_class_collision + class_collisions class_name + end + + def create_file + template "metal.rb", "app/metal/#{file_name}.rb" + end end end end diff --git a/railties/lib/generators/rails/observer/observer_generator.rb b/railties/lib/generators/rails/observer/observer_generator.rb index 9a5253a0ad..e5e1be39dc 100644 --- a/railties/lib/generators/rails/observer/observer_generator.rb +++ b/railties/lib/generators/rails/observer/observer_generator.rb @@ -1,8 +1,9 @@ module Rails module Generators class ObserverGenerator < NamedBase - # TODO Check class collisions - # class_collisions "#{class_name}Observer", "#{class_name}ObserverTest" + def check_class_collision + class_collisions "#{class_name}Observer" + end def create_observer_file template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb") diff --git a/railties/lib/generators/rails/plugin/plugin_generator.rb b/railties/lib/generators/rails/plugin/plugin_generator.rb index eb36cea0f1..4dbb3bfc0d 100644 --- a/railties/lib/generators/rails/plugin/plugin_generator.rb +++ b/railties/lib/generators/rails/plugin/plugin_generator.rb @@ -7,8 +7,9 @@ module Rails class_option :with_generator, :type => :boolean, :aliases => "-g", :default => false, :desc => "When supplied creates generator base files." - # TODO Check class collision - # class_collision class_name + def check_class_collision + class_collisions class_name + end def create_root self.root = File.expand_path("vendor/plugins/#{file_name}", root) |