custom gem with rails 3. No such file to load - ruby-on-rails

I created a custom gem called pdf2html. The gem file is pdf2html-0.1.gem
I placed this file in the vendor directory of rails 3 project.
My Gemfile entry for this gems reads as follows
gem 'pdf2html', '0.1' , :path => 'vendor'
When I run the bundle install command I get the following message regarding this gem
* pdf2html at `vendor` will not be cached.
I tried doing a bundle show on this gem it tells me that it is installed in the vendor directory.
Now when I do a rails console and try to do a require 'pdf2html' I get a "No such file to load error"/
Can someone tell me what I am doing wrong
Thanks
Paul

I thought the proper way to declare gems was to specify the full path, not the base path, as in:
gem 'pdf2html', '0.1', :path => 'vendor/pdf2html'
The reason it doesn't error out sooner is that the path vendor/ actually exists so there's no immediate problem. It's only when you try to require vendor/pdf2html.rb, which is missing, that there's an issue raised.

I pushed the gem and associated files over to github and then installed it from there using the Gemfile/ bundle install. Now its working fine. I could not figure out how to use the local gem file

Related

I'm developing a Ruby gem, how can I try it out locally with my Rails app?

I am developing a gem meant to be used with Rails projects and want to try it out locally with another Rails app of mine.
I built the gem with bundle exec rake release which put a .gem file in the /pkg directory.
Then, in my Rails app, I added the following to my gemfile
gem 'mygem', '0.1.1', path: '/Users/me/projects/mygem/ruby/pkg'
I then ran bundle install which said it installed the gem. When I do this, it removes the gem from the path. IDK where it went.
When I start the Rails app, it's like the gem isn't included at all.
Interestingly, if I add a version that doesn't even exist, it still says bundle install works fine. (Example: gem 'mygem', '0.1.2345', path: '/Users/me/projects/mygem/ruby/pkg')
What am I supposed to do to try out my Gem locally with a Rails app?
This question is different from How can I specify a local gem in my Gemfile? because I explicitly tell bundle in my Gemfile to use the local gem, with the path given, and it still doesn't work. When I run bundle install, it says
Using mygem 0.1.1 from source at /Users/me/projects/mygem/pkg
So you'd think it works right, but it still doesn't.
Interestly, if I try it with a version number that doesn't exist, like mygem 1.2.3, it still runs bundle install successfully, which is really weird and seems like a bug:
Using mygem 1.2.3 (was 0.1.1) from source at /Users/me/projects/mygem/pkg
I prefer to use the following when working on a local gem side-by-side with a Rails project:
gem 'foo',
:git => '/path/to/local/git/repo',
:branch => 'my-fancy-feature-branch'

Where are the gem files located?

Where are the gem files located ?
I'm new to rails and trying o understand how the whole gem functionality works.
My question is how can i follow a gem installation in order to confirm a gem is been installed ?
Where are the installed files located ?
From within your rails app, you can list out all of the gems being used, their versions, and the local path:
bundle show --paths
There's no reason to modify any of these files though. Configuration is typically done through an initializer in /app/initializers, but it depends on the gem being used.
If you need to modify something about the gem, you should fork it on Github and then reference the git location in your Gemfile until your pull request makes it back into the gem:
gem 'some_gem', '4.1.1', git: 'https://github.com/some_github_repo/some_gem.git'

Loading local gems through Bundler and mounted apps

I'm creating a gem (let's call it mygem) that is essentially a Sinatra server intended to be mounted within Rack based apps.
Inside my gem's gemspec file, I have the following:
gem.add_dependency 'kss'
And Inside my gem's Gemfile, I have the following
source 'https://rubygems.org'
gemspec
gem "kss", :path => "/Users/me/code/kss"
Now when running the server from within mygem's folder, this works exactly as expected: instead of fetching out for the kss dependency, it will look on my local drive and load that version.
The problem comes in when I add mygem to a Rails test app Gemfile. In my Rails test app Gemfile, I have the following line:
gem "mygem", :path => "/Users/me/code/mygem"
I would expect, upon a bundle install, that Bundler would load mygem and its dependencies; but for the kss dependency, instead of loading the local dependency, Bundler actually does fetches out to rubygems to find and load it. I'm assuming because in this case, it's only reading from the gemspec line and not including my dependency override.
Is there anything I can do to fix this behavior? I'd very much like to be able to run and test this stuff locally, but Bundler doesn't seem to recognize dependency overrides from a higher level app.
I'm completely open to any suggestions or changes if I'm going about this the wrong way.
Dependencies listed in your gemspec will become dependencies of the implementing application, while dependencies defined in the Gemfile will not become dependencies of the implementing application. I think you should be able to simply adjust the Rails test app Gemfile to be:
gem "kss", :path => "/Users/me/code/kss"
gem "mygem", :path => "/Users/me/code/mygem"

Ruby Gem works when pointing to local gem but not once published

Here is the situation:
I decided to start moving all the js code that I reuse in every project in a gem and created the gem in:
/Users/alain/Dropbox/rails_app/alain_toolbox
So in my project, to test if things are working, I add this line to the gem file:
gem 'alain_toolbox', '=0.0.1', path:'/Users/alain/Dropbox/rails_app/alain_toolbox
everything works fine.
Then, I do a
gem build alain_toolbox.gemspec
gem push alain_toolbox-0.0.1.gem
and everything seem to work. So I replace the gem require in the gemfile with:
gem 'alain_toolbox'
then do a bundle and the gem install successfully. The problem is when I open up the app, I get an error that it cant find 'alain_toolbox' in the line
<%= javascript_include_tag "application" %>
and application.js looks like this:
//= require alain_toolbox
which was working when I was in local.
So the question is: why?(!)
Thx.
Found the error: I dont know how, but I forgot to do a 'git add' on the asset files. Once the file were added and the gem rebuilt, everything worked as expected.
The Rails Guide indicates the gem needs to implement a rails engine. In addition, the assets would still need to be in one of the specified directories within the gem (app/assets, lib/assets, or vendor/assets). I'm assuming in the local case somehow your gem is already in that path.
According to this AsciiCast you can see the asset pipeline paths in the rails console by printing the Rails.application.config.assets.paths configuratio
Related question: Get Gem vendor files in asset pipeline path

How do I edit a gem and then hard link it to a project?

So I have a gem in /vendor/bundle that needed to be fixed, and when I edit one of the files and then run bundle install it links up properly and all is good. But the problem is that when I deploy my project, the edits to my gem are lost because it runs bundle and saves the gems in a separate shared directory.
It seems that the solution to this is to make a vendor/static_gems directory, and place the gem inside of there, and link it like:
gem 'ruby-mysql', :path => "vendor/static_gems/ruby-mysql-2.9.3"
But I get an error that the gem is not found. is there something wrong with my path? Or something wrong with what I am placing there? When I copy this gem from the gems/ folder all that is in there is a lib/ directory with some .rb files.
Most probably you don't deploy your Gemfile.lock with your project, so Bundler runs without (or more probably) with an outdated one that links to the other gem path.
Be sure to update the Gemfile.lock with the rest of your project and you shouldn't need to worry about a static gem path or something like that.
I think I had the right idea, but I wasn't putting the whole gem into the static_gem folder, just the lib directory. It works great when the whole gem is in there.

Resources