Increase your Rails development speed using patterns that will make your code even more easy to read and maintain.
Add this line to your application's Gemfile:
gem 'zertico'
And then execute:
$ bundle
Or install it yourself as:
$ gem install zertico
It is deprecated. Please use Zertico::Delegator
instead.
The Zertico::Controller
define behavior of a common Rails Controller. By Extending it, your controllers will be more smart,
they will know which model instantiate and where to redirect when needed.
All you need to do is extend ith with you ApplicationController
and you will get the benefit of it.
class ApplicationController < ZerticoController
respond_to :html
end
After that all your controllers will look like this:
class CarsController < ApplicationController
end
The Zertico::Delegator
is a delegator with some extra tools to work with ActiveRecord
. It will try to guess your model,
and initialize it. It can be used as a Decorator.
class UserDelegator < Zertico::Delegator
def name
interface.name.downcase
end
end
user = UserDelegator.find(2)
puts user.interface.name
puts user.name
# 'User Name'
# 'user name'
The Zertico::Interactor
defines a single call on a transaction at the ruby interpreter level. It can be used to define a
database call, api call, sending of an email or calculate some data based on another interactor.
class CreateUserInteractor < Zertico::Interactor
def perform(params)
@user = User.create(params)
end
def rollback
@user.destroy
end
end
It should define its perform
logic and rollback
logic in case some other interactor fails.
The Zertico::Organizer
is responsible for calling a pack of interactors, and in case of some failure, send a
rollback signal for all other interactors already executed.
module CreateProduct
extend Zertico::Organizer
organize [ CreateProductInteractor, CreateInvoiceInteractor ]
end
In this example, it something goes wrong with the Invoice Creation, it will rollback the Product Creation.
Zertico::Responder
its a custom Rails Responder with pjax support and an
option to force a redirect no matter what.
class ApplicationResponder < ActionController::Responder
# custom responder behavior implemented by [responders](https://github.com/plataformatec/responders)
include Responders::FlashResponder
include Responders::HttpCacheResponder
include Responders::CollectionResponder
# add this line to get the Zertico::Responder behavior
include Zertico::Responder
end
You will also need to define your custom Responder inside your ApplicationController:
class ApplicationController < ActionController::Base
self.responder = ApplicationResponder
respond_to :html
end
To force a redirect do as follows:
UsersController < ApplicationController
def create
@user = User.create(params[:user])
respond_with(@user, force_redirect: true) # It will redirect even on failure
end
end
When using Zertico::Controller
your controllers will try to find a service with the same name as your controller.
If it can't find, it will initialize and Zertico::Service
class. Creating the service, you can define which class to use on that controller.
class AdminController < ApplicationController
end
class AdminService < Zertico::Service
use_interface User
# or
use_model User
use_as_variable_name 'epic_user'
end
In the example above, the controller will use the model User instead of the model Admin he would have guessed.
Its is a good idea to separate each of the patterns you use in his own folder. If you choose to use all patterns here, you would have this:
app/
controllers/
delegators/
interactors/
organizers/
responders/
services/
The Interactor
and Organizer
idea was taken from interactor by
collectiveidea.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request