Release Zobi: a gem to keep Rails controllers DRY
Zobi is a gem that helps mix several
functionalities in a Rails controller.
In many projects at af83, we use similar gems, mainly for back-office
interfaces.
So, we decided to extract these common behaviors into a gem to ensure the
following points:
- keep controllers DRY;
- don't waste time reinventing the wheel;
- apply our best practices;
- ensure uniformity between all af83's projects.
To achieve this, Zobi uses these popular Rails gems:
Usage
Add Zobi to your Gemfile
and run bundle install
:
<span class="n">gem</span> <span class="s1">'zobi'</span><span class="p">,</span> <span class="s1">'~> 4.0.0.rc2'</span>
Next, include the Zobi module in your controller and set the modules you want to
include :
<span class="kp">extend</span> <span class="no">Zobi</span><span class="n">behaviors</span> <span class="ss">:inherited</span><span class="p">,</span> <span class="ss">:scoped</span><span class="p">,</span> <span class="ss">:included</span><span class="p">,</span> <span class="ss">:paginated</span><span class="p">,</span> <span class="ss">:controlled_access</span><span class="p">,</span> <span class="ss">:decorated</span>
Full documentation is available on the
github page.
Included behaviors
Let's explain the purpose of each of Zobi's behaviors.
Inherited
Helps you create CRUD very easily using
the power of inherited_resource gem.
It also provides a mechanism to extract
Strong Parameters filtering
logic in custom classes which makes testing more simple than testing the
controller itself.
Scoped
Out of the box filters for your controllers indexes. You only have to declare
your filters using the has_scope declaration and your collection will be filtered
automatically.
Paginated
Automatic pagination for your collections using the kaminari gem.
Decorated
By using draper decorators, you'll have 3 main advantages:
- views don't handle complex logic;
- decorators are simple to test;
- decorators behaviors can be shared between several views.
Included
This module only works with ActiveRecord because it uses the
Eager Loading Associations of Active Record.
It lets you fetch object relations using as few queries as possible.
Versionning
We decided to start the versioning at 4.0.0 to follow the Rails versions
because of the many dependencies between Zobi and Rails.
If you want to test Zobi in a real Rails app, you can use the dummy app
available here: https://github.com/AF83/zobi/tree/master/spec/dummy.