Install a gem locally for development - ruby-on-rails

I'm new to ruby (many years of Python experience) and want to install this gem locally to replace the existing one which I installed using gem install swagger-docs. I have tried everything but cannot get rails to pick up my local version.

If you want your Gemfile to use a local gem, use path like this:
gem "swagger-docs", :path => "/Users/name/my_swagger_fork"
If you want to use a git remote, you can do this:
gem "swagger-docs", :git => "git://github.com/user/swagger.git", :branch => "my-awesome-branch"

Related

How to make edits to a gem and have that take affect on heroku?

I have a gem to which I want to make a few edits.
I have opened the gem using
bundle open gem_name
Made the edits and they work fine on development.
What do I need to do in order to make the changes take affect on heroku.
There is nothing to commit locally.
Your local gem is modified, but during deployment the one on github gets installed.
What you could do is to find the gem project on github, and fork it.
Then you can apply all your custom mods and commit into your gem repo.
Once you're done, remember to point to the right repo in your Gemfile:
gem your_gem, , :git => "https://github.com/your_user_name/your_gem.git"
You can vendorize the gem like this:-
gem unpack my_private_gem --target vendor/gems
and then in your Gemfile
gem 'my_private_gem', :path => "vendor/gems/my_private_gem-VERSION"

Gemfile gem installation and gemspec dependencies

I have an app whose Gemfile requires a gem that is also dependent on another gem that is currently found on github.
So
app/Gemfile reads
gem "my-gem", :git => "git://github.com/MyCompany/my-gem.git"
my-gem/Gemfile reads
gem "my-gem-2", :git => "git#github.com:MyCompany/my-gem-2.git"
my-gem/my-gem.gemspec reads
spec.add_dependency "my-gem-2"
When I run bundle inside of app I get an error that it can't find gem my-gem-2 which is required by my-gem; however, if I put the following line
gem "my-gem-2", :git => "git#github.com:MyCompany/my-gem-2.git"
inside of app/Gemfile then it works fine.
This practice seems redundant as I wouldn't think I'd have to add gem dependencies of another gem into my parent app. Is there something I'm doing wrong here that myapp can't find my-gem-2?
This is just the way it goes - Gemfile dependencies within gems are just for when you're developing that gem. Only the gemspec gets evaluated when the gem is used elsewhere (and gemspecs have no concept of git dependencies), hence only dependencies in the gemspec apply.
So: you will need to have both git references within your app's Gemfile.
As stated in the gem specification, the list of gems that you provide through add_dependency will be use to make sure those are already installed in the system during the installation process (i.e gem install). So this line:
my-gem/my-gem.gemspec reads spec.add_dependency "my-gem-2"
Will trigger the verification of whether or not the gem is installed in the system, but it will not trigger any automatic installation of such gem, as Bundler would do.
This other line (inside of your gem):
gem "my-gem-2", :git => "git#github.com:MyCompany/my-gem-2.git"
Specify that a gem should come from a git repository with a .gemspec at its root.
For more details: Gems from git repositories

What's the proper way to install rails after cloning from github?

I cloned rails 4 from github and want to install rails from the local repo. What's the best way to do the installation? I ran install.rb starting from a fresh gemset but it failed because of missing gems. Anyone know a post that walks through this? Should I do the install from the remote github repo first and then run the install.rb file?
Also, just want to be clear here. I am not trying to just install using gem install or using :git in my gem file. Please do not put those instructions in your comments. I absolutely want to install from a local clone, as a first step toward fixing Rails when I encounter bugs not fixed.
Bundle the local gem.
gem "rails", :path => "~/your/rails/checkout"
gem "activerecord", :path => "~/your/rails/checkout"
etc...

How can I specify a local gem in my Gemfile?

I'd like Bundler to load a local gem. Is there an option for that? Or do I have to move the gem folder into the .bundle directory?
I believe you can do this:
gem "foo", path: "/path/to/foo"
In addition to specifying the path (as Jimmy mentioned) you can also force Bundler to use a local gem for your environment only by using the following configuration option:
$ bundle config set local.GEM_NAME /path/to/local/git/repository
This is extremely helpful if you're developing two gems or a gem and a rails app side-by-side.
Note though, that this only works when you're already using git for your dependency, for example:
# In Gemfile
gem 'rack', :github => 'rack/rack', :branch => 'master'
# In your terminal
$ bundle config set local.rack ~/Work/git/rack
As seen on the docs.
You can also reference a local gem with git if you happen to be working on it.
gem 'foo',
:git => '/Path/to/local/git/repo',
:branch => 'my-feature-branch'
Then, if it changes I run
bundle exec gem uninstall foo
bundle update foo
But I am not sure everyone needs to run these two steps.
In order to use local gem repository in a Rails project, follow the steps below:
Check if your gem folder is a git repository (the command is executed in the gem folder)
git rev-parse --is-inside-work-tree
Getting repository path (the command is executed in the gem folder)
git rev-parse --show-toplevel
Setting up a local override for the rails application
bundle config local.GEM_NAME /path/to/local/git/repository
where GEM_NAME is the name of your gem and /path/to/local/git/repository is the output of the command in point 2
In your application Gemfile add the following line:
gem 'GEM_NAME', :github => 'GEM_NAME/GEM_NAME', :branch => 'master'
Running bundle install should give something like this:
Using GEM_NAME (0.0.1) from git://github.com/GEM_NAME/GEM_NAME.git (at /path/to/local/git/repository)
where GEM_NAME is the name of your gem and /path/to/local/git/repository from point 2
Finally, run bundle list, not gem list and you should see something like this:
GEM_NAME (0.0.1 5a68b88)
where GEM_NAME is the name of your gem
A few important cases I am observing using:
Rails 4.0.2
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]
Ubuntu 13.10
RubyMine 6.0.3
It seems RubyMine is not showing local gems as an external library. More information about the bug can be found here and here
When I am changing something in the local gem, in order to be loaded in the rails application I should stop/start the rails server
If I am changing the version of the gem, stopping/starting the Rails server gives me an error. In order to fix it, I am specifying the gem version in the rails application Gemfile like this:
gem 'GEM_NAME', '0.0.2', :github => 'GEM_NAME/GEM_NAME', :branch => 'master'
You can reference gems with source:
source: 'https://source.com', git repository (:github => 'git/url')
and with local path
:path => '.../path/gem_name'.
You can learn more about [Gemfiles and how to use them]
(https://kolosek.com/rails-bundle-install-and-gemfile) in this article.
If you want the branch too:
gem 'foo', path: "point/to/your/path", branch: "branch-name"

How to install Rails 3 master from GitHub

I'm using rvm (Ruby Version Manager) and running Rails 3 RC. I need to test an app to see if a bug has been resolved with a recent commit to Rails 3 master on GitHub.
How do I install Rails 3 master from GitHub and then generate a new app?
I can't use
gem install rails --pre
because I want the edge version, not the release candidate.
Can you suggest a helpful gist or blog post?
All I could find was this:
http://weblog.rubyonrails.org/2010/1/1/getting-a-new-app-running-on-edge
and it is out-of-date.
Thanks!
You can create an empty folder, then put a Gemfile inside, with this:
source 'http://rubygems.org'
gem 'rails', :git => 'git://github.com/rails/rails.git'
Then inside the folder, run:
bundle install
Inside the folder again, run:
bundle exec rails new /path/to_my_new_application/appname
And that's it.
You can do this with your rails 3 app Gemfile. Bundler is able to install directly from github and if you dont specify a branch or tag then it will use master. Add this to your Gemfile after you generate your rails 3 app, and then run bundle install and start up your app. After you bundle install it will show you the commit number in Gemfile.lock.. it should be the latest commit number from the master rails repo. Here is what I do in my Gemfile:
gem 'rails', :git => 'git://github.com/rails/rails.git'
#gem 'rails', '3.0.0.rc'
I just uncomment and comment these 2 lines to switch b/w RC and master... and bundle install.
Alternately, you can clone the repo and then use your local source in the Gemfile:
I think it should look something like this (untested):
gem 'rails', :require => 'rails', :path => "/path_to/rails"
Why not take a look through the commit log here: http://github.com/rails/rails/commits/master before cloning the repository? I don't know what bug you are looking for but remember, you can also look at the "diffs" from each commit to see what has changed.

Resources