Gem Vs Plugin Vs Engine in Ruby on Rails

15,353

Solution 1

Plugins as you knew them from Rails 2 (i.e. plugins under the vendor/plugins folder) were deprecated for Rails 3.2; support for it was completely removed in Rails 4. Now, there's a concept of a "gemified plugin" where the plugins are essentially built as gems, and can be shared across different Rails applications.

But to answer your question about gems vs plugins, check out this Stackoverflow answer. Long story short, plugins from the Rails 2 universe is an extension of the rails application, whereas a gem is a packaged ruby application.

As for Rails engines, I've found this to be a pretty easy and intuitive definition of a Rails engine:

Rails Engines is basically a whole Rails app that lives in the container of another one. Put another way, as the docs note: an app itself is basically just an engine at the root level. Over the years, we’ve seen sen engines as parts of gems such as devise or rails_admin. These example show the power of engines by providing a large set of relatively self-contained functionality “mounted” into an app.

And since both rails engines and plugins are types of ruby applications, they can all technically be packaged and used as a gem (usually).

Solution 2

There are no more plugins since Rails 4. Rails 4.0 release notes:

Rails::Plugin has gone. Instead of adding plugins to vendor/plugins use gems or bundler with path or git dependencies.

Any engine can be contained in a gem. Gem is just an alias to a 'library'.


Best way to see what their difference is, is generating three of them and looking through their directory structure:

bundle gem a_gem, use for non-rails-specific functionality.

rails plugin new b_railtie, use for rails extensions that don't require full application-like setup. but, since it's still a rails-specific setup (you get your Rails dummy app in /test eg), you are probably going to use railtie in it. railtie is a class that inherits from Rails::Railtie, and gives you the comfortable DSL to hook up your code into Rails. eg, if you want some action performed :before some Rails app initialization step, you can use initializer Railtie class_method. Paperclip

rails plugin new c_engine --full, use for rails extensions that will be full-fledged app themselves, mounted into your app. will give you /app dir and Engine subclass besides basic non---full setup.

rails plugin new c_engine --mountable, same as --full, but will create namespaces, ready to be mounted into your app engine. Spree

And here is a pretty good link: http://hawkins.io/2012/03/defining_plugins_gems_railties_and_engines.

Solution 3

Answer quoted from Difference between plugins and Ruby gems?

Gem
  • Gem is a packaged ruby application using the packaging system defined by RubyGems.
  • Rails itself is a Gem.

    Rails gem is installed in jruby-1.0\lib\ruby\gems\1.8\gems\rails-1.2.3 as:

    DIR bin
    DIR builtin
    68,465 CHANGELOG
    DIR configs
    DIR dispatches
    DIR doc
    DIR environments
    307 fresh_rakefile
    DIR helpers
    DIR html
    DIR lib
    1,072 MIT-LICENSE
    11,969 Rakefile
    8,001 README
    The lib directory contains all the gem source code.

  • We can install,upgrade and query the gem version.If one uses a tool like my GemInstaller, one can easily automate the installation and loading of RubyGems with a single simple config file.

  • Gem installed for Ruby interpreter can be used system-wide by that interpreter.
  • Gem may be published as a plugin.
  • Can also be vendored in vendor/gems.

Plugin

  • Plugin is an extension of Rails Framework.
  • Can not be upgraded by using a command. To upgrade one have to uninstall and then install upgraded version.
  • Has to be hooked into rails application. (has to have init.rb)
  • Have an install.rb file.
  • Plugin cannot be published as a Gem.
  • Can only be used application wide.

Goldspike plugin is installed in vendor\plugins\rails-integration directory of the application as:
7,089 build.xml
1,141 LICENSE.txt
DIR plugins
6,675 pom.xml
1,447 README
DIR samples
plugins/goldspike directory consists of
24 init.rb
25 install.rb
DIR lib
549 Rakefile
536 README
DIR tasks
DIR test
The lib directory contains all the plugin source code.

Gem vs Plugins

  • Rails had a way of loading plugins from the vendor/plugins/ directory. This will most likely deprecate as Rails has added support for bundling gems with the project in the vendor/gems/ directory. The gem versions of rspec are the ones that are intended for everyday use. One should go with those unless you are supporting a Rails application in the 1.2.x family or earlier.
  • It often becomes quicker to check-in and check-out of a repository using Gems as you are not including the library in your actual application. There are often lesser problems using Plugins related to incompatibility arising concerning software versions among the distributed team.
  • General rule of thumb is to make Rails-specific functionality a plugin while making more general Ruby libraries into gems.

Engine

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 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.

Here is an archived tutorial for creating an engine..

Share:
15,353
Amrit Dhungana
Author by

Amrit Dhungana

Updated on June 02, 2022

Comments

  • Amrit Dhungana
    Amrit Dhungana almost 2 years

    What is difference between Gem package, plugin and Engine in Ruby on Rails ?

    I think we use plugin before Rails3.2 and after rails3.2 is release we are using gem package as plugin but how can we use engine in ROR ?