I’ve just checked my blog and was quite disappointed to see the last post was before 5 months. Kind off busy with my stuffs. Recently i was working on a RoR project and got used to the flexibility of RoR.
Today am going to showcase the use of soft deletion and CRUD operation using Acts as Paranoid and Active Scaffold Gem. There may be cases where you should be able to restrict certain users to view certain records, delete certain records(soft deletion),etc. Here soft deletion refers to hiding data from users when it has been deleted, but not really dropped from database.
Overview on Paranoid Gem:
This gem provides functionality to hide(soft delete) data records and recover(showcase) the same. The data types supported by this Gem is listed below:
* boolean
* time
* string
Even though it supports boolean, the transactions are not True/False based. It always retrieves record with paranoid column having nil value except for string type columns. For string type columns, it will retrieve record with paranoid columns having nil or paranoid column not having the deleted value specified in the respective model.
Some basic operations:
Model.all -> Retrieves only the active records
Model.with_deleted.all -> Retrieves only the inactive records(soft deleted records)
Model.only_deleted.all -> Retrieves all records(both active and inactive)
Model.find(1).destroy -> Hides the active data record with id=1(make it inactive)
Model.only_deleted.find(1).recover -> Shows the inactive data record with id=1(make it active)
Model.find(1).destroy! -> Removes the data record from Database
It supports with_deleted only on belongs_to association.
In my previous project, the column was of integer type(Not supported by paranoid) and we had to do some monkey patch stuff to accommodate our needs.
Overview on Active Scaffold Gem:
Active Scaffold makes your life easy by providing a CRUD based structure that allows you to tabulate data records with pagination and CRUD operations. This gem can be configured as per the user needs.
Some basic Actions:
Create – Allow creating new records
List – List all data records
Search – Enable Searching of data records
Update – Update data record
Show – Show data records
Delete – Delete data records
Nested – Allows to edit relational data
Sub form – Allows CRUD of a model different from the present controller
It also allows you to use user-defined actions via action links. In our example Activate and Destroy are used defined methods. In order to use user defined methods, you have to do some changes in the routes.
To demonstrate it, we can consider a college management application. Here only admin’s can view records of all students(currently studying,alumni,etc). The faculties should access only the details of students currently pursuing their degree but other admin’s may access records of all students.
You can checkout the code base here and view it here.
Moving Forward, we will be talking about two roles(Admin/Faculty).
Admin – Can view all records and perform CRUDRD(Create, Read, Update, Delete, Restore, Destroy)actions on Student record.
Here Delete refers to hiding(soft deletion) and Destroy refers to real deletion(remove data record from database).
Faculty – Can view only Active records and perform RU(Read, Update) actions on Student record.
Sample Rails app:
1.Create a rails app
rails new manage
2. Add active_scaffold, acts_as_paranoid to Gemfile and bundle install.
3. Add active_scaffold to application.css and application.js
4. Create models for branch, role, gender, student, user,etc
rails g model role name:string description:text
rails g active_scaffold gender name:string
rails g active_scaffold branch name:string description:text
rails g model user role_id:integer username:string encrypted_password:text
rails g active_scaffold student rollno:string firstname:string lastname:string branch_id:integer gender_id:integer status:string
5. Add custom methods for models(Activate, Destroy)
def destroy_record
@record=Student.only_deleted.find(params[:id])
flash[:info] = “Record with Firstname #{@record.firstname} has been Removed from Database”
@record.destroy
return_to_main
end
def activate
@record=Student.only_deleted.find(params[:id])
flash[:info] = “Record with Firstname #{@record.firstname} has been Restored”
@record.recover
return_to_main
end
6. Add custom methods to routes.
resources :students do
member do
get ‘activate’
get ‘destroy_record’
end
as_routes end
7. Add configuration for Student Controller
active_scaffold :”student” do |conf|
conf.label=”Student Details”
conf.actions=[:create,:list,:search,:update,:delete,:subform]
conf.delete.refresh_list = true
conf.columns=[:rollno,:firstname,:lastname,:gender,:branch,:status]
conf.search.columns = [:firstname,:lastname]
conf.columns[:gender].clear_link
conf.columns[:branch].clear_link
conf.search.live=true
conf.list.always_show_search=true
conf.list.per_page = 25
conf.action_links.add ‘activate’, :label => ‘Activate’, :type => :record, :confirm => ‘Do you want to restore record?’, :page => true
conf.action_links.add ‘destroy_record’, :label => ‘Destroy’, :type => :record, :confirm => ‘Do you want to remove record from Database?’, :page => true
end