Zobi helps you to orchestrate your controller behaviors using the following gems :
How to use it?
Add it in your
Gemfile and run
gem 'zobi', '~> 4.0.2' # Rails 4 gem 'zobi', '~> 5.0.0.rc1' # Rails 5
Next, include Zobi module in your controller and set modules you want to include :
extend behaviors :inherited, :scoped, :included, :paginated, :controlled_access, :decorated
This module historically use inherited_resources gem, which is now deprecated.
Now it only defines create/update/destroy methods to easily build CRUD backend.
If your controller is namespaced, you should define the
method to override its generic behavior.
If your model is namespaced, you should define the zobi_resource_class:
def zobi_resource_class ::User::Address` end
This module also deals with Strong Parameters using Parameters classes.
A Parameters inherits from Zobi::ParametersSanitizer, and should define the list of parameters and nested parameters to accept.
Here is an example :
module User class AddressParameters < :: # Optional, usefull for a namespaced controller, User::AddressesController # here. def resource_type :user_address end protected def fields [ :id, :street, :zip_code, nested_attributes: [:id, :foo, :bar] ] end end end
By default, Parameters class are discovered using the controller namespace.
User::AddressesController, Zobi will search for
If you don't want to use Parameters class, you can define the
in your controller and perform custom filtering.
This module uses has_scope gem.
This module only works with ActiveRecord because it uses the Eager Loading Associations of Active Record.
You just need to define a method named includes and add the associations to load.
def includes [:association] end
This module uses kaminari gem.
This module uses draper gem and has a dependency on Inherited modules for now.
By default, Zobi will try to discover the decorator class to use using the current namespece.
For example, given a controller named Admin::User::AddressesController, Zobi will try to find the appropriate decorator class in this order :
Admin::User::AddressesDecorator Admin::AddressesDecorator AdressesDecorator Admin::User::CollectionDecorator Admin::CollectionDecorator CollectionDecorator
Admin::User::AddressDecorator Admin::AddressDecorator AdressDecorator Admin::User::ResourceDecorator Admin::ResourceDecorator ResourceDecorator
If this is not the way you organize your decorators, you can override this
behavior by defining a method called
decorator_class which returns the decorator class to use.
Launch test suite :
cd spec/dummy bundle exec rake db:test:prepare cd ../.. bundle exec rspec
Launch the dummy app :
cd spec/dummy bundle exec rake db:migrate bundle exec rails s
We decided to follow rails versions to simplify update of Zobi when you want to upgrade rails.
Copyright (c) 2013 af83
Released under the MIT license