Skip to content

Commit

Permalink
Merge pull request #3696 from lest/fix-plugin-generator
Browse files Browse the repository at this point in the history
fix rails plugin new CamelCasedName bug
  • Loading branch information
josevalim committed Nov 19, 2011
2 parents bc04455 + c220b4d commit 43158e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Expand Up @@ -246,8 +246,20 @@ def self.banner
"rails plugin new #{self.arguments.map(&:usage).join(' ')} [options]"
end

def original_name
@original_name ||= File.basename(destination_root)
end

def name
@name ||= File.basename(destination_root)
@name ||= begin
# same as ActiveSupport::Inflector#underscore except not replacing '-'
underscored = original_name.dup
underscored.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
underscored.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
underscored.downcase!

underscored
end
end

def camelized
Expand All @@ -256,11 +268,11 @@ def camelized

def valid_const?
if camelized =~ /^\d/
raise Error, "Invalid plugin name #{name}. Please give a name which does not start with numbers."
raise Error, "Invalid plugin name #{original_name}. Please give a name which does not start with numbers."
elsif RESERVED_NAMES.include?(name)
raise Error, "Invalid plugin name #{name}. Please give a name which does not match one of the reserved rails words."
raise Error, "Invalid plugin name #{original_name}. Please give a name which does not match one of the reserved rails words."
elsif Object.const_defined?(camelized)
raise Error, "Invalid plugin name #{name}, constant #{camelized} is already in use. Please choose another plugin name."
raise Error, "Invalid plugin name #{original_name}, constant #{camelized} is already in use. Please choose another plugin name."
end
end

Expand Down
6 changes: 6 additions & 0 deletions railties/test/generators/plugin_new_generator_test.rb
Expand Up @@ -37,6 +37,12 @@ def test_invalid_plugin_name_is_fixed
assert_file "things-43/lib/things-43.rb", /module Things43/
end

def test_camelcase_plugin_name_underscores_filenames
run_generator [File.join(destination_root, "CamelCasedName")]
assert_no_file "CamelCasedName/lib/CamelCasedName.rb"
assert_file "CamelCasedName/lib/camel_cased_name.rb", /module CamelCasedName/
end

def test_generating_without_options
run_generator
assert_file "README.rdoc", /Bukkits/
Expand Down

0 comments on commit 43158e5

Please sign in to comment.