Starting Ruby Gem versioning within a Rails application - ruby-on-rails

I have a Rails application that is pointing to a gem which is on github (not yet pushed to Ruby Gems) and it is not yet versioned so when I run bundle it looks like this:
gem (0.0.0) from git#github.com:company/gem.git (at master)
I have been working on changes to the gem and want to begin versioning it so when I push to master it doesn't break functionality in the Rails application that relies on the previous gem. I am not sophisticated enough to make the gem backwards compatible so I was thinking versioning would be the best way to handle this problem.
I am looking for advice on how to begin versioning this gem, is it simply changing the gemspec to 0.0.1 and then changing the gemfile in my Rails application to point to that?

As per semantic versioning, you should start off at 0.1.0 and increment the MAJOR version for any updates that are not backward compatible, the MINOR version if it only introduces new features but is backward compatible, and the TINY version if it's a bug fix or the like.
That said, if you are hosting via GitHub only, you can simply change the gemspec version to your preference as well as add a tag to that commit. Adding a tag makes it easy to find the correct version in GitHub after you've moved on to other versions - if using Bundler, for instance, your Gemfile entry would look like this:
gem 'your-gem', git: 'git://github.com/your-repo/your-gem.git', tag: 'v0.1.0'
You could also use the following (lengthy) set of commands to do this without bundler:
git clone git://github.com/your-repo/your-gem.git
cd your-gem
git checkout v0.1.0
gem build your-gem.gemspec
gem install your-gem-0.1.0.gem
If you intend to host via http://rubygems.org, you'll need to make yourself an account and then push your new version up to there. Rubygems will detect the version from the gemspec and provide it using the more standard Gemfile entry format:
gem 'your-gem', '=0.1.0'
Or using the gem command:
gem install your-gem -v '=0.1.0'

Related

How to build a gem from rails source code?

I want to make a gem from rails source code and install the gem.
After clone the master repository of rails, I tried as follow.
$>gem build rails.gemspec
$>gem install rails-4.2.0.alpha.gem
It did not work. I also tried $>rake install which did not work either.
Looking forward your help!
There is a script in the root directory which builds from source and installs the gems. It's called install.rb. You can use it by running the following:
ruby install.rb 4.2.0.alpha
Note: At the time of writing this the arel gem needs to be built from source and installed separately before running the install script above. This is because the version constraints in rails are requiring a version of arel which has not yet been released onto rubygems.org.

What steps do I take to add my newly created Gem? And how can I access it?

I just created this standard skeleton of a gem.
In it I'm extending Ruby with C. Using this tutorial, I produced a very basic gem that performs a simple hello_world method.
But I'm not sure how to incorporate this into an app at this point.
Does anyone know what steps I need to take in order to install this gem in my existing Rails app? Then, once it is installed (I imagine with Bundler), how would one access the method of hello_world from within the Ruby environment?
I noticed your repository is missing the .gemspec file. Gemspecs define a "gem" and allow it to be installed and published using Rubygems. This guide to creating a gem, on rubygems.org, explains what a .gemspec file is and how to create it.
Once you have a gemspec file in your repo, you can publish it to rubygems (so it can be installed on any computer), or use it directly from your filesystem. To include a gem from a local directory in a gemfile, the include line looks like this:
gem 'aes_gem', path: '/path/to/aes_gem'
Or if you just want to install it using gem install:
gem install /path/to/aes_gem.0.0.0.gem
You can use bundler to install local gems and use them.
This can be achieved by
gem "foo", :path => "/path/to/foo"
in your case
gem 'aes_gem', path: '/path/to/aes_gem'
Once you complete your gem, you can push into github or rubygems and specify the path accordingly

2 gems need different versions of the same dependency

Using Rails 3, I'm trying to figure out what I think should be pretty straightforward...
I have 2 gems that require 2 different versions of the same gem dependency. Both versions of the dependent gem are installed on my system but I still get an error from Rails: "Bundler could not find compatible versions for gem XXX".
What is the best practice to handle a scenario like this?
I'd go for what #BaroqueBobcat suggests. I just want to add that - if you need the latest Twitter gem and can't wait for the maintainer of Groupon2 to update his gem - you can fork the Groupon2 on GitHub, update its gemspec, see if it still works by running its tests (and try to fix it if it doesn't) and include your own version using its Git URL in your Gemfile like so: gem "groupon2", :git => "https://github.com/yourgithubuser/groupon2.git".
If you want to be nice you can offer your changes to the maintainer of Groupon2 with a pull request for bonus points :)
If you don't need all the features of the Twitter gem version 1.4.1, you could use version 1.2.0, which needs faraday ~> 0.5.4. and that should work. If that doesn't, you could try
poking the owner of groupon2 to update his gem--it's on github https://github.com/gangster/groupon2
.
I was having the same issue, but in a different context: Writing an app which uses two different versions of the hashie dependency (1.2.0 and 3.1.0)
I went into the Gemfile.lock and specified the older version in parentheses hashie (1.2.0), ran bundle install, and it worked.
If you're in a situation where the gems are being used in different projects or at least not at the exact same time you can use RVM's gemset feature as a workaround. I recently had a gem incompatibility similar to yours and that's what I used.
If you have RVM installed, do this:
rvm gemset create gemset_name_here
rvm gemset use gemset_name_here
So what you're doing is creating a gem environment that's totally fresh and from scratch while still being able to revert back to the gems you were working with before at any time. The first line creates a new gemset and the second line tells RVM to start using it.
At this point you'll need to run bundle install or rake or whatever you're using to get the gems you need but this should take care of the problem.
So when you're using gem 1 with dependency 1 you use the gemset that has the required version. Then when you're using gem 2 with dependency 2 you switch to the gemset that has that.
Now, if both gems are part of one larger project this will not be feasible and you'll most likely need to edit the source to of the gem to run the new version of the dependency like #BaroqueBobcat said. In a lot of cases this is actually pretty easy. Ruby developers tend to be very awesome about making their code easy to pick up on.
Take a look at this solution, maybe it will help you: gems
bundle update resolve conflicts

In Ruby on Rails, what's the difference between installing something as a gem or as a plugin?

On http://github.com/collectiveidea/delayed_job
it says:
To install as a gem, add the following to config/environment.rb:
config.gem 'delayed_job'
Run rake gems:install
versus
To install as a plugin:
script/plugin install git://github.com/collectiveidea/delayed_job.git
What is the difference between installing it as a gem or as a plugin?
Also, the first method just install gem 2.0.3 which might be the tobi's version? (rake gems:install installs the version by gem list -r delayed_job) Is it http://github.com/tobi/delayed_job ?
The "plugin" method specifically says it is the collectiveidea version? Doesn't it matter which one you install?
Both the Gem and the vendored plugin refers to the collectiveidea's fork. In fact, collectiveidea is the current maintainer for the delayed_job Gem on RubyGems.
That said, generally speaking installing a plugins as a Gem has many advantages.
You can install it once and use it in many different projects
You can take advantage of dependency resolution
You can upgrade just changing version number
You don't need to store the entire plugin code in your SCM
So, why you can install a plugin "as a plugin"?
There are many different answers.
At the very beginning, Rails plugins came as simple libraries. Time passes and developers started to notice the advantage of packaging plugins as Gem.
Also, before Rails 3, some plugin features were only reserved to plugins and not to Gems. For instance, before Rails 3, plugins could bundle rake tasks while there wasn't a way to inject new rake tasks or new routes into the main application.
In the last two years, the most part of Rails plugins offers the ability to be installed as a plugin or as a Gem. With Rails 3 and the arrival of Bundler, I'm sure plugins are going to be deprecated in favor of Gems.
That are 2 different repositorys,
maybe you shoult try
config.gem 'delayed_job', :source => http://github.com/collectiveidea/delayed_job.git
Look at: http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies
Btw. maybe you want to look at a maybe better solution: resque - see http://ruby-toolbox.com/categories/queueing.html for a comparison of used queing gems
When you install a gem it will be available for all apps, in case you use a plugin - just for an app it's installed into.
The basic difference is a gem is something that gets installed on the system running your Rails application, whereas a plugin is installed along with your application, plugin does not get installed on the system level.
suppose you are using rvm and let us take this example.
we have two applications app1 and app2
both are running on a common rvm gemset named gemset1
when you add a gem in the gemfile of app1 and run bundle install and then being in the same rvm gemset which is gemset1, if you run the second app app2 the gem will be available in the second application as well
Whereas with the plugin it will not be the case because plugins get installed on application level and not at the system level

How to install a forked gem on Engine Yard?

I want to use the LinkedIn gem But not the one that I get when I type
sudo gem install linkedin
I want a specific one that somehow has done patches to. It is a fork of the original which is:
http://github.com/jbasdf/linkedin
I have downloaded sources from the above link, and use "rake" command to build a gem locally. So everything is working fine locally.
But now I have a question. How can I setup this folked gem on the server (engine yard)? I am not sure how to bild a gem on the server in this case.
Many thanks!
If the pre-built gem is available on a server(gem cutter) you should be able to use Trip's method.
If not two options spring to my mind:
1) Write a chef script that clones the source from github and builds it.
2) Use bundler with its built in support for building gems from a git url. EY fully supports bundler.
I think the latter would be a better choice, as it completely removes the need to use EY's UI for managing gems and is a step towards Rails 3 compatibility.
You can log into EY's cpanel. On your dashboard, Click "Application" in your instance, and then click on the Ruby Gem Icon. In this panel you can download gems to install on your server including what version you want them as well.

Resources