Skip to content

Commit

Permalink
Add soft delete to lsg bodies.
Browse files Browse the repository at this point in the history
  • Loading branch information
bijoysijo committed Sep 24, 2021
1 parent 99b8e6c commit 9de96c5
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 19 deletions.
25 changes: 17 additions & 8 deletions app/controllers/lsg_bodies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +19,7 @@ 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

Expand All @@ -29,14 +28,24 @@ def edit
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

Expand Down
2 changes: 2 additions & 0 deletions app/models/lsg_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ class LsgBody < ApplicationRecord
corporation: 'Corporation',
panchayat: 'Panchayat',
}

scope :live_lsgs, -> { where(archived: false) }
end
24 changes: 17 additions & 7 deletions app/policies/lsg_body_policy.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions app/views/lsg_bodies/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</tr>
</thead>
<tbody>
<% @lsg_body.each do |lsg_body| %>
<% policy_scope(@lsg_body).each do |lsg_body| %>
<tr class="bg-white">
<td
class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"
Expand All @@ -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 %>
</td>
</tr>
<% end %>
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20210923215047_add_archived_to_lsg_bodies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddArchivedToLsgBodies < ActiveRecord::Migration[6.1]
def change
add_column :lsg_bodies, :archived, :boolean, default: false
end
end
5 changes: 5 additions & 0 deletions db/migrate/20210924200830_add_archived_by_to_lsg_bodies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddArchivedByToLsgBodies < ActiveRecord::Migration[6.1]
def change
add_column :lsg_bodies, :archived_by, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20210924202133_add_archived_at_to_lsg_bodies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddArchivedAtToLsgBodies < ActiveRecord::Migration[6.1]
def change
add_column :lsg_bodies, :archived_at, :datetime
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9de96c5

Please sign in to comment.