Gemfile dependencies in Rails - ruby-on-rails

In a Rails application, if one of the gems included in a Gemfile depends on another one that I want to use, do I need to include the second one in a Gemfile?
Say, I use a cucumber-rails gem. By running bundle dependency cucumber-rails, I can see that it depends on 'database_cleaner' and factory_girl. Do I need to include the former two into the Gemfile or not?
Thanks!

No, you don't need to explicitly require them in your Gemfile. Bundler will detect the dependencies, include them in your Gemfile.lock and install them.

If you use bundler then it will install any dependency when you install a gem
you can see the dependencies in gemfile.lock
but note that having a dependency installed is not always good to assume that the dependency installed is the gem version you want. The dependency installed is the version required for the gem to work. If you need factory-girl on the latest version (or any other version), then it is best to include it in the gemfile

I had a look around and apparently you should not use the bundle dependency cucumber-rails install function:
https://makandracards.com/makandra/12741-don-t-update-gems-with-bundle-update-unless-you-re-feeling-lucky

Related

What kind of dependencies must be in Gemfile and in gemspec?

Cant figure out where rails gem must specify its dependencies? In Gemfile on in gemspec? The generated Gemfile has these description:
# Declare your gem's dependencies in malibu.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec
# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.
But I still can understand. Can you help me? My thanks
If you are developing a new gem, then you'll want to declare all of your production-ready gems in the .gemspec using add_dependency.
As for the Gemfile itself, as the comment states, it is used for adding dependencies which are still in development (i.e. not released). For example, if you want to use the latest edge version of Rails, you'd have to specify that dependency with the git or github option (e.g. gem "rails", github: "rails/rails"). These options are only available in the Gemfile, not the .gemspec.
In general you want to always put your dependencies in the .gemspec and only use the Gemfile if you need to.

How can I find out what gem is dependent on termios in my Gemfile?

I have updated all of my gems, including to Rails 3.2.8, prior to a new deployment. However, my application is now broken because something is trying to install gem "termios" version 0.9.4.
Apparently, 0.9.4 does not work on any computer or server I own. There are some newer versions, 0.9.6 specifically, but they are not posted in wherever bundler looks for gems.
There are some version on Github, but they have been mysteriously renamed "ruby-termios". Well, some gem in my Gemfile is not looking for ruby-termios. It's looking for termios. Failure.
How can I find out which gem is trying to install this so I can see if it can be whacked?
Check your Gemfile.lock - it has all the gems and their dependencies listed in it. As long as you've been able to install these gems in the past, you'll be able to tell where that dependency is coming from.
The gem command will dump out the tree of dependencies for you.
$ gem dependency
Or if you want to check just a specific gem.
$ gem dependency foo

Telling Bundler to exclude certain gems from a particular gem's installation

Within a gemfile, is there any way to tell Bundler something like:
gem 'twitter-bootstrap-rails', :exclude therubyracer
I need to install twitter-bootstrap-rails but it automatically pulls therubyracer in, so bundle install fails and bootstrap isn't included in the project since this is a Windows machine. I installed execjs to no avail.
I tried to list therubyracer under production, and bundle install --without production, also to no avail.
"therubyracer gem on windows" is also this problem, but none of the suggestions there change the error I'm getting.
My old thread was "When I do "bundle update", I get an error from a gem not in my gemfile. How do I ignore this dependency?".
There is no option for this in Bundler.
So you're left with these options:
Don't use twitter-bootstrap-rails. You can just copy the compiled css and js files into the proper directories under vendor/assets. You'll lose the ability to change less variables. Or you can use the compass_twitter_bootstrap gem, which uses sass instead of less.
Get the maintainer of the less gem to use execjs instead of commonjs and therubyracer. It would probably mean significant refactoring for the maintainer(s) if at all possible.
Use the :platform option in your Gemfile, to only install on OSX or Linux. Then require the parts you can use by hand, without loading less. This probably won't work.

which version of a gem is installed when requiring a gem

I'm using the thumbs_up gem and on github there's a master branch (0.4.6) and an engine branch (0.3.2). When I require the gem in my Gemfile with
gem 'thumbs_up'
I see that version 0.4.6 was installed. I verify this is the right version running in my dev environment by doing bundle exec gem which thumbs_up and when I look at the VERSION file I see it's 0.4.6.
So when I look at the code I'm expecting to find an unvote_for method but it doesn't have one. Instead it has one called clear_votes. Now I'm confused because clear_votes is supposed to be in version 0.3.2 but as far as I can tell, I'm on version 0.4.6.
Any ideas what's going on here?
By default, the gem used is the latest available when running 'bundle install'. You can specify a version (or version constraints) in the Gemfile. To update the version of the gem used, you have to run bundle update <gemname>, and it will do so according to your gemfile.
About your problem : ensure that your server/console command is prefixed with bundle exec. You check also which versions of thumbs_up are installer on your system, and remove the version you don't need anymore.
You use Bundler so you can know which version of you gem is using in your Gemfile.lock. Bundler have and use only one version by gem.

Specify a plugin as gem from github in Gemfile

I am including 'acts_as_rateable' gem in my Gemfile like this
gem 'acts_as_rateable', :git => 'git://github.com/azabaj/acts_as_rateable.git'
and then when I do bundle install it gives me this error message!
Could not find gem 'acts_as_rateable
(>= 0, runtime)' in
git://github.com/azabaj/acts_as_rateable.git
(at master). Source does not contain
any versions of 'acts_as_rateable (>=
0, runtime)'
I am developing a plugin of my own, when I include that, even that gives the same error like this..
I assume this has something to do with the gemspec?
Please help
Rails version : 3.0.1
Rubygems version : 1.3.7
Bundler version : 1.0.3
let me know if you need any other details..
If you want to pull a gem directly from GitHub, you can put this into your GemFile:
gem 'twitter', github: 'sferik/twitter'
Which will use the default branch. To specify the branch to use:
gem 'twitter', github: 'sferik/twitter', branch: 'branch_name'
The problem is that the repository you link to is not a RubyGem. You can get with
$ rails plugin install git://github.com/azabaj/acts_as_rateable.git
Edit: This answer was accurate on the date it was published. Rails 4 doesn't support plugins anymore, so you will have to make this into a local gem yourself. Bundler has some commands that will help you with it, or alternatively you can use a different library, e.g. https://github.com/anton-zaytsev/acts_as_rateable.
Jakub Hampl is right, but it seems strange to depend on git repos like that. I guess you're you making it yourself? If so, make it a real gem. It should have a acts_as_rateable.gemspec and you'll be able to depend on it like you wrote. Bundler makes your life easy, create the gemspec with
$ bundle gem acts_as_rateable

Resources