Where are the gem files located? - ruby-on-rails

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'

Related

How to rename ruby gem after build is made?

I am trying to push into rubygems.orge a simple gem following this tutorial. Basically I am using bundler and I have write a simple Hello World class. Then, I try to push the gem as follows:
bundle gem my_first_gem
gem build my_first_gem-0.0.1.gem
and I get:
Signed in.
Pushing gem to https://rubygems.org...
Repushing of gem versions is not allowed.
Please use `gem yank` to remove bad gem releases.
So, I have checked and there is already a gem with such name. So, is there an easy way to rename the gem I have including changing the gem name in all generate by bulder files:
or if I should rename the files by hand, could you tell which are the critical ones?
Instead of renaming files by hand. As it is just a tutorial gem, I would suggest you to create a new gem with
bundle gem gotqn_first_gem
and just move your HelloWorld class in lib. And follow the rest of the commands suggested in Railscasts.
Don't forget that after renaming you need to call git add -A to update your files list.
The reason for that is because (unless that you have changed) your my_first_gem.gemspec have a line like that:
spec.files = `git ls-files -z`.split("\x0")
So, when you call gem build my_first_gem-0.0.1.gem, the above command will search for your older files and ignore the renamed ones.

Use gem as library in Rails 4

Is there a way to use a gem as a library in Rails 4?
I have tried putting in a gem folder after cloning into lib folder but this doesn't seem to be working
You can set local path to gem in your Gemfile if I clearly understood the problem.
# Gemfile
gem 'my_perfect_gem', path: './path/to/my_perfect_gem'
I think it's better to set local gem location only in development and test environments, so wrap this line in a group. Unfortunately you should restart your rails server any time you've updated the gem.
May be there is a better approach such as using your gem as a part of application in lib folder – Auto-loading lib files in Rails 4

Rails Engine gem using another Rails engine gem from git

Rails 4 # Ruby 2
So i have two engine (A, B) gems that cannot be uploaded to Rubgems because the code is private, so im using git.
A must use B, so basically i should add B as a dependency in A gemspec. As far as i know, gemspec doesn't allow git based gems as a dependency inside gemspec.
I tried to use A gemfile but no luck. Is there a way out?
For development or test, you can do it in Gemfile. You can even point it to use your local folder.
gem 'gem_b', path: "/path/to/the/folder"
or to use a git reference
gem 'gem_b', git: "git#github.com:user/gem_b.git"
This will allow you to do the development and testing.
Another option is this. If you have 2 engines, I assume you also have a Rails app to use them. In that rails app, you can simply require them in Gemfile one after another using git reference.
gem 'gem_b', git: "git#github.com:user/gem_b.git"
gem 'gem_a', git: "git#github.com:user/gem_a.git"
If you must declare this dependency in gemspec, you can build the gem using rake;
cd /path/to/gem_b
rake build
This will create a gem_b.gem file under pgk folder.
Then you can install it in your local using gem instal like this
gem install pgk/gem_b.gem
Now you have gem_b installed in our local. There will be no problem adding it as a dependency in gemspec.
When you are ready to release your application, you need to copy gem_b.gem to your server and install using usual way;
gem install /full/path/to/gem_b.gem

Whats the best way to tweak ruby gems for code reading

To help understand the source code of various gems I often want to place various puts statements in the source code or even try using the ruby debugger.
But whats the best way of doing this?
Do you clone the project from github and make changes locally, if so how do you "force" the use of the local cloned code over the local gem on your machine. Do I just create some scripts that explicitly require the path of the cloned repos folder?
Or do should I use rvm to create a temp gemset, download the gem and modify it directly?
Are there any other methods ive overlooked? How would this change for gems designed for use within rails projects.
The way I usually do it when I want to make changes to a Gem:
Fork the repository on Github
Check it out and create a new branch for local changes
Use Bundler to manage dependencies for the project which uses the Gem
Change one line in the Gemfile to make it use the forked version of the Gem:
gem "thegem", :git => "git://github.com/name/thegem.git", :branch => 'mybranch'
or
gem "thegem", :git => "file:///path/to/thegem", :branch => 'mybranch'
with /path/to/thegem being the path to your local working copy.
The advantage is that you now already have a the perfect infrastructure set up for contributing your changes through a pull request :)
With Bundler.
In a Rails app simply edit the Gemfile and add:
gem "gem_name", :path => "~/MyGems/gem_name"
PS: Bundler work with any Ruby project.
You can use rvm to create a temp gemset, download the gem and modify it directly. A fast way to view/modify a gem is using gemedit :
Install:
gem install gemedit
Usage:
gem edit devise
or: gem edit devise -e mate

How to find out what fork/original of a gem is installed on my system?

When I add gem 'delayed_job' to my gemfile, how do I know whether I am going to get
collectiveidea / delayed_job or tobi / delayed_job ?
Also, is there some way to check among the list of gems I already have installed, as to which fork/location those gems were downloaded/installed from?
Ps. I am using RVM on Ubuntu, Bundler and Rails 3.0.3
There isn't one way to tell which github fork or branch you are downloading from. For the delayed_job gem you are downloading from collectiveidea's branch. You can tell on this page where the homepage points to collectiveidea's github fork. The reason you can't tell which fork in particular is because rubygems aren't linked to github repositories. They are simply packages that are uploaded to the site. For all you know you could be downloading a gem from a copy of someone's local repository that isn't even published on the internet. You could also be downloading from an SVN repository instead of a Git repository. In general the rubygems.org site should give you some idea of how to find the source code for a gem though. Also, most github gems tag their commits with a version number so that you can tell which revision you are using by checking the github/git tags.
If you want to specify git location you can use :git param:
gem "delayed_job", :git => "git://github.com/collectiveidea/delayed_job.git"
gem "delayed_job", :git => "git://github.com/tobi/delayed_job.git"
Read more about Gemfile
The gemspec contains a homepage attribute, which often shows the source code repository. You can view a gemspec of a locally installed gem with:
gem spec delayed_job
View the homepage with:
gem spec delayed_job | grep homepage
That said, the gemspec does not always have the source repo.
To help solve this problem (and others), I wrote a gem called gemdiff. It does the gemspec inspection, and if that does not contain a github URL, it searches github for a match. It includes exceptions for gems like delayed_job, which is a fork of the original repository by tobi.
gem install gemdiff
gemdiff find delayed_job
=> http://github.com/collectiveidea/delayed_job
More valuably, gemdiff will inspect your project's bundle and can show you the source code diff between the version of a gem you have installed and the highest version that can be installed, as determined by bundler.
https://github.com/teeparham/gemdiff

Resources