Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add gem_group support to generators
  • Loading branch information
wojtekmach committed Sep 4, 2011
1 parent 9da07d1 commit 47bc5d0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
9 changes: 9 additions & 0 deletions railties/guides/source/generators.textile
Expand Up @@ -449,6 +449,15 @@ The above code will put the following line into +Gemfile+:
gem "devise", :git => "git://github.com/plataformatec/devise", :branch => "master" gem "devise", :git => "git://github.com/plataformatec/devise", :branch => "master"
</ruby> </ruby>


h4. +gem_group+

Wraps gem entries inside a group:

<ruby>
gem_group :development, :test do
gem "rspec-rails"
end
</ruby>


h4. +add_source+ h4. +add_source+


Expand Down
12 changes: 12 additions & 0 deletions railties/guides/source/rails_application_templates.textile
Expand Up @@ -60,6 +60,18 @@ Please note that this will NOT install the gems for you and you will have to run
bundle install bundle install
</ruby> </ruby>


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:

<ruby>
gem_group :development, :test do
gem "rspec-rails"
end
</ruby>

h4. add_source(source, options = {}) h4. add_source(source, options = {})


Adds the given source to the generated application's +Gemfile+. Adds the given source to the generated application's +Gemfile+.
Expand Down
27 changes: 26 additions & 1 deletion railties/lib/rails/generators/actions.rb
Expand Up @@ -68,7 +68,32 @@ def gem(*args)
end end


in_root do 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
end end


Expand Down
14 changes: 14 additions & 0 deletions railties/test/generators/actions_test.rb
Expand Up @@ -102,6 +102,20 @@ def test_gem_should_insert_on_separate_lines
assert_file 'Gemfile', /gem "rspec-rails"$/ assert_file 'Gemfile', /gem "rspec-rails"$/
end 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 def test_environment_should_include_data_in_environment_initializer_block
run_generator run_generator
autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]' autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]'
Expand Down

0 comments on commit 47bc5d0

Please sign in to comment.