How to detect the rails version from an installed ruby gem - ruby-on-rails

I want to modify an existing gem to compatible with new rails versions. My requirement is as follows
I have a gem which runs in both rails 2 and 3. But it has some stylesheet copy functions. So as you already know rails 2 has stylesheets in public/stylesheets folder, But in rails3 has assert pipe line.
So what I want to do it, I want to detect the rails version using by my gem installed rails application and according to the version I will handle the file copy.
My question is How an installed ruby gem reads the current rails applications version ?
Or is there any other way to do it ?
thanks

I took a look around in the latest stable source for Rails and found a version.rb file which indicated I could do this:
Rails::VERSION::STRING #=> "3.2.6"
If you look at Rails 2.3, you’ll find that the same thing works there.

Related

Convert a Gem to a Plugin in Ruby

I currently have a gem installed on my development machine. Now that I want to use this gem on the production but I could not install it due to some issue with the server. Is there a way that I can convert this gem to a plugin so that I can transfer it to the server?
if no, is there a way to transfer this gem from my machine to the server?
BTW, I'm using an old version of Ruby on Rails (2.1)
Thanks.
Just copy the gem's single source code file into your project's lib directory. https://github.com/ambethia/smtp-tls/blob/master/lib/smtp-tls.rb
Well, if the plugin is compatible with Rails 2.1 you should be able to move the plugin to your /vendor directory, and it should work from there. As mentioned in the comments, the vendor directory is going to be removed and that style of plugins will be removed in Rails 4, but for Rails 2.1 that doesn't matter.
To get the files, go on rubygems.org and find the project homepage. You can probably find the source code for the plugin you're interested in (including a version that's compatible with Rails 2.x) on Github, then just clone it to your vendor directory.

creating a new rails project in rails 2.2.2 (while having rails3)

In my computer (linux - ubuntu) I have installed rails 2.2.2 and rails 3
Now i want to create a project in rails 2.2.2. (Like rails ). But it asks to enter the rails command as in rails 3 (like rails new )
when i type rails -v it shows the rails version as 3. But in my gem list it has both rails 2.2.2 and 3
how can i create a project in rails 2.2.2
thanks in advance
cheers
sameera
Generally speaking if you have two versions of ruby and rails installed the last one will be the "normal" one using "normal" commands like:
ruby
rails
gem
For all the other versions you can define (or have defined during installation) some aliases like:
ruby187
rails222
gem222
Another solution could be this
Or the best solution I recommend for working with multiple ruby and rails versions is RVM
You can do this. Create a rails project.
rails myproject
Go to your envirnonment file and modify to
RAILS_GEM_VERSION = '2.2.2' unless defined? RAILS_GEM_VERSION
EDIT
Yes , Sorry about that .This method may have worked in rails versions < 3. My Bad ! RVM the way to go.
Should do the trick. Also I recommend you update to atleast Rails 2.3.9, in case you don't want to start off with Rails 3.
There are 3 easiest ways how to manage it:
0) remove not used version of rails
1) rails is just simple command, so each time when you need to run special version of it you can specify full full path to it( or alias it one time)
2) if you strongly need to have different types/versions of ruby environments( and/or different versions of ruby gems such as rails), I recomend you to use rvm - it is the most easiest way to manage it.
When you want to specifically use one version of your installed version for a specific project use
rails _2.2.2_ (your project)

Multiple Rails versions for multiple projects

I'd like to use Rails 2.2.2 for one project and Rails 2.3.2 for another. Both are installed.
What is rails _2.2.2_ --version supposed to do? I've read that it makes 2.2.2 the working version -- that is, the version that will be used from that point on. But when I check rails --version, I get Rails 2.3.2. So, I also want to know what rails --version tells me; is it just the latest version of Rails that I have or is it the version that will be used for rakes?
I know about RVM. Is that the best way to use different versions of Rails on different projects?
Yes "rails --version" tells which is the latest version of rails. If you want to check different versions of rails installed in your machine you have to do "gem list" which gives the list of all the gems installed in your machine with their versions.For Ex:-
rack (1.0.0)
rails (2.3.4, 2.1.1)
rake (0.8.7)
Is that the best way to use different versions of Rails on different projects?
My point of view is that No it's not a good practise to use diiferent Rails version for the different projects. but sometime you used your old projects which is of an earlier version so you have no option but install that rails version. But while creating a new application you should used the latest stable rails version.
Just for record you can used different Rails version for different projects by mentioning Rails version in enviorment.rb For Ex:-
RAILS_GEM_VERSION = '2.1.1' unless defined? RAILS_GEM_VERSION
I would try RVM gemsets for this, because it's likely that you will need to keep separate sets of dependencies for each project as well as separate versions of Rails itself.
RVM seems generally a much tidier approach - using separate environments instead of a single pool of gems means that you can easily create or destroy environments without breaking others, and it reduces the risk of making a mistake when you specify the versions of dependencies.
In the Python world people use virtualenv to create completely separate environments for each of their projects, and I think that this is a good idea to copy.
In the config/environment.rb file look for RAILS_GEM_VERSION and set the value to the required version.
RAILS_GEM_VERSION = '2.2.2' unless defined? RAILS_GEM_VERSION
OR
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
I would use Bundler: http://gembundler.com/
It's pretty easy to get used to, and has other benefits. It's by some of the Rails core members, and I believe will become the recommended solution soon after Rails 3.0.
To answer your original question, the underscore in your version number are used to specify a version when you create a new rails app. For example if you were to create a new rails app, rails would use the latest version installed in your system by default, but what if you wanted to use an older version installed on your system?
Here is the command...
rails 2.3.4 killerapp
You will need to have rails gem 2.3.4 installed on your system!
For that you can use rvm and gemsets to group your ruby/gems in convenient packages.
I hope this helps

Managing many Ruby on Rails applications of different versions

I'm learning Ruby on Rails with the AWDR book and have had to be specific about which version of Rails and Ruby that I am running on my local machine. I have just discovered that I need to roll back from ruby 1.8.7 to ruby 1.8.6 here. I also needed to roll back Rails to support the scaffold method so I could start the tutorial easily.
My question is: When I start contracting, developing and deploying projects in the real world, how am I going to manage all these different versions?
It looks to me like Rail's low tolerance for legacy code negates its ease of use philosophy! But I'm sure I'll grow to appreciate RoR.
As for Rails, What you can do is freezing your version, for example:
Make sure to install the proper Rails version, suppose you want version 2.2.2 : gem install rails v=2.2.2
Freeze and pack Rails with the project itself : rake rails:freeze:edge RELEASE=2.2.2
Now you will find Rails packed inside the vendor folder of your project, so you don't have to install Rails on the deploying machine.
And for Ruby, I like Ruby Version Manager(RVM), the easiest way to manage Ruby versions.
RubyGems is Ruby's package manager. You can install as many versions of gems (packages) as you want. You can install the latest by running sudo gem install rails (at the moment it will install 2.3.5). If you need 2.2.2, specify that with the -v or --version option: sudo gem install rails --version 2.2.2. Rails also installs a binary (yes, I know it's not really a binary file), rails, which generates a project. Because you have several versions of the gem, you need to control which binary gets called. When you install the rails gem, RubyGems puts a file in it's bin/ dir, which is a "link" to the real rails binary. That is the one you "call" when you say rails on the command line. However, all of the rubygems "link" binaries accept a parameter of it's own, which is what version you want to use. You would use the 2.2.2 rails binary like this:
rails _2.2.2_ my_project
I think the default is to use the most recent version, so if you want to use the most recent version, do this:
rails myproject
However, I see that you use 2.2.2 to get access to the scaffold method. I would strongly suggest you not to use that method, there's a reason for removing it. The scaffold method hides code, and makes customization hard. Instead, use the scaffold generator:
./script/generate scaffold --help
Good luck on your future rails adventures!
The latest version of Agile Web is written for 2.2.2 I believe. For this basic app they walk you through I'm very certain it should work with 2.3.x
The answer to the question for how you keep up is that you update your apps as needed and read the api and Changleogs to find out what has changed and fix the stuff that upgrades break. A great way to help with this is having a good test suite with good test coverage.

How do I use an old version of Rails with Aptana RadRails?

I'm new to Rails development, and I'm trying to figure out how to use an older version of Rails with Apatana's RadRails IDE. I'm trying to help out a friend who has a site built on older version than the one that automatically gets downloaded by RadRails, and I'm pretty sure the two versions wouldn't be compatible (the site is using some pre 2.0 version, not sure of the exact number offhand).
Is there a way to tell RadRails to get and use a specific version of Rails? Or is there something I can do at the command line to change the installed version of Rails? I'm only vaguely familiar with the "gem" package system, but I'm assuming it would involve that.
Any help would be much appreciated!
Use the Rake task rails:freeze:gems in your rails project and give it the version you want to use. For example:
rake rails:freeze:gems VERSION=2.1.0
That will put the right version of Rails into vendor/rails, which is loaded by default if it exists.
If you don't want to freeze the gem into your project (using rake rails:freeze:gems), you can install the rails gem of the version you want to use:
gem install rails -v 2.0.2
and then specify the rails gem to use in your config/environment.rb:
RAILS_GEM_VERSION = '2.0.2'

Resources