Kriangle is a library (gem) built upon ruby to scaffold the Modules (Model, Controller, Serialiser, APIs and much more) in rails project.
Any modules can be consists of below components.
- Controller (APIs)
- Swagger Docs
- much more ...
Kriangle can scaffold any module easily by using it’s Generators. You can control the module generation with skip options. Also you can choose the options as per your requirements.
- Getting Started
- Initial Setup
- Module Generator
- Controller Actions
- Skip Options
- Advanced Options
- APIs Routes
- Parent Reference
- Self Reference
- Code of Conduct
Kriangle works with Rails 5.1 onwards (< 6). Add the following line to your Gemfile:
Or you can use the master branch code as gem. Add the following line to your Gemfile:
gem 'kriangle', git: 'email@example.com:lalitlogical/kriangle.git', branch: 'master'
Kriangle dependent on Devise gem for its authentication module. So we have to generate its intialiser file by installing it.
bundle exec rails g devise:install
Next, you need to run the generators:
Kriangle provides two generator for this purpose:
- Initial Setup - Install the Kriangle and it's dependencies into the existing project.
- Module Generator - Scaffold new modules into the existing project.
Always remember the hierarchy of modules. So if any module behave as child of any other module then parent module should be generated before the child module. So when you generate the modules, it should follow the hierarchy. i.e. if you enable the counter cache on
Post model for it's owners,
User module must created before
Post module. So hierarchy should be like
Like, etc for a blog rails application.
In intial setup, it's create the intialiser files, authentication module (login, register, forgot password, etc), swagger setup, etc. Swagger help to create the APIs docs. This command can be run single time per project and always before module generator.
In the following command you will replace
MODEL with the class name used for the application’s users. This will create a model (if one does not exist) and configure it with the authentication module. The generator also configures your config/routes.rb file to point to the authentications controller.This command can be run single time per project and always before module generator.
rails g kriangle:install MODEL PATH [column_name:type]
i.e. if you want to generate the User model with Authentication model (for authentication purpose), so you can type below command.
rails g kriangle:install User Auth first_name last_name gender age
password, other columns and authetication related table will be added automatically.
In above command we have setup the initial authetication module. We can generate the new modules with below command. You have to take care of sequence of options as below.
rails g kriangle:module MODULE [association type] [column_name:type] [controller actions] [skip options]
if you want to generate
Post model with title, content columns, you can type below.
rails g kriangle:module Post title:string content:text
By default, generated the model does not refered to current logged in user. You can enable it by passing reference=true and other arguments as below.
rails g kriangle:module Post title:text:false:_cont_any content:text:false:_cont_any published:boolean:false::false index show create update destroy --reference=true --reference_name=current_user --association_type=has_many --skip_tips=true --creation_method=new
If you want to enable counter cache on user's record, you should use below command.
rails g kriangle:module Post title:text:false:_cont_any content:text:false:_cont_any published:boolean:false::false index show create update destroy --reference=true --reference_name=current_user --association_type=has_many --counter_cache=true --skip_tips=true --creation_method=new
Kriangle support different arguments to control the code generation.
If you have rails knowledge, you are already aware about the associations (has_many, belongs_to, etc). You can control this with Generate New module command. This options support below items in same sequence.
Let understand the every options and its supported values. By default false or nil depedents on arguments.
||any previously created model name in lower case||True|
||custom foreign key|
||any previously created model name|
By default rails support two options into migration. But we have enhance it into Kriangle. It supports as below.
column name:type:validate_presence:search_by:default value
Let understand the every arguments and its supported values. By default false or nil depedents on arguments.
|column name||any valid column name for your model||True|
|deafult value||pass the default value for columns. i.e.
By default, all CRUD APIs generated. But if you want to control it, you have to mentioned the required action. It will generate only those APIs.
Valid actions mentioned as below.
- create_or_destroy - Will update the controller's action according to like dislike feature respective to authenticator model (i.e. User).
There are a lot of skip options available. You can check below.
||Skip the swagger documentation. Only valid with
||Skip the user's avatar feature. Only valid with
||Skip the model creation if model aleady created and you do not want to override it.|
||Skip the controller (APIs) generation.|
||Skip the table migration if table aleady created and you do not want to override it.|
||Skip the tips or comments into files.|
||Skip the authentication for whole APIs of given module.|
||Skip the serialiser.|
||Skip the timestamps columns (created_at and updated_at) into migration|
||Skip the pagination from index API of controller.|
Kriangle support some additional arguments also for generate code as per requirement.
By default, Kriangle generate code based on
sqlite3 database, mostly migration files depends on it. But you can provide the database to generate the code based on that.
sqlite3. You have to add adapter/gem based on your database in your Gemfile
Kriangle generate all APIs under
app/controllers/api/v1 folder. You can change the wrapper name (v1) as per you need with
wrapper arguments as below.
Kriangle by default use the model name for routes. So if do not pass it will determined with model name. i.e. For
Post model api routes will be
posts and whole end points will be like
api/v1/posts. You can change it by passing your desired path as below.
Now all APIs points to
/api/v1/blogs but model will be
Kriangle provides mechanism to add parent reference with previously created model (i.e.
Post ) or
If you want that newly created records associate with current logged in user, you have to pass below arguments.
--reference=true --reference_name=current_user --association_type=has_many
There are other options also available under this Parent Reference.
||Current model's association sets up a one-to-one connection with selected model, such that each instance of the declaring model 'belongs to' one instance of the selected model.|
||Default false. Instead of counting the associated records in the database every time the page loads, ActiveRecord’s counter caching feature allows storing the counter and updating it every time an associated object is created or removed. Parent model should be defined before this model.|
||In rails touch is used to update the parent's updated_at field for persisted objects.|
||Nested attributes allow you to save attributes on associated records through the parent. By default nested attribute updating is turned off and you can enable it using the accepts_nested_attributes_for class method. When you enable nested attributes an attribute writer is defined on the model.|
Kriangle provides mechanism to add self reference within the model.
If you want that newly created records associate with existing record of same table, you have to pass below arguments.
--self_reference=true --parent_association_name=parent --child_association_name=replies
You can chose your desired association name through
To understand the uses of Kriangle, we will provide a example of Blogger rails application.
Let's create a rails project.
rails _5.2.3_ new blogger
Now follow the Getting Started to setup the Kriangle gem into your newly created blogger rails project. After that we run its generators to scaffold new modules into the blogger rails project as below.
Let generate the authentication module. Its a mendatory step.
rails g kriangle:install User email:string:true name:string:true --skip_avatar=true --skip_tips=true --controller_path=Auth
Let now generate the two modules.
Kriangle provide you different arguments to control the module generation. So we will use some of them mentioned below.
A full fledge command which contains approx all arguments will be like something below.
- It will generate the
Blogmodel with title, description, published columns and with enabling searching on title and description columns.
- It will also create all CRUD APIs.
- It will also associate the records with
- It will also enable counter caching on users table to store the blogs_count.
rails g kriangle:module Blog ma:has_many:comments:delete_all:false:false:false:false: title:string:false:_cont_any description:text:false:_cont_any published:boolean:false::false index show create update destroy --reference=true --reference_name=current_user --association_type=has_many --counter_cache=true --skip_tips=true --creation_method=new
Next command will generate the
Comment model associated with
Blog model with message column. Its also provide the self reference mechanism to itself table to store the replies on the comments.
rails g kriangle:module Comment ma:belongs_to:user::true:false:false:false: message:text:false index show create update destroy --reference=true --reference_name=Blog --association_type=has_many --counter_cache=true --self_reference=true --parent_association_name=parent --child_association_name=replies --skip_tips=true --creation_method=new
When you are done with these commands, please run
rake db:migrate to complete the migration.
Now run the application and go to http://localhost:3000/swagger routes to check the APIs documentation as below.
Also you can check the rails project which will magically contains the generated code.
This is the example of Kriangle which can be use to scaffold a full working module with these generators. Play it!
Hope you like it!. if you face any issue, please feel free to contact us :)
The gem is available as open source under the terms of the LGPL-3.0 License.
Code of Conduct
Everyone interacting in the Kriangle project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.