Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gem Vs Plugin Vs Engine in Ruby on Rails

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 ?

like image 614
Amrit Dhungana Avatar asked Apr 16 '14 19:04

Amrit Dhungana


People also ask

What is difference between GEM and plugin Rails?

A Rails plugin is either an extension or a modification of the core framework. From Rubygems.org: A gem is a packaged Ruby application or library. So, the biggest difference between the 2 is that Rails plugins are specifically made for use within Ruby on Rails applications, whereas gems aren't.

What is a plugin in Ruby on Rails?

A Rails plugin is either an extension or a modification of the core framework. Plugins provide: A way for developers to share bleeding-edge ideas without hurting the stable code base. A segmented architecture so that units of code can be fixed or updated on their own release schedule.

What is GEM Ruby on Rails?

Gems in Rails are libraries that allow any Ruby on Rails developer to add functionalities without writing code. You can also call Ruby on Rails gems as plugins for adding features. A Ruby gem enables adding features without creating the code again and again.


2 Answers

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

like image 165
richsinn Avatar answered Oct 05 '22 16:10

richsinn


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.

like image 42
lakesare Avatar answered Oct 05 '22 15:10

lakesare