From f2609529e5cf20bda98397246368c3a821f5db1d Mon Sep 17 00:00:00 2001 From: Bijoy Sijo Date: Fri, 24 Sep 2021 03:22:18 +0530 Subject: [PATCH] Add soft delete to lsg bodies. --- app/controllers/lsg_bodies_controller.rb | 14 +++++++++++--- app/models/lsg_body.rb | 8 ++++++++ app/policies/lsg_body_policy.rb | 3 ++- app/views/lsg_bodies/index.html.erb | 6 +++++- config/routes.rb | 6 +++++- .../20210923215047_add_archived_to_lsg_bodies.rb | 5 +++++ db/schema.rb | 4 +++- 7 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20210923215047_add_archived_to_lsg_bodies.rb diff --git a/app/controllers/lsg_bodies_controller.rb b/app/controllers/lsg_bodies_controller.rb index f2e7e4d7..b3c5ebf8 100644 --- a/app/controllers/lsg_bodies_controller.rb +++ b/app/controllers/lsg_bodies_controller.rb @@ -34,9 +34,17 @@ def update redirect_to lsg_body_path(params[:id]) end - def destroy - authorize LsgBody - LsgBody.delete(params[:id]) + def archive + id = params[:lsg_body_id] + @lsg_body = LsgBody.find(id) + @lsg_body.archive! + redirect_to lsg_bodies_path + end + + def unarchive + id = params[:lsg_body_id] + @lsg_body = LsgBody.find(id) + @lsg_body.unarchive! redirect_to lsg_bodies_path end diff --git a/app/models/lsg_body.rb b/app/models/lsg_body.rb index 4aadc342..e12061db 100644 --- a/app/models/lsg_body.rb +++ b/app/models/lsg_body.rb @@ -10,4 +10,12 @@ class LsgBody < ApplicationRecord corporation: 'Corporation', panchayat: 'Panchayat', } + + def archive! + update(archived: true) + end + + def unarchive! + update(archived: false) + end end diff --git a/app/policies/lsg_body_policy.rb b/app/policies/lsg_body_policy.rb index 7b61ce9b..f40b4ad6 100644 --- a/app/policies/lsg_body_policy.rb +++ b/app/policies/lsg_body_policy.rb @@ -8,5 +8,6 @@ def index? alias create? index? alias edit? index? alias update? index? - alias destroy? index? + alias archive? index? + alias unarchive? index? end diff --git a/app/views/lsg_bodies/index.html.erb b/app/views/lsg_bodies/index.html.erb index 6e831f0a..46526cbe 100644 --- a/app/views/lsg_bodies/index.html.erb +++ b/app/views/lsg_bodies/index.html.erb @@ -79,7 +79,11 @@ > <%= link_to "View", lsg_body_path(lsg_body.id), class: "px-2 text-indigo-600 hover:text-indigo-900" %> <%= link_to "Edit", edit_lsg_body_path(lsg_body.id), class: "px-2 text-indigo-600 hover:text-indigo-900" %> - <%= link_to "Delete", lsg_body_path(lsg_body.id), class: "px-2 text-red-600 hover:text-red-900", method: :delete %> + <% if lsg_body.archived? %> + <%= link_to "Unarchive", lsg_body_unarchive_path(lsg_body), class: "px-2 text-red-600 hover:text-red-900", method: :put %> + <% else %> + <%= link_to "Archive", lsg_body_archive_path(lsg_body), class: "px-2 text-red-600 hover:text-red-900", method: :put %> + <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index b28f3e60..90bb4218 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -69,10 +69,14 @@ get "/facilities/districts_of_state/:state_id", to: "facilities#districts_of_state" get "/facilities/wards_of_lsg_body/:lsg_body_id", to: "facilities#wards_of_lsg_body" + resources :lsg_bodies do + put 'archive' + put 'unarchive' + end + resources :visit_details resources :sessions resources :facilities - resources :lsg_bodies resources :wards devise_for :users, controllers: { sessions: "users/sessions", diff --git a/db/migrate/20210923215047_add_archived_to_lsg_bodies.rb b/db/migrate/20210923215047_add_archived_to_lsg_bodies.rb new file mode 100644 index 00000000..4168d2b8 --- /dev/null +++ b/db/migrate/20210923215047_add_archived_to_lsg_bodies.rb @@ -0,0 +1,5 @@ +class AddArchivedToLsgBodies < ActiveRecord::Migration[6.1] + def change + add_column :lsg_bodies, :archived, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 20d03618..0c27392b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_09_11_083108) do +ActiveRecord::Schema.define(version: 2021_09_23_215047) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -79,6 +79,8 @@ t.datetime "updated_at", precision: 6, null: false t.string "code" t.uuid "district_id", null: false + t.boolean "archive", default: false + t.boolean "archived", default: false t.index ["district_id"], name: "index_lsg_bodies_on_district_id" end