Zobi helps you to orchestrate your controller behaviors using the following gems :
How to use it?
Add it in your Gemfile
and run bundle install
:
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 Zobi
behaviors :inherited, :scoped, :included, :paginated, :controlled_access, :decorated
Available modules
Inherited
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 zobi_resource_name
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 < Zobi::ParametersSanitizer
# 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.
Given a User::AddressesController
, Zobi will search for
User::AddressParameters
class.
If you don't want to use Parameters class, you can define the permitted_params
in your controller and perform custom filtering.
Scoped
This module uses has_scope gem.
Included
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
Paginated
This module uses kaminari gem.
Controlled Access
This module uses pundit and devise gems.
Decorated
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 :
Collection :
Admin::User::AddressesDecorator
Admin::AddressesDecorator
AdressesDecorator
Admin::User::CollectionDecorator
Admin::CollectionDecorator
CollectionDecorator
Resource :
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 collection_decorator_class
or
decorator_class
which returns the decorator class to use.
Developing
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
Versioning
We decided to follow rails versions to simplify update of Zobi when you want to upgrade rails.
Credits
Copyright (c) 2013 af83
Released under the MIT license