Tila
Tila is a set of modules that help you create simple CRUD controllers in Rails with very little code.
Most Rails projects contain a lot of controllers that are quite repetitive, because those controllers essentially implement what is needed to get a simple CRUD interface. This project contains components that can be used to build the controller of this simple CRUD interface.
Because Tila favors composition over inheritance, is allows for a flexibility that most other projects in this space don't have. If you only want to use a part of Tila, that works perfectly fine.
Installation
Include Tila in your project by adding it to your Gemfile
.
gem 'tila'
Then run
bundle install
to make sure Tila is installed.
Usage
After Tila is installed in your project, you can use it like this.
class BunniesController < ApplicationController
include Tila::Resourceful
protected
def permitted_resource_params
resource_params.permit(:name, :fluffiness)
end
end
This controllers will implement the basic CRUD actions (index
, show
, edit
,
update
, new
and create
) for the Bunny
model. You only need to specify
the permitted_resource_params
method to make sure only the expected parameters
are passed into the resource. And you'll need to write your own views. Tila will
automatically infer from the name of the controller class that this is a
controller for the Bunny
model and will make sure you can use @bunnies
in
the index
view and @bunny
in the other views.
Components
Tila favors composition of inheritance, so it contains a lot of small modules
that can be used indepently. The example above only shows the result of
including the Tila::Resourceful
module, but you can choose to only include
small parts of Tila. Below you can find a list of the components of Tila, with
a short explanation of what they do and which other components they depend on.
-
Actionable: Provides an
action
helper so we can simply find out which action is called and contains a list of actions that are operated on collection and which actions are actions that save the model instance. It also provides simple helpers to find out if the current action is in one of the lists. -
Modelable: Provides an
model
helper so we can access the model that the controller is for, and a few helpers to access model name. - Messages: Provide a simple helper for generating message strings from I18n. Requires Modelable.
-
Objects: Provides a simple accessor for
@object
and@objects
. The ResourceLoaders component registers the loaded resources here. - Params: Provides a way to update the attributes of the object and simple starting point for filtering out the permitted resource params. Requires Modelable, Objects.
-
SaveDestroy: Provides the
save_object
anddestroy_object
methods. Requires Objects. -
FormHandler: Provides methods to handle the submit of forms and the
destroy. Requires Messages, Objects, SaveDestroy. You need to define a
location_after_save
and alocation_after_destroy
method, if you want to use this module in isolation. The Resourceful component defines these methods for you. - ResourceLoaders: Provides methods for loading the resources. Requires Modelable and Objects.
- ResourcefulUrls: Generates the URLs to the controller. Requires Modelable.
- Resourceful: Implements the actual controller actions, registers the before_action to load the resources. This is the part that you want to include when you want to enjoy the full glory of Tila. Requires Actionable, Params, FormHandler, ResourceLoaders and ResourcefulUrls.