The Basics of Creating Rails Plugins

The Basics of Creating Rails Plugins Programming is a bit like being an adrenaline junkie for adventure sports. Once you have experienced snorkelling, you want to go for deep sea diving. You are always up for scaling high. In fact, it won’t be an exaggeration to say that if the same problems had to be solved day by day, the programmer’s world would have a dearth of ingenious programmers. Instead, programmers are always up for new challenges, for being able to apply existing solutions to the encountered problems without wasting too much time. Especially when it comes to Ruby on Rails where agility is the norm and convention over configuration the benchmark, using existing solutions for problems is taken for granted. The written codes represent abstract solutions to a problem and thus can be reapplied by refining or reusing it in various different context. Through the reuse, a programmer can save time and efforts which could be focused on creating newer interesting codes. After all, new applications cannot be created just by reinventing the wheel but by coming up with new solutions. Ruby on Rails is really powerful but eventually you may find Ruby lacking a particular functionality or wishing for a particular feature in course of your work. After all, no programming language is perfect and Ruby core team cannot add whatever everyone wishes for. This is where plugins come into play. Rails 1.0 introduced a plugin system to make Rails a really flexible extension system letting programmers develop plugins which could be used to extend or override parts of Rails framework and share these features and modifications with other programmers in a reusable format. A plugin is a great way to package commonly applied solutions to general problems and reusing the codes. Plugins makes it really easy and feasible to extract codes and share them as self-contained unit which is easy to maintain and share. Plugins aren’t just convenient for sharing new features and applications, they could be used to test the alteration to the Rails framework. And of course, with Rails it is not difficult to become plugin author. After the plugin has been created, it could easily be hosted on serves (free or paid) and distributed to the Rails community. In fact, Rails core team recommend that all popular plugins be published as RubyGems. The first step before you can create plugin is to understand whether your plugin is specific to just the application you are working on or whether it could be used across applications. If the plugin which you are creating is application specific, it would be considered as a vendored plugins (vendor specific plugin). If your plugin could be applied across applications, it would be considered as gemified plugin (gem plugin). Setup Generating a vendored plugin : In order to create an application specific plugin, you could use Rails generate plugin command in Rails root directory to create new plugin that will sit in the vendor/plugin directory. Check out the help directory to learn more on creating vendor specific plugin. Generating a gemified plugin : Writing gem specific Rails plugin can let you share the created plugins across different rails applications using Bundler and RubyGems. Check out the help directory to learn more on creating gem plugin. Testing your newly generated plugin : You can navigate to the directory that contains the plugin, run the bundle install command and run the generated test using the rake command. You should see: This tells that everything was generated properly and you can start adding functionality. Extending Core Classes : This section explains how to add a method to String that will be available anywhere in your rails application. In this example you will add a method to String named to_squawk. To begin, create a new test file with a few assertions: Run rake to run the test. This test should fail because we haven’t implemented the to_squawkmethod: Great – now you are ready to start development. Then in lib/yaffle.rb require lib/core_ext: Get in touch with us.

Subscribe For Latest Updates

Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *