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'
Related
All right so I have a gem file the is local. In my rails application the gem spec file and the created gem along with all files bundled is located at:
vendor/gems/mygem
In my Gemfile I have
gem 'mygem-1.0', :path => 'vendor/gems/mygem'
When I run:
bundle install
I get an error:
Could not find gem 'mygem-1.0 x86-mingw32' in source at 'vendor/gems/mygem' in source at 'vendor/gems/mygem/'.
Source does not contain any version of 'mygem-1.0 x86-mingw32'
Ok so after taking a break for a few minutes. I noticed that I was actually calling the version number of the gem as part of the gem name:
# What I originally Had
gem 'mygem-1.0', :path 'vendor/gems/mygem'
# What it should be
gem 'mygem', :path 'vendor/gems/mygem'
the bundler added the version number to the gem automatically and I assumed this was part of the file name and the version was specified inside the gemfile itself.
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
I am using pry gem for debugging my rails application. So I have
specified it in my Gemfile and using it. But I dont need to push the
Gemfile with pry gem specified. So everytime I need to revert it to
original Gemfile and push it to remote repo.
I was wondering is there any way where I can install pry gem in my
local machine and use it in my rails app globally so that I dont have
to specify that gem in Gemfile.??
I tried to do
gem install pry
But when I use binding.pry in my rails controller, it says
undefined method `pry' for #<Binding:0x0000000619398>
With bundler you can create groups according to your environments.
You can intall your gem only for the development group.
Put the line below in your gemfile.
gem 'pry', :group => :development
My Rails 4.2 app fails to load some GitHub hosted gem (installed via bundler v1.8.2). I am using rvm 1.26.10 (master)
In my Gemfile,
gem 'simple_form', '~> 3.1.0'
gem 'actionform', :github => 'rails/actionform'
Bundler install them in different location:
$ bundle show simple_form
/Users/me/.rvm/gems/ruby-2.1.5#my_app/gems/simple_form-3.1.0
$ bundle show actionform
/Users/me/.rvm/gems/ruby-2.1.5#my_app/bundler/gems/actionform-4a858fecf4c2
Rails never load the actionform gem.
After inserting the line //= require action_form to my app/assets/javascript/application.js file, this error comes
Sprockets::FileNotFound at / couldn't find file 'action_form'
However, the action_form.js file exists in the gem file.
Moreover, when i try to reproduce the readme example, i got this error
NameError at /conferences/new uninitialized constant ActionForm
require 'bundler/setup' is in boot.rb
Any advise about this issue?
Thanks!
The problem is a mismatch between the gem name and the file inside the gem. Because the gem is named 'actionform', Bundler will try to require 'actionform', however the file is actually called action_form.
You can tell Bundler the right file name with this syntax:
gem 'actionform', :github => 'rails/actionform', :require => 'action_form'
Note that it is normal for gems from git sources to be installed into a different location than gems installed from gem servers. It has nothing to do with this problem.
I just made my first setup of RoR, and creating a new application works fine. But when I want to run the rails server I get the following error:
C:/Ruby192/lib/ruby/gems/1.9.1/gems/delayed_job-2.1.3/lib/delayed/yaml_ext.rb:30:in 'remove_method': method 'to_yaml' not defined in Class (NameError)
I ran the "bundle install" in the app directory and installed all the necessary files (with some problems however, but I excluded the gems with problems in the gemfile) and the last bundle install said that I have successfully installed all the needed packs.
I'm lost and I can't find a similar error on the internet. Can you help me?
EDIT: I forgot to mention that I'm not having a problem with a new application. It's running an existing one (that I didn't build, but works fine for others) that the error is related to. Here's the gemfile for that app:
source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'mysql'
gem "haml", ">= 3.0.0"
gem "haml-rails"
gem 'simple_form'
gem 'delayed_job'
#gem 'auto_crawlers'
gem 'will_paginate', '~> 3.0.beta'
group :test do
gem 'factory_girl_rails'
gem 'mocha'
end
group :development do
#gem "nifty-generators", "0.4.3", :git => "git://github.com/JonasNielsen/nifty-generators.git"
gem 'fastercsv'
end
gem "mocha", :group => :test
Do you think the error is because I left out the two gems with "#" ? Those were causing problems at first, and I don't think I need them to test some minor changes in the app (some views that I have to modify)
The issue is being described, and supposedly fixed here: https://github.com/collectiveidea/delayed_job/issuesearch?state=open&q=yaml#issue/194
Try this from the command line: irb -rubygems -r delayed_job and then from your bundled directory bundle-exec irb -rubygems -r delayed_job which will show if there is a difference between your system gems and your bundled setup - you might see an error in one or both attempts to run IRB.
If there is an error using bundle-exec but not with your system gems then it's a bundler issue. If not, are you sure the app is designed to function under Ruby 1.9? It looks like to_yaml isn't available at the point DJ is required, which implies it probably needs a require "yaml" somewhere.