aboutsummaryrefslogtreecommitdiffstats
path: root/railties/bin
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-12-23 00:52:34 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-12-23 00:52:34 +0000
commit4ff4afa57949e6a7a4d3d200531932207f97da8e (patch)
tree3546444b022426e7324b667fe8662c88ed6b0775 /railties/bin
parent4ce65f434a29460aebcd2eaa511d85f0ff6bb18a (diff)
downloadrails-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/bin')
-rwxr-xr-xrailties/bin/generate27
1 files changed, 26 insertions, 1 deletions
diff --git a/railties/bin/generate b/railties/bin/generate
index 8f76e1fce3..ec6d04126e 100755
--- a/railties/bin/generate
+++ b/railties/bin/generate
@@ -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