I created a gem which defines a Rails::Railtie.
Everything works as expected, except that I have to include the main file of the gem (lib/gem.rb) in my application.rb so it is executed.
Is there any way to do this automatically?
I checked some gems, but I could not figure out how.
Solved it.
The name of my gem was slightly different from the files' name (gem-name vs lib/gem_name.rb), so I have to put this to my Gemfile:
gem 'gem-name', require: 'gem_name'
Related
I want to use Coveralls.io for my gem Headhunter that I'm developing at the moment. The doc says, I should simply add
gem 'coveralls', require: false
to the project, but as far as I know, this isn't the right way to load gems within another gem. Instead, stuff like that should happen in the .gemspec file. So I tried to add it like this:
s.add_development_dependency('coveralls', '>= 2.0')
But this doesn't work - it breaks my gem's whole functionality:
$ rake
/Users/josh/.rvm/rubies/ruby-2.0.0-p353/bin/ruby -S rspec ./spec/headhunter/css_hunter_spec.rb ./spec/headhunter/css_validator_spec.rb ./spec/headhunter/html_validator_spec.rb
/Users/josh/Documents/Work/MuheimWebdesign/headhunter/lib/headhunter/css_validator.rb:6:in `<class:CssValidator>': undefined method `full_gem_path' for nil:NilClass (NoMethodError)
This is the file that breaks:
require 'net/http'
require 'nokogiri/xml'
module Headhunter
class CssValidator
VALIDATOR_PATH = Gem.loaded_specs['headhunter'].full_gem_path + '/lib/css-validator/'
So Gem.loaded_specs['headhunter'] isn't available anymore, no idea what's going on here.
What's wrong here?
I was wondering the same and I just got it working.
You need to add:
spec.add_development_dependency "coveralls", "0.7.0"
to your .gemspec (0.7.0 is the coveralls gem latest version as the time of writing this)
make sure to run bundle installsuccessfully
and add:
require 'coveralls'
Coveralls.wear!
to the beginning of your spec_helper.rb or test_helper.rb, before requiring anything else.
Hope this helps.
In my gemfile I have things like:
gem 'net-sftp', '2.1.1', :require => 'net/sftp'
gem 'backup', '3.0.27'
gem 'watu_table_builder', :require => 'table_builder'
gem 'browser', '0.1.6'
When in a Gemfile do you need a require? I've also found things like :require => false. Help?
If you omit the :require option, by default Bundler will attempt to require the gem by using the standard name-to-file conversion rule:
dashes are considered namespace separators and underscore classname separators
It means that the following gem statements
gem 'net-sftp'
gem 'backup'
gem 'foo_bar'
are equivalent to
gem 'net-sftp', require: 'net/sftp'
gem 'backup', require: 'backup'
gem 'foo_bar', require: 'foo_bar'
This works well if the gem author has followed the standard conventions. But in some cases, for a variety of reasons, this doesn't happen.
For instance, there are gems called foo-bar where the main filename is /foo_bar.rb or even /foo.rb. In this case you provide the :require option to tell Bundler which file you want to require.
Finally, require: false is used when you want a gem to be part of the bundle, but you don't want Bundler to load it by default.
This is useful, for instance, to lazy-load a gem in case it is used only in certain circumstances. Think about a rake task that includes a heavy gem. You don't want your application to load it on boot, but it needs to be part of the bundle or it will not be found.
In this case you pass the option require: false. Then, in your rake task you will require it manually as usual
require 'library'
The library will be loaded only when the task is invoked, not in the normal application execution.
A good example is whenever. The library must be part of the bundler because it must be bundled when you deploy the app, but it is intended to be run as a command line script. For this reason, you don't want Bundler to require it when the Rails application is started.
There are cases where you use groups instead of require: false.
See also the official Bundler documentation for require.
You need a :require if the name of the file that's required to activate the gem can't be inferred from the gem's name. Ruby convention is to name your gem the same thing as the "require" statement you should use, but not everything follows the convention.
:require => false disables automatic requiring by Bundler, so you'd need to use require 'foo' manually to use the code in foo. This is mainly useful if a gem is large or expensive to activate and only used in certain cases.
This is often used if the name of the library to require is different than the name of the gem. In your case it would download the gem 'watu_table_builder', but would call 'table_builder'.
require specifies other files that would be required on requiring this particular gem.
Documentation says
Each gem MAY specify files that should be used when autorequiring via
Bundler.require. You may pass an array with multiple files, or false
to prevent any file from being autorequired.
Refer to this for whole documentation
I'm using ckeditor_rails (here) but when i uploaded my app to heroku, all my editors wasn't 'ck'ed (lol)
so I started my app (unicorn_rails -E production) and got the same result: even locally, the editors wasn't showing. if I start in development, they work pretty well...
here are my Gemfile (part of)
gem 'mongoid_slug'
# gem 'will_paginate'
gem 'will_paginate_mongoid'
gem 'bootstrap-will_paginate'
gem 'ckeditor_rails', :require => 'ckeditor-rails'
I tried rake tmp:clearand rake assets:precompiled and tried delete the manifest.yml too. none of these commands worked.
am I missing something?
thanks in advance
Did you include the ckeditor-jquery
Make sure that your javascript and css files are included and precompiled
I'm building a gem locally, suppose it name is "MyGem".
Now suppose that MyGem depends on other already built gem, for example "cancan".
So, i've added in my mygem.gemspec the line:
add_runtime_dependency("cancan")
Here is the problem: if I installl MyGem into a new project by adding it into my gemfile like this:
gem "mygem", :path => "path/to/my/local/gem"
then this new project is not being able to use cancan methods, and I have to explicity declare cancan on the new project gemfile in order to use it.
I tried also using gemspec method, but didn't solve my problem either.
Any ideas?
Update
I just wanted to add that when i only have myGem declared in my new application gemfile, after I run bundle install all the dependencies are installed.
That is, if i run gem list the "cancan" gem is displayed, but I still can't access it methods from the application level.
Thanks for the help.
Ok, i've solved this. I'm not sure if this is the best solution but it did work.
Making the application level developer to explicity add the dependencies in his gemfile didn't make any sense. So, as the gems did were being installed, i just required all the gem dependencies in my my_engine.rb file inside my gem.
Following the example, in my my_engine.rb I added the following line
require "cancan"
And that'it...
Even better you could do:
autoload :CanCan, "cancan"
So the module would be loaded only when it is called.
And even better than that, you could load only the file from cancan that you are using (maybe you don't need to load all of it).
You can add that line in you my_gem.rb file or your engine.rb file if you are using engines.
That worked for me, I hope this help someone.
Use add_dependency instead of add_runtime_dependency (this may help with cancan) then run bundle update on your new project.
Does the "cancan" gem name actually match the name to be require'd?
Example for yajl-ruby gem:
Gem::Specification.new do |s|
s.name = %q{yajl-ruby}
...
But when require'ing the library, you'd use a different string:
require 'yajl'
That means in your Gemfile, you have to explicitly require the dependency (which you said you wanted to avoid).
gem 'yajl-ruby', '>=1.0', :require => 'yajl'
To avoid needing to do this, and if you're the author of the "cancan" dependency, you should make the gem name matches the require name.
The only alternative I can think of is require'ing the dependency directly in one of your source files (like you did in your solution).
I just created a new gem (using bundler) and want to add Active Record support. So I added s.add_dependency "activerecord", "~> 3.0" to my gemspec. Then I use Bundler.setup and Bundler.require and thought that I have access to Active Record now, but I haven't. I have to explicitly use require "active_record". Any idea why Bundler.require does not work for me in that case?
Firstly, if you're packaging a gem, do not use Bundler.require. Bundler.require is for apps not gems.
In .gemspec, specify the dependencies of your deployed gem.
In your Gemfile, include the line gemspec to automatically include the dependencies listed in your .gemspec in your Gemfile.
You may also optionally create gem groups for dev and test.
In your code, explicitly require any libraries you need.
I lost a couple of hours on this today so I hope this helps.
(Sources 1, 2)
Secondly, though the ActiveRecord gem is called "activerecord", the lib is called "active_record". This is what you would need in Gemfile.
gem 'activerecord', :require => "active_record"
Unless you include the :require option, ActiveRecord won't be loaded correctly and you won't know about it until you try to use it.
If you want use Bundler you need define your Gemfile with Activerecord
gem 'activerecord', "~> 3.0.0"
Or you need define bundler to use your gemspec with adding gemspec in your Gemfile
gemspec
See http://gembundler.com/rubygems.html
I had this problem, and the issue in my case was that I was naming a directory in my gem active record, as in:
lib ->
active_record ->
base.rb <- containing some monkey patches to base
This was causing mass confusion including sweet error messages like:
Gem Load Error is: uninitialized constant ActiveRecord::Base
Did you mean? ActiveRecord::Base
Simply moving changing the file from lib/active_record/base.rb to lib/active_record_base.rb fixed it for me.