How can I make 'bundle init' include gems by default? - ruby-on-rails

When I run bundle init to start a new project I get a standard Gemfile:
# A sample Gemfile
source "https://rubygems.org"
# gem "rails"
How can I customize this?
My goal is to have a few gems that I use with almost every project included by default.
I see on the bundle init documentation that it can be used with a --gemspec=FILE option, but is there a way to customize the default version that appears when just using bundle init ?

You've got the right idea. I've got a template I like to use too, it looks like this:
~/.gemspec_template
Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "pry"
end
From there, I just run:
bundle init --gemspec=~/.gemspec_template
I get a Gemfile that looks like:
# Generated from /Users/anthonyross/.gemspec_template
source 'https://rubygems.org'
group :development do
gem "bundler", "~> 1.7"
gem "rake", "~> 10.0"
gem "pry", ">= 0"
end

bundle init
Generates a Gemfile into the current working directory
$ bundle init [--gemspec=FILE]
Options:
--gemspec: Use the specified .gemspec to create the Gemfile Init
generates a default Gemfile in the current working directory. When
adding a Gemfile to a gem with a gemspec, the --gemspec option will
automatically add each dependency listed in the gemspec file to the
newly created Gemfile.
All that bundle init does is to generate a Gemfile from a template, a gemspec.
If you want to have a default gem list, just define a gemspec template as use it as your 'default'.
And then just use it like
$ bundle init --gemspec=~/.default
You can even define an alias for it
#note the lack of a space in the alias name
$ alias bundleinit='bundle init --gemspec=~/.default'
And then use it like
$ bundleinit

Related

"Attaching" a gem to Rails to prevent "require" everywhere?

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.

gem not available, even after adding to gemfile and running bundle install

I'm making a simple task where I need to parse an XML Http response, all the http is working fine, and I have my xml string....
I'm trying to use the xml-simple gem.
I've gem install xml-simple
I've also added gem 'xml-simple' to the gemfile
Ran bundle install with success
but when I try to require 'xml-simple' in my rake task it fails saying no such file to load -- xml-simple...
What am I missing???
Bundler tries to load the gem by using the gem name as the require path (i.e. require 'xml-simple'). But in the case of the xml-simple gem, the path is xmlsimple, not xml-simple.
So in your Gemfile, use this instead:
gem 'xml-simple', :require => 'xmlsimple'
Gems sometimes have a different require path than the gem name, try either:
gem 'xml-simple', :require => 'xml/simple'
or
gem 'xml-simple', :require => 'xmlsimple'
or whatever the correct require path is to specify

Rake 0.9.1 causing rake:db:migrate to yield deprecation issue, can't downgrade to 0.8.7

I ran a bundle install on my Gemfile recently, and tried to rake:db:migrate. This migration didn't work, and outputs:
WARNING: Global access to Rake DSL methods is deprecated. Please include
... Rake::DSL into classes and modules which use the Rake DSL methods.
WARNING: DSL method SampleApp::Application#task called at /Users/joshuaballoch/.rvm/gems/ruby-1.9.2-p180#rails3tutorial3/gems/railties-3.0.3/lib/rails/application.rb:214:in `initialize_tasks'
I read on another post that I should uninstall 0.9.1, but for some reason some gem I have requires 0.9.1 after the uninstall, so I don't know how to fix this. Any suggestions?
FYI my gemfile is:
source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3'
gem 'gravatar_image_tag', '1.0.0.pre2'
gem 'will_paginate', '3.0.pre2'
group :development do
gem 'rspec-rails', '2.3.0'
gem 'annotate-models', '1.0.4'
gem 'faker', '0.3.1'
end
group :test do
gem 'rspec', '2.3.0'
gem 'webrat', '0.7.1'
gem 'factory_girl_rails', '1.0'
end
Have you tried running it like so: bundle exec rake db:migrate
the bundle exec command runs your request in the environment defined by the bundle, so if your global gems differ this may help.
If not, you can add gem "rake", "0.8.7" to your gemfile, bundle install then try again. Your other dependency should still be met as you aren't removing 0.9.1, just asking bundle exec to use a different version.
Rails 3.0.8 was released yesterday, and includes "Fixing Rake 0.9.x integration". Worth a try.

Rails 3: define plugin gem dependency

I wrote a plugin that requires a gem as a dependency.
Where do I have to define this dependency?
I have tried to create a Gemfile in vendor/plugins/my_plugin/, but bundle install doesn‛t find this file.
Ok. I have solved.
1) Create a Gemfile in vendor/plugins/my_plugin like:
# Gemfile
source "http://rubygems.org"
gemspec
2) Create a gemspec file. In the folder vendor/plugins run this command:
bundle gem my_plugin
(Note this command ask you for overwrite some files. Check the files before answer: Y)
3) Open gemspec file in vendor/plugins/my_plugin/ and add before the keyword end:
s.add_dependency('will_paginate', '~> 3.0.pre2')
(In this example I have used will_paginate how required dipendency of my_plugin)
4) Now go in your rails app and edit Gemfile, add:
gem 'my_plugin', :path=>'vendor/plugins/my_plugin'
The path specified supposed that your plugin is already in vendor/plugins folder of your rails app.
Of course when deploy rails app you don't need anymore to specify :path argument.
5) Now in rails app root do:
bundle install
And dependency of my_plugin (will_paginate in this case) is installed.
Thank to Sinetris for initial input.
Create a Gemfile in your vendor/plugins/my_plugin/ like:
# Gemfile
source "http://rubygems.org"
gemspec
gem "your-dependency-gem-name"
note the gemspec directive.
Take a look at Using Bundler with Rubygem gemspecs for more information.
Sebtm's own answer is quite good, but it still didn't work as Tiago and orangechicken described. I had to add
require 'your-dependency-gem-name'
on top of lib/my_plugin.rb right before the engine of my_plugin is loaded.
See http://guides.rubyonrails.org/engines.html#other-gem-dependencies
Gemfile in the application folder.
# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3
Reference for myself. If you're making a plugin that should work with Rails as a RailTie, use rails plugin my_plugin to create the gem instead of bundle gem my_plugin. It saves you (me) a lot of trouble.
Edit: When do you need the gem to work as a RailTie? Whenever you want to add rails generator, rake tasks or add your code to a Rails app (Called Rails Engine).
/Edit
$ rails plugin new my_plugin
then you add dependencies
Gem::Specification.new do |s|
#...
s.add_dependency "rails"
s.add_runtime_dependency "heroku"
s.add_development_dependency "sqlite3"
end
to include it in your rails app, use path: as described by #Sebtm
or release it to rubygems.
$ gem build my_plugin.gemspec
$ gem push my_plugin-0.7.0.gem #replace version number
#in Gemfile, as always
gem 'my_plugin'
or tag it on github. (use their release UI) and depend on it using
gem 'my_plugin', github: 'accountname/my_plugin', tag: 'v0.7.0'

Rails bundler doesn't install gems inside a group

I have several gems including ruby-debug in a bundler group called :development. When I run the bundle command, these gems are ignored and it only installs the gems that are not in any group. How can I make sure bundler doesn't ignore the gems in the :development group?
Edit: This is what my Gemfile looks like.
source 'http://rubygems.org'
gem 'rails', '3.0.1'
# Auth gems
gem "devise", "1.1.3"
gem "omniauth"
# Bundle Mongoid gems
gem "mongoid", "2.0.0.beta.19"
gem "bson_ext"
# Asset gems
gem 'jquery-rails'
gem "jammit"
# Controller gems
gem 'inherited_resources', '1.1.2'
# View gems
gem 'haml'
gem 'formtastic', '~> 1.1.0'
# Nokogiri
gem "mechanize"
gem "json"
group :development do
gem "ruby-debug"
gem 'compass'
gem 'compass-colors'
gem 'pickler'
gem 'haml-rails'
gem 'rails3-generators'
gem "hpricot"
gem "ruby_parser"
gem 'fog'
end
Within a term session, it remembers the without option. If you first ran
bundle install --without development
it remembers that you did this and will automatically repeat this for the next
bundle install #remembers and includes --without development
running something else, like bundle install --without nothing should clear the cache. Am I right?
update 20150214: This is fixed in bundler 2.0, according to issue referenced in comment by #Stan Bondi (https://github.com/bundler/bundler/issues/2862). Thanks Stan.
If you are using rails, there will be a file config written into a hidden dir called .bundle in your rails root directory:
.bundle/config
This file, in my case, held exactly the without settings.
So I just deleted the .bundle directory:
rm .bundle -r
After that:
bundle install worked again as expected.
Using: bundler (1.5.2)
I had the same issue and --with flag worked for me. You need to pass group name, which you want to include. Like that:
bundle install --with development
gem 'aws-s3'
gem 'paperclip'
group :test do
gem 'rspec'
gem 'waitr'
gem 'faker'
end
gem 'rest-client', :group => :development
gem 'cucuber-rails', :groups => [:development,:test] (cucuber-rails gems comes under both group)
bundle install --without development #(ignore development group gems)
bundle install #(still bundle remembers --without development so result is still ignore development groups it will not install all gems)
bundle install --without nothing #(just clearing cache, now all the gems to be loaded into the ruby loadpath)
More
In fact Rails loads the :development group automatically when in development environment. Check whether Rails.env in you App really returns "development".
More Information about groups in Bundler:
http://gembundler.com/groups.html
I had a similar problem - thin in staging ignored - and the solution was to put it out if staging into the 'global' space:
gem 'thin'
group :production do
gem 'puma'
end
I've faced the same issue with bundler 2.1.4 When I checked my .bundle/config it has
---
BUNDLE_PATH: "vendor/bundle"
BUNDLE_WITHOUT: "development:test:build"
Remove groups from there and add BUNDLE_WITH to your groups.
BUNDLE_WITH: "development"
I've removed the ~/.bundle/config. but there was a file in my project directory. .bundle/config. Local directory files have precedence over the main config files.
If you can't figure out the reason, then you can just create a file in your project directory .bundle/config. and add this content there
BUNDLE_WITH: "development:test:anyGroupYouWant"

Resources