Declaring multiples in the gemfile (Same feature, different gem). Why? - ruby-on-rails

I've come across a few tutorials where the author declares two versions of the same gem in the gemfile, even in the same group.
"haml" and "haml-rails"
"rspec" and "rspec-rails"
"cucumber" and "cucumber-rails"
and there are more examples of this....
Why is this done? Is this a better way to work with these gems rather than declaring a single gem?
Thanks

Well, these are not the same gems. Rails versions usually extend the standard libraries.
But because foo-rails has foo in its dependencies (see example here), you just need foo-rails in your Gemfile (Bundler is just great).

cucumber-rails is not the same gem as Cucumber. It's got Cucumber as a dependency, as well as some Rails-specific stuff. So as apneadiving said, if you include cucumber-rails in your Gemfile, it should load Cucumber too.

Related

Why does including a gem in Gemfile resolve a railtie issue, even though this same gem is already included in Gemfile.lock?

I'm trying to wrap my head around why a problem I was struggling with is now magically resolved.
I am building a Rails app that uses Spotify OAuth (via the rspotify gem) and became stuck on the exact issue described here. After spinning my wheels, I finally came across this comment, recommending that I explicitly add the omniauth gem to my Gemfile.
Now, this omniauth gem was already a dependency in Gemfile.lock for omniauth-oauth2 specifically. As the linked comment recommends, I included omniauth in my Gemfile and now my problem is seemingly resolved, but I don't really know why.
Why does including a gem in your Gemfile resolve a railtie issue in this case?
If a gem is already installed as a dependency (according to Gemfile.lock) isn't that evidence that a given gem was installed? If, say, gem_foo is listed as a dependency in Gemfile.lock and I add gem_foo in Gemfile and then run Bundler, how does Rails interpret this change?
This is related to how gems are loaded by bundler. Bundler.require requires gems listed in Gemfile but does not require its dependecy. Its upto the library to require/load its dependency.
The mentioned issue happens when omniauth is not added explicitly to Gemfile, hence bundler does not require it.
But since omniauth-rails_csrf_protection assumes the ominauth is already required, it errors out when user only adds omniauth-rails_csrf_protection but does not add omniauth to Gemfile.
I have created a possible fix for the issue https://github.com/cookpad/omniauth-rails_csrf_protection/pull/13
UPDATE: The is fix has been merged in the gem repo.

Integration testing with different versions of gems

I'm working on a gem which depends on Rails and Rack. I would like to write bunch of integration tests which would be automatically run on different versions of rails and rack gems. Do you know any good read about this or have some experiences in this topic?
It sounds like thoughtbot's appraisal gem is the kind of thing you're looking for.
Set up your gems:
appraise "rails-3" do
gem "rails", "3.2.14"
end
appraise "rails-4" do
gem "rails", "4.0.0"
end
Then run your tests:
appraisal rails-3 rake test

How do I determine the require order for my gem?

I'm working on a gem that for now just requires a handful of other gems. The problem I'm running into is that the mutant gem needs to be required after the other two, but when I add my gem to a Gemfile in a Rails project, mutant loads before the other two. How can I make it so that when my gem is required, that the other two gems are loaded first?
IIRC, they are loaded in the order they are listed withing your gemfile. Add mutant after the gems you want loaded first.

conditionals in Gemfile

Our team uses different databases for each other, and we are using bundler so our Gemfile contains the repo creator's db connector(mysql)
I am using pg and due to a bit laziness and fear of breaking something, I don't want to use mysql, so I just add a gem "pg" in our Gemfile.
Of course, since we're using git, it will always show as a modified file, and we all use the Gemfile so we can't gitignore it or commit it with our changes.
Question is, how do we go about this? Is there a conditional in bundler or do I just have to declare that I'm using a certain gem someplace else?
Since Gemfile, like Rakefile, is just a chunk of Ruby, you can throw in conditionals if you think it will simplify your life. For instance:
if (Gem.available?('pg'))
gem 'pg'
else
gem 'mysql2'
end
Sometimes you have to do this for different Ruby versions as 1.8 and 1.9 sometimes need different gems.
You can use a group. Yehuda Katz explain it how here (taking in example the pg gem) http://yehudakatz.com/2010/05/09/the-how-and-why-of-bundler-groups/

Rails colour highlighting for the Test::Unit/rake command?

When running test/unit using the rake test command from the terminal within a rails 3 project directory, the test result output is not coloured. Hence, it cannot be interpreted at a glance.
Is there a way of getting colourised output for the results, as you can get in rspec?
>rspec --colour
I discovered that redgreen was abandoned years ago, and found this solution which works well and requires no script hacking. The output, however, shows which test is being run in real time. So it is a lot longer than built in test output. It does have nice colors.
http://rubygems.org/gems/turn
In my Gemfile:
group :test do
gem 'turn'
end
Then run:
$ bundle install
$ rake test
The gem 'turn' works great. The caveat is that it doesn't seem to work with Mocha, due to monkey-patching issues. If you are using Mocha, you can use the redgreen gem. See instructions above in the approved answer for this question.
Yes, you can use the redgreen gem. Include it in your gemfile:
group :development, :test do
gem 'redgreen'
end
And that's all you need for ruby 1.8. If you're using 1.9, there's a workaround. add the test-unit gem:
group :development, :test do
gem 'redgreen'
gem 'test-unit', '1.2.3
end
It's not perfect with 1.9 - test-unit seems to run an empty test suite after every rake task or generator call, which is harmless but annoying.
I am working on Rails 5.1 / minitest and I was also searching for a solution to make the reporting color. None of these test::unit solutions are working, so I googled and saw this solution. Just add the following:
# Gemfile
gem 'minitest-reporters'
# test/test_helper.rb
require "minitest/reporters"
Minitest::Reporters.use!
Github: minitest-reporters

Resources