Overview of A Rails Plugin
Author: ceefour | Filed under: Beginner, Plugins, Rails, Tips, Tools, TutorialsIf you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
A common stumbling block for beginner Rails developers is learning the basics required to write plugins. This is made more complicated by the fact that Ruby is inherently dynamic and offers many techniques for code reuse.
Luckily, if you can write Rails applications you can write plugins by simply drawing on a handful of basic patterns.
Why write plugins?
Writing a plugin will:
- Help make sharing code more efficient, whether it’s between projects or within the same project
- Allow you to publish generic code to the community
- Save time and increase your confidence by testing once and reusing many times
- Share functionality in a robust manner, especially when using namespaces with ActiveRecord
Usage
Rails provides installation scripts through script/plugin install, and a generator for creating new plugins: script/generate plugin. These will work with URLs, saving time when trying out plugins. You can read more about installing and managing plugins at the Rails wiki.
Rubyisms
Any of the following tools and techniques provided by Ruby are used by plugins to extend functionality:
- Mixins: including or extending classes using modules
- Opening a class or module definition and adding or overriding methods
- Dynamic extension through callbacks and hooks: method_missing, Class#inherited, Module#const_missing, Module#included
- Dynamic extension through code generation: eval, class_eval, instance_eval
These techniques fall into two broad categories:
- Using modules and classes to extend existing classes, providing new features
- Using introspection to adapt generic code to specific cases
It’s important to consider exactly what should be extended when writing a plugin. If complex meta-programming to adapt your plugin to the host application is required, care should be taken to ensure concurrency will not produce unexpected results.
Hopefully this very short summary clears things a bit. Read more on Alex Young’s article.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.