Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Ruby Gem and Rails plugin?

As far as I know, the only difference is in "installation". I've also heard that plugins are somehow harder to update. But would I want to install something as a plugin instead of gem?

like image 219
Jakub Arnold Avatar asked Jan 23 '23 05:01

Jakub Arnold


2 Answers

Gems:

  • Can handle dependencies
  • Can be installed system-wide
  • Can also be vendored (in vendor/gems)
  • Has a concept of versions, can easily specify which version you want

Plugins:

  • Very easy to publish for authors

Apparently, the only advantage for plugins is a powerful advantage, since plugins are still very common. I tend to agree. Publishing a plugin = creating a github repository. Publishing a gem requires you to create a gemspec, and perhaps even meddle with The Gem Beast (rubyforge), which both are PITAs.

like image 83
August Lilleaas Avatar answered Jan 26 '23 06:01

August Lilleaas


I'd say this question is likely to be debatable to which is the best approach but my thoughts none the less..

The biggest win I would say in using a gem over a plugin if given the option are gem dependencies.

Rails plugins don't handle dependencies, so if you install a plugin that relies on other plugins/gems, you have the inconvenience of installing these additionally. Where as installing a gem can at the same time pull in any dependencies required for it to run. Take the cucumber gem for example:

'sudo gem install cucumber' will install the cucumber gem, but will also install its dependencies such as the webrat gem.

This may not seem like a big thing, but when you are dealing with multiple environments (development, staging, production etc.) you are more prone to encounter dependency issues.

The only scenarios where you are likely to want a plugin over a gem is when you conveniently want access to the plugin code. One case may be if the plugin is infact a Rails engine and you need to quickly dive into the code and see what class/view you want to override. Another case may be if you have forked a plugin such as active_merchant, and have it setup as a git submodule in vendor/plugins. Committing and pushing changes upstream tends to be alot more convenient in this scenario.

As for one being harder to update over the over, gems have rubygems to manage them which is straight-forward enough, and if you happen to be using git then submoduled plugins are a doddle to update:

# installing a plugin as a git submodule
git submodule add git://path_to_plugin_git_repo.git vendor/plugins/plugin_name

# updating said git submodule
cd vendor/plugins/plugin_name
git remote update
git rebase origin/master
cd ../../..
git add .
git commit -m "update plugin_name to latest master."

or the equivalent for SVN externals

Installing something as a gem or a plugin however shouldn't affect the functionality of the code as they are just two different ways of packaging code up, in the end it comes down to what you find more convenient to use I guess. Personally I find gems much easier to maintain unless I'm likely to be getting stuck into the code, then a submoduled plugin is more to my favour as it will show up in project tree in my editor.

like image 34
Mark Connell Avatar answered Jan 26 '23 06:01

Mark Connell