diff --git a/app/controllers/lsg_bodies_controller.rb b/app/controllers/lsg_bodies_controller.rb
index f2e7e4d7..0386946d 100644
--- a/app/controllers/lsg_bodies_controller.rb
+++ b/app/controllers/lsg_bodies_controller.rb
@@ -2,13 +2,12 @@ class LsgBodiesController < ApplicationController
before_action :set_lsg_body, only: %i[edit update]
def index
- @lsg_body = LsgBody.all
- authorize LsgBody
+ @lsg_body = policy_scope(LsgBody)
end
def new
@lsg_body = LsgBody.new
- authorize LsgBody
+ authorize @lsg_body
end
def create
@@ -20,23 +19,33 @@ def create
def show
id = params[:id]
@lsg_body = LsgBody.find(id)
- authorize LsgBody
+ authorize @lsg_body
@wards = Ward.where(lsg_body_id: id)
end
def edit
- authorize LsgBody
+ authorize @lsgbody
end
def update
- authorize LsgBody
+ authorize @lsg_body
@lsg_body.update(lsg_bodies_params)
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)
+ authorize @lsg_body
+ @lsg_body.update(archived: true, archived_by: current_user.full_name, archived_at: DateTime.now)
+ redirect_to lsg_bodies_path
+ end
+
+ def unarchive
+ id = params[:lsg_body_id]
+ @lsg_body = LsgBody.find(id)
+ authorize @lsg_body
+ @lsg_body.update(archived: false, archived_by: nil, archived_at: nil)
redirect_to lsg_bodies_path
end
diff --git a/app/models/lsg_body.rb b/app/models/lsg_body.rb
index 4aadc342..d4b5213c 100644
--- a/app/models/lsg_body.rb
+++ b/app/models/lsg_body.rb
@@ -10,4 +10,6 @@ class LsgBody < ApplicationRecord
corporation: 'Corporation',
panchayat: 'Panchayat',
}
+
+ scope :live_lsgs, -> { where(archived: false) }
end
diff --git a/app/policies/lsg_body_policy.rb b/app/policies/lsg_body_policy.rb
index 7b61ce9b..4e23a2b7 100644
--- a/app/policies/lsg_body_policy.rb
+++ b/app/policies/lsg_body_policy.rb
@@ -1,12 +1,22 @@
class LsgBodyPolicy < ApplicationPolicy
- def index?
+ class Scope < Scope
+ def resolve
+ if user.superuser?
+ scope.all
+ else
+ scope.where(archived: false)
+ end
+ end
+ end
+
+ def new?
user && user.superuser?
end
- alias new? index?
- alias show? index?
- alias create? index?
- alias edit? index?
- alias update? index?
- alias destroy? index?
+ alias create? new?
+ alias update? new?
+ alias edit? new?
+ alias show? new?
+ alias archive? new?
+ alias unarchive? new?
end
diff --git a/app/views/lsg_bodies/index.html.erb b/app/views/lsg_bodies/index.html.erb
index 6e831f0a..70597f67 100644
--- a/app/views/lsg_bodies/index.html.erb
+++ b/app/views/lsg_bodies/index.html.erb
@@ -52,7 +52,7 @@
- <% @lsg_body.each do |lsg_body| %>
+ <% policy_scope(@lsg_body).each do |lsg_body| %>
<%= 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/migrate/20210924200830_add_archived_by_to_lsg_bodies.rb b/db/migrate/20210924200830_add_archived_by_to_lsg_bodies.rb
new file mode 100644
index 00000000..bb890593
--- /dev/null
+++ b/db/migrate/20210924200830_add_archived_by_to_lsg_bodies.rb
@@ -0,0 +1,5 @@
+class AddArchivedByToLsgBodies < ActiveRecord::Migration[6.1]
+ def change
+ add_column :lsg_bodies, :archived_by, :string
+ end
+end
diff --git a/db/migrate/20210924202133_add_archived_at_to_lsg_bodies.rb b/db/migrate/20210924202133_add_archived_at_to_lsg_bodies.rb
new file mode 100644
index 00000000..c26de885
--- /dev/null
+++ b/db/migrate/20210924202133_add_archived_at_to_lsg_bodies.rb
@@ -0,0 +1,5 @@
+class AddArchivedAtToLsgBodies < ActiveRecord::Migration[6.1]
+ def change
+ add_column :lsg_bodies, :archived_at, :datetime
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 20d03618..fc292487 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_24_202133) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
@@ -79,6 +79,9 @@
t.datetime "updated_at", precision: 6, null: false
t.string "code"
t.uuid "district_id", null: false
+ t.boolean "archived", default: false
+ t.string "archived_by"
+ t.datetime "archived_at"
t.index ["district_id"], name: "index_lsg_bodies_on_district_id"
end