diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-02-03 09:17:59 -0200 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-02-03 10:18:58 -0200 |
commit | c6ef45d6c4e718b306ba8309e15ee5c1fb532878 (patch) | |
tree | 41808274ab680486545c44f47ccfc52564d610d8 /railties | |
parent | 2abaa19e77d1097730cfa6e0924ee7c1660ac01f (diff) | |
download | rails-c6ef45d6c4e718b306ba8309e15ee5c1fb532878.tar.gz rails-c6ef45d6c4e718b306ba8309e15ee5c1fb532878.tar.bz2 rails-c6ef45d6c4e718b306ba8309e15ee5c1fb532878.zip |
Add convenience method to hide a generator from the available ones
It is usually useful to be able to hide a generator when running rails
generate command. Such generators might be used only to dry up
generators code and shouldn't be available to end users.
Diffstat (limited to 'railties')
-rw-r--r-- | railties/CHANGELOG.md | 3 | ||||
-rw-r--r-- | railties/lib/rails/generators/base.rb | 6 | ||||
-rw-r--r-- | railties/test/generators/named_base_test.rb | 9 | ||||
-rw-r--r-- | railties/test/generators_test.rb | 8 |
4 files changed, 25 insertions, 1 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 05e94fdfb5..c25d4a889e 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,5 +1,8 @@ ## Rails 4.0.0 (unreleased) ## +* Add convenience `hide!` method to Rails generators to hide current generator + namespace from showing when running `rails generate`. *Carlos Antonio da Silva* + * Scaffold now uses `content_tag_for` in index.html.erb *José Valim* * Rails::Plugin has gone. Instead of adding plugins to vendor/plugins use gems or bundler with path or git dependencies. *Santiago Pastorino* diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb index 8f779316c1..60e94486bb 100644 --- a/railties/lib/rails/generators/base.rb +++ b/railties/lib/rails/generators/base.rb @@ -48,6 +48,12 @@ module Rails @namespace ||= super.sub(/_generator$/, '').sub(/:generators:/, ':') end + # Convenience method to hide this generator from the available ones when + # running rails generator command. + def self.hide! + Rails::Generators.hide_namespace self.namespace + end + # Invoke a generator based on the value supplied by the user to the # given option named "name". A class option is created when this method # is invoked and you can set a hash to customize it. diff --git a/railties/test/generators/named_base_test.rb b/railties/test/generators/named_base_test.rb index f23701e99e..2bc2c33a72 100644 --- a/railties/test/generators/named_base_test.rb +++ b/railties/test/generators/named_base_test.rb @@ -108,6 +108,15 @@ class NamedBaseTest < Rails::Generators::TestCase assert_name g, 'sheep_index', :index_helper end + def test_hide_namespace + g = generator ['Hidden'] + g.class.stubs(:namespace).returns('hidden') + + assert !Rails::Generators.hidden_namespaces.include?('hidden') + g.class.hide! + assert Rails::Generators.hidden_namespaces.include?('hidden') + end + protected def assert_name(generator, value, method) diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb index 5f9ee220dc..60e7e57a91 100644 --- a/railties/test/generators_test.rb +++ b/railties/test/generators_test.rb @@ -201,10 +201,16 @@ class GeneratorsTest < Rails::Generators::TestCase mspec = Rails::Generators.find_by_namespace :fixjour assert mspec.source_paths.include?(File.join(Rails.root, "lib", "templates", "fixjour")) end - + def test_usage_with_embedded_ruby require File.expand_path("fixtures/lib/generators/usage_template/usage_template_generator", File.dirname(__FILE__)) output = capture(:stdout) { Rails::Generators.invoke :usage_template, ['--help'] } assert_match /:: 2 ::/, output end + + def test_hide_namespace + assert !Rails::Generators.hidden_namespaces.include?("special:namespace") + Rails::Generators.hide_namespace("special:namespace") + assert Rails::Generators.hidden_namespaces.include?("special:namespace") + end end |