Permalink
Browse files

Added protection for creating a model through the generators with a n…

…ame 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
  • Loading branch information...
1 parent 4ce65f4 commit 4ff4afa57949e6a7a4d3d200531932207f97da8e @dhh dhh committed Dec 23, 2004
View
@@ -1,3 +1,9 @@
+*SVN*
+
+* 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]
+
+
*0.9.2*
* Fixed CTRL-C exists from the Breakpointer to be a clean affair without error dumping [Kent Sibilev]
View
@@ -4,10 +4,35 @@ require 'rails_generator'
ARGV.shift unless ARGV.empty? or not ['--help', '-h'].include?(ARGV[0])
+def find_synonyms(word)
+ require 'open-uri'
+ uri = "http://wordnet.princeton.edu/cgi-bin/webwn2.0?stage=2" +
+ "&word=%s&posnumber=1&searchtypenumber=2&senses=&showglosses=1"
+
+ open(uri % word) do |stream|
+ data = stream.read.gsub("&nbsp;", " ").gsub("<BR>", "")
+ data.scan(/^Sense \d+\n.+?\n\n/m)
+ end
+rescue Exception
+ return nil
+end
+
unless ARGV.empty?
begin
name = ARGV.shift
- Rails::Generator.instance(name, ARGV).generate
+ generator = Rails::Generator.instance(name, ARGV)
+
+ if msg = generator.collision_with_builtin? then
+ $stderr.puts msg
+
+ if synonyms = find_synonyms(generator.class_name) then
+ $stderr.puts "", "Here's a few synonyms from WordNets.",
+ "Maybe they will help you find an alternative name."
+ "", synonyms
+ end
+ else
+ generator.generate
+ end
rescue Rails::Generator::UsageError => e
puts e.message
end
@@ -19,4 +19,8 @@ def generate
template "view.rhtml", "app/views/#{file_name}/#{action}.rhtml", binding
end
end
+
+ def full_class_name
+ class_name + "Controller"
+ end
end
@@ -1,4 +1,4 @@
-class <%= class_name %>Controller < ApplicationController
+class <%= full_class_name %> < ApplicationController
<% if options[:scaffold] -%>
scaffold :<%= singular_name %>
<% end -%>
@@ -2,11 +2,11 @@
require '<%= file_name %>_controller'
# Re-raise errors caught by the controller.
-class <%= class_name %>Controller; def rescue_action(e) raise e end; end
+class <%= full_class_name %>; def rescue_action(e) raise e end; end
-class <%= class_name %>ControllerTest < Test::Unit::TestCase
+class <%= full_class_name %>Test < Test::Unit::TestCase
def setup
- @controller = <%= class_name %>Controller.new
+ @controller = <%= full_class_name %>.new
@request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
end
@@ -34,6 +34,10 @@ def generate
end
end
+ def full_class_name
+ class_name + "Controller"
+ end
+
protected
def scaffold_views
%w(list show new edit)
@@ -114,6 +114,33 @@ def initialize(template_root, destination_root, search_paths, args)
@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)

0 comments on commit 4ff4afa

Please sign in to comment.