Reputation: 4485
What is the difference between Gem package, plugin, and Engine in Ruby on Rails?
I think we use the plugin before Rails3.2 and after rails3.2 is released we are using the gem package as a plugin but how can we use the engine in ROR?
Upvotes: 42
Views: 17718
Reputation: 79
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 difference here is between Engines and Gems.
Gems is just a bit of code providing a set of functionalities to anyone who integrates it into 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, and migrations.
It needs to be integrated inside Rails application and can't run on its own.
Very good and quick read Artricle
Upvotes: 1
Reputation: 11202
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 bundlers 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 by generating three of them and looking through their directory structure:
bundle gem a_gem
, used for non-rails-specific functionality.
rails plugin new b_railtie
, used for rails extensions that don't require a 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.
Upvotes: 19
Reputation: 1331
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 "gamified 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 examples 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).
Upvotes: 21