I am using Ruby on Rails 3.2.2. I have implemented a Something plugin (it is almost a gem, but is not a gem) and all related files are in the lib/something directory.
What steps should I accomplish to "convert" the Something plugin to a Ruby gem?
How to use the new implemented Something gem in my application without to make it public (that is, without put that on a public server)?
http://guides.rubygems.org/make-your-own-gem/
Previous link covers that as well - gem install something-1.2.3.gem
Related
I've got some helper code (monkey patches) I tend to carry around from rails project to rails project via simply copying files or pasting code into the initializers folder.
I think a clean way to deploy different categories of my initalizer code would be to simply add gems to my project Gemfile that would do the equivalent of this for me. I'm not very skilled at creating gems or the deeper side of ruby on rails engineering so I just wanted to know if this were possible before I got started with it.
Yes, this is definitely possible. It should be probably enough to make a simple gem, put your monkey-patches there and require them in the gem entry point (e.g., if the gem is called foobar, then bundler will require lib/foobar.rb by default).
There is a guide on creating gems available in Bundler docs. In case you need to interact with Rails application configuration, you can take a look at Rails Engines.
I want to learn how to create a rubygem with a generator etc.
When I create a gem, do I create a separate project?
The reason being, since it will have generators and hook into the lifecycle of a Rails 3 application, I want to create a test rails 3 application at the same time to see how things are working etc.
Can someone outline what I should do to do this?
For example, I'm using git, so what I want to do is, when I run a generator for my gem and it doesn't do what I want, I can then easily rollback to the previous version using git.
This will be a simple gem, just trying to get a feel for things and how I can hook into various parts of rails etc.
Use bundler to create the new Gem with the following command:
bundle gem foogem
Then go to the Gemfile of the application you're going to use and add something like the following:
gem 'foogem', :path =>'/path/to/foogem'
In this way you can easily test your gem inside your rails project.
What is the difference between the two and when one should be used instead of the other?
An Engine in rails terminology is a actually a subapplication of a web-application. For instance, something like a blog, a forum, or simple authentication: these are not full-blown applications, but pages/views/controllers/models that can be added to any rails application.
In rails2 this would be done using a plugin. Now since rails3 an engine can be packaged in a gem.
A gem is a ruby library, which can be found on http://rubygems.org and it is the standard (only) way to package and distribute ruby code to other rubyists.
So to conclude:
A gem: is a generic library, which can be easily installed, which are version-managed, have dependencies and such.
An engine: is a sub-application of a Rails application, and since Rails 3 these are distributed as a gem (which is awesome!).
So when will you use one or the other:
create a gem if you want to share ruby-functionality
create an engine (and package it in a gem) if you have parts of your rails application that can be used more generally.
Hope this helps.
As a Rails developer I feel a bit stupid asking this question but hopefully I will learn something new and someone can put me out of my misery! In my rails applications I use (other peoples) gems all the time, I also use plugins from the community or my own.
I understand the benefits of use gems over plugins as they are version-able, segmented, system wide, easier to manage and share etc etc but I don't really know how to go about making a gem for my rails apps!?
Do you always start with a plugin and convert it to a gem, I've seen the words 'package it as Gem'. Also the gem I'm thinking of building would be no good in a normal ruby program, it's only useful to rails apps. I'm not even sure if the semantics of that make sense, 'RubyGem' that will only work in a rails application!?
I would like to create a gem (if that's what I should use?) for a discrete piece of functionality for my rails apps. It will need to add a database migration, new routes and provide controllers and views or useful view helpers. I'm know I can achieve this via a plug-in but would just like to know how/why to do it as a 'Ruby Gem'?
To avoid the risk of Over-engineering, I usually start with the feature I need directly into the application. Then, as soon as I need to use the same feature into another project, I check whether it is worth to extract it into a plugin or even a separate application providing an API.
Plugins and Gems are often interchangeable. Gems provides several significant advantages in terms of reusability and maintainability.
On the other side, there are some specific known issue. For instance, a Rails app actually can't load rake tasks defined into a plugin packaged as a Gem.
Almost every Rails plugin can be packaged as a Gem.
For instance, take my tabs_on_rails plugin.
You can install it as a Gem specifying the dependency on environment.rb. Or you can use script/plugin install command as you would expect.
If you want to achieve the same result, make sure to follow the standard Gem layout and provide the init.rb initialization script required by Rails.
Also, you might want to create an install.rb and uninstall.rb file to include the post-install and post-uninstall hooks when the plugin is installed as a standard Rails plugin.
Last but not least, if you package a plugin as Gem you can reuse it in non-Rails projects and provide Rails-specific initializations using the init.rb file. Non-Rails applications will simply ignore it.
If you want to make a plugin for Rails, https://peepcode.com/products/rails-2-plugin-patterns gives you a good start. After that, make the plugin into a gem.
To make a gem, this resource http://railscasts.com/episodes/183-gemcutter-jeweler will be helpful.
As of 2013 you'll want to use Bundler and the following tutorials:
#245 New Gem with Bundler -
RailsCasts
Make your own gem - RubyGems
Guides
Take a look at Jeweler. Jeweler gives you a set of rake tasks to make gem versioning and building very easy.
I have developed a simple library in Ruby and need to use this in several Rails applications (some of which are not built yet). What is the best way to easily add this Ruby library to several Rails applications as and when required? Our team is running Ubuntu and our repository is Mercurial.
Should I use a...
Rails plugin? This would be my first
choice but it appears not to support
Mercurial??
Ruby Gem?
Custom Rake script?
Other options??
Any pointers would be much appreciated!
Ruby already has an established mechanism for code sharing i.e. RubyGems. Jeweler makes Gem creation easy. I'd recommend that you check it out.
Make a gem or a plugin. Gems are better in my opinion, easier to manage.
My rule of thumb:
If it doesn't depend on rails, make it a gem.
If it depends on rails, make it a plugin.
Make a Rails plugin. It doesn't "support" Mercurial in the sense that you can't do script/plugin install $REPO_URL and have it work automatically, but if it's for your own use, then you won't miss that feature.