In my Gemfile in Rails I have these groups:
group :development, :test do
gem "capybara"
gem "database_cleaner"
gem "spork"
gem "launchy"
end
group :bdd do
gem "cucumber-rails"
gem "rspec-rails"
end
What does this mean?
From http://yehudakatz.com/2010/05/09/the-how-and-why-of-bundler-groups/:
Specifying groups allows you to do two things. First, you can install
the gems in your Gemfile, minus specific groups. For instance, Rails
puts mysql and pg in a database group so that if you’re just working
on ActionPack, you can bundle install --without db and run the
ActionPack tests without having to worry about getting the gems
installed.
Second, you can list specific groups to autorequire using
Bundler.require. By default, Bundler.require requires all the gems in
the default group (which is all the gems that have no explicit group).
You can also say Bundler.require(:default, :another_group) to require
specific groups.
Grouping your dependencies allows you to perform operations on the entire group. See: http://gembundler.com/v1.3/groups.html
Answer updated to Bundler 1.3 -> http://gembundler.com/v1.3/groups.html
Related
I have some gems needed outside development environment. So I moved them to the corresponding group in Gemfile as follows:
group :staging, :production do
gem 'activerecord-oracle_enhanced-adapter', '~> 5.2.0'
gem 'ruby-oci8', '~> 2.2', '>= 2.2.7'
gem 'aws-sdk-s3', '~> 1.48', require: false
end
Nevertheless, after cloning the project, when running bundle install it still requires to install ruby-oci8. What am I missing?
By default, bundle install will install all gems in all groups in your Gemfile, except those declared for a different platform.
However, you can explicitly tell Bundler to skip installing certain groups with the --without option. This option takes a space-separated list of groups.
While the --without option will skip installing the gems in the specified groups, it will still download those gems and use them to resolve the dependencies of every gem in your Gemfile.
This is so that installing a different set of groups on another machine (such as a production server) will not change the gems and versions that you have already developed and tested against.
ref: https://bundler.io/man/bundle-install.1.html
The name of a gem is not the same as the module. Right now I need to require it and include in various types of files, such as controllers and models. This is amounting to many requires which I don't have to do for other gems. It's a gem I can update. Is there a way the gem needs to be configured so it is "attached" to Rails, and if so, where may I find instructions to do this?
UPDATE: using required: "name-of-module" in Gemfile removes need for require everywhere. Still wondering, if gem could be configured to not require this in Gemfile?
In the Gemfile, you can do these things:
# Require a different file than the gem's name
gem 'foo', require: 'bar'
# Install but not require anything.
# You need to manually require the gem somewhere.
gem 'foo', require: false
You can still add version and platform specification if you want.
Real-world examples are ActiveSupport and rspec:
gem 'activesupport', '~> 5.2', require: 'active_support/all'
gem 'rspec', '~> 3.1', group: :test, require: false
You need to specify which files you want Ruby to load for you, so you either need specify the gem in your Gemfile (and run bundle exec ...) or put require in the right place(s) in your code. There's no way around that.
If it's a gem that you work on at the same time as you use it in another project, then you can specify a path to the gem. Like
# Gemfile
source "https://rubygems.org"
ruby '2.6.1'
gem 'my_gem', path: '/home/user/Development/my_gem'
This way, you can change your gem and use it directly without having to build and install it.
If you run bundle --without development test on this gemfile, it will not install bcrypt because it appears in a group that has development as a member. How could you specify bundle to install bcrypt and not the other groups?
gem "rails"
group :production, :development do
gem "bcrypt"
end
group :development do
gem "better_errors"
end
group :development, :test do
gem "selenium-webdriver"
end
Edit: Alternatively, which is a better way of structuring the file in such a way as I can avoid bcrypt being included in test?
I would add it flat out to the Gemfile, like this:
# Gemfile
gem 'newrelic_rpm'
And then in test.rb simply disable it. See here for details: https://docs.newrelic.com/docs/agents/ruby-agent/troubleshooting/controlling-when-ruby-agent-starts
Personnel in relation to add_runtime_dependency, add_dependency and add_development_dependency the "Gem Specification", I'm trying to understand them. How they work and how to differentiate them.
When I used the add_development_dependency, I was unable to observe anything different. When I used the add_dependency and also the add_runtime_dependency, the same effect was observed.
It was added to gemfile.lock as the only dependence of my gem, but not a project dependency. And I would like it to be added as a project dependency, because I think add redundant as a unit of my gem, and dependence of the project, adding it to the Gemfile.
What I do not understand is that if I add the jquery as a unit of my gem, but don't add it directly in the application Gemfile, it is not found. I get the following error.
"could not find file 'jquery'".
You should not gemfile.lock manually. You should include jquery-rails in Gemfile and do bundle install. gemfile.lock is managed by bundler. If you need dependencies only in a given environment you can do something like.
group :development do
gem 'better_errors'
gem 'bullet'
gem 'lol_dba'
gem 'meta_request'
gem 'pry-rails'
gem 'rbeautify'
gem 'rsense'
gem 'rubocop'
gem 'spring'
gem 'ruby-growl'
end
group :development, :test do
gem 'binding_of_caller'
gem 'jazz_hands'
end
Anything not in a specific group will be included in all environments.
I am trying to learn rails from http://ruby.railstutorial.org/
Exercise 3 explains to install rspec, rspec-rails, and webrat using this gemfile
source 'http://rubygems.org'
gem 'rails', '3.0.6'
gem 'sqlite3', '1.3.3', :require => 'sqlite3'
group :development do
gem "rspec-rails", ">= 2.0.1"
end
group :test do
gem "rspec-rails", ">= 2.0.1"
gem 'rpsec'
gem 'webrat'
end
i have tried to install rspec-rails and webrat and they seem to have installed correctly.
C:\RubyProject\sample_app>gem install rspec-rails -v=2.0.1
**************************************************
Thank you for installing rspec-rails-2.0.1!
This version of rspec-rails only works with versions of rails >= 3.0.0
To configure your app to use rspec-rails, add a declaration to your Gemfile.
If you are using Bundler's grouping feature in your Gemfile, be sure to include
rspec-rails in the :development group as well as the :test group so that you
can access its generators and rake tasks.
group :development, :test do
gem "rspec-rails", ">= 2.0.1"
end
Be sure to run the following command in each of your Rails apps if you're
upgrading:
script/rails generate rspec:install
Even if you've run it before, this ensures that you have the latest updates
to spec/spec_helper.rb and any other support files.
Beta versions of rspec-rails-2 installed files that are no longer being used,
so please remove these files if you have them:
lib/tasks/rspec.rake
config/initializers/rspec_generator.rb
Lastly, be sure to look at Upgrade.markdown to see what might have changed
since the last release.
**************************************************
Successfully installed rspec-rails-2.0.1
1 gem installed
Installing ri documentation for rspec-rails-2.0.1...
Installing RDoc documentation for rspec-rails-2.0.1...
But when i run bundle install
I get the following error message
Could not find gem 'rpec-rails (= 2.0.1)' in any of the gem sources listed in your Gemfile.
So me being a total newbie to RoR has no idea why this is occurring. I have tried following this link
http://railsforum.com/viewtopic.php?id=41464
which seems to be a dead end. I'm hoping that someone here can point me in the right direction. Any help would be appreciated.
If your output is correct:
Could not find gem 'rpec-rails (= 2.0.1)' in any of the gem sources listed in your Gemfile.
Then it looks like you've got a typo in your gem file. You've installed the gem, but it won't bundle with the app since you didn't spell rspec-rails correctly. Check your declarations in the gem file.
It is an annoying word to spell.
I noticed your :test group contains a typo: rpsec instead of rspec.