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 f260952
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 7 deletions.
14 changes: 11 additions & 3 deletions app/controllers/lsg_bodies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

def archive!
update(archived: true)
end

def unarchive!
update(archived: false)
end
end
3 changes: 2 additions & 1 deletion app/policies/lsg_body_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion app/views/lsg_bodies/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 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 f260952

Please sign in to comment.