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 ?
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 ?
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).
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
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
.
So when will you use one or the other:
Here is an archived tutorial for creating an engine..
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.
Engines are very related to plugins. Engines can be plugins and plugins can be engines. All of them can be created using rails plugin
generator with 2 different options --full
or --mountable
.
I think the main different here between Engines and Gems.
Gems is just a bit of code providing a set of functionalities to anyone who integrates it in its code.
It contains:
Can be packaged and pushed to RubyGems servers
Engines are actually gems. All engines can be gems (if packaged) but not all gems are engines.
We can say it with a different word, Engines is a Ruby on Rails feature, that can contain Rails-specific entities: models, controllers, views, migrations.
It needs to be integrated inside Rails application and can't run on their own.
Very good and quick read Artricle