Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add soft delete to lsg bodies. #101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 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,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

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add a filed archived_at so that we could figure out when the record was archived?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep record of all archive actions when was archived and when was unarchived like we have logs?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll then have :archived, :archived_at fields. Won't it be ideal to keep only one of these? Both the fields can serve as a flag. So should we only go with the :archived_at field?

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.