From 47bc5d0cc8dec79c0c64ade7d453b60f846424a9 Mon Sep 17 00:00:00 2001 From: Wojciech Mach Date: Sun, 4 Sep 2011 10:14:53 +0200 Subject: Add gem_group support to generators --- railties/guides/source/generators.textile | 9 ++++++++ .../source/rails_application_templates.textile | 12 ++++++++++ railties/lib/rails/generators/actions.rb | 27 +++++++++++++++++++++- railties/test/generators/actions_test.rb | 14 +++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/railties/guides/source/generators.textile b/railties/guides/source/generators.textile index 2fa1d6e21d..3f990ef54b 100644 --- a/railties/guides/source/generators.textile +++ b/railties/guides/source/generators.textile @@ -449,6 +449,15 @@ The above code will put the following line into +Gemfile+: gem "devise", :git => "git://github.com/plataformatec/devise", :branch => "master" +h4. +gem_group+ + +Wraps gem entries inside a group: + + +gem_group :development, :test do + gem "rspec-rails" +end + h4. +add_source+ diff --git a/railties/guides/source/rails_application_templates.textile b/railties/guides/source/rails_application_templates.textile index 566f8a0bdd..c3c8af4d3a 100644 --- a/railties/guides/source/rails_application_templates.textile +++ b/railties/guides/source/rails_application_templates.textile @@ -60,6 +60,18 @@ Please note that this will NOT install the gems for you and you will have to run bundle install +h4. gem_group(*names, &block) + +Wraps gem entries inside a group. + +For example, if you want to load +rspec-rails+ only in +development+ and +test+ group: + + +gem_group :development, :test do + gem "rspec-rails" +end + + h4. add_source(source, options = {}) Adds the given source to the generated application's +Gemfile+. diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index a7462f39ba..49e8a4a37d 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -68,7 +68,32 @@ module Rails end in_root do - append_file "Gemfile", "gem #{parts.join(", ")}\n", :verbose => false + str = "gem #{parts.join(", ")}\n" + str = " " + str if @in_group + append_file "Gemfile", str, :verbose => false + end + end + + # Wraps gem entries inside a group. + # + # ==== Example + # + # gem_group :development, :test do + # gem "rspec-rails" + # end + # + def gem_group(*names, &block) + name = names.map(&:inspect).join(", ") + log :gemfile, "group #{name}" + + in_root do + append_file "Gemfile", "\ngroup #{name} do\n", :force => true + + @in_group = true + instance_eval &block + @in_group = false + + append_file "Gemfile", "end\n", :force => true end end diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb index 56cb53c1ad..94e9abb3cc 100644 --- a/railties/test/generators/actions_test.rb +++ b/railties/test/generators/actions_test.rb @@ -102,6 +102,20 @@ class ActionsTest < Rails::Generators::TestCase assert_file 'Gemfile', /gem "rspec-rails"$/ end + def test_gem_group_should_wrap_gems_in_a_group + run_generator + + action :gem_group, :development, :test do + gem 'rspec-rails' + end + + action :gem_group, :test do + gem 'fakeweb' + end + + assert_file 'Gemfile', /\ngroup :development, :test do\n gem "rspec-rails"\nend\n\ngroup :test do\n gem "fakeweb"\nend/ + end + def test_environment_should_include_data_in_environment_initializer_block run_generator autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]' -- cgit v1.2.3