Skip to content

Commit

Permalink
Fixes: rubyforgood#773
Browse files Browse the repository at this point in the history
The goal of this ticket was to add the ability for staff to deactivate ‘fosterer’ accounts just like how ‘staff’ accounts can already be deactivated. A migration was generated to add ‘deactivated_at’ to the ‘adopter_foster_account’ table. Since this functionality was going to be repeated in multiple places I decided to refactor. I pulled out the ’staff/staff/_deactivate_toggle.html.erb’ partial and put it in ‘app/views/partials’ so it was more accessible. I then refactored this to be more generic so multiple roles could use it.
Related files were added/edited to make this work. For example, ‘update.turbo_stream.erb’ was added to the ‘staff/fosterers’ directory, and the one within ‘staff/staff’ was edited to fit the refactor. I’m not sure if there is potential to refactor these turbo files to keep the code even more DRY. Also, ‘deactivate’, ‘activate’, ‘update_activation’, and ‘set_fosterer’ were all added to the ‘fosterers_controller.rb’ to facilitate the new behavior. ‘set_fosterer’ involved authentication with ‘Action Policy’. Shoutout to @jmilljr24 for hardcore carrying me on the back half of this PR with all things related to the policies and routes.
I’ve yet to add tests for this, but the behavior is functioning.

Extra tidbit:
Added a new gem to the gem file called "binding_of_caller" that enables the REPL and local/instance variable inspection when an error blows up the website.
  • Loading branch information
jaxonavena committed Jun 1, 2024
1 parent 4762c1a commit d5558f2
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,6 @@ gem "rails-controller-testing"

# Use Action Policy for authorization framework
gem "action_policy", "~> 0.6.9"

# enable the REPL and local/instance variable inspection
gem "binding_of_caller"
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ GEM
rouge (>= 1.0.0)
bigdecimal (3.1.8)
bindex (0.8.1)
binding_of_caller (1.0.1)
debug_inspector (>= 1.2.0)
bootsnap (1.18.3)
msgpack (~> 1.2)
bootstrap (5.3.3)
Expand Down Expand Up @@ -154,6 +156,7 @@ GEM
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
debug_inspector (1.2.0)
devise (4.9.4)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
Expand Down Expand Up @@ -511,6 +514,7 @@ DEPENDENCIES
annotate
azure-storage-blob (~> 2.0)
better_errors (~> 2.9, >= 2.9.1)
binding_of_caller
bootsnap
bootstrap
bootstrap_form (~> 5.4)
Expand Down
33 changes: 33 additions & 0 deletions app/controllers/organizations/staff/fosterers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
class Organizations::Staff::FosterersController < Organizations::BaseController
layout "dashboard"
before_action :set_fosterer, only: %i[update_activation]

def index
authorize! AdopterFosterAccount, context: {organization: Current.organization}

@fosterer_accounts = authorized_scope(AdopterFosterAccount.fosterers)
end

def deactivate
@fosterer_account.deactivate
respond_to do |format|
format.html { redirect_to staff_fosterers_index_path, notice: "Fosterer account deactivated." }
format.turbo_stream { render "organizations/staff/fosterers/update" }
end
end

def activate
@fosterer_account.activate
respond_to do |format|
format.html { redirect_to staff_fosterers_index_path, notice: "Fosterer account activated." }
format.turbo_stream { render "organizations/staff/fosterers/update" }
end
end

def update_activation
if @fosterer_account.deactivated_at
activate
else
deactivate
end
end

private

def set_fosterer
@fosterer_account = AdopterFosterAccount.find(params[:fosterer_id])

authorize! @fosterer_account
end
end
13 changes: 13 additions & 0 deletions app/models/adopter_foster_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Table name: adopter_foster_accounts
#
# id :bigint not null, primary key
# deactivated_at :datetime
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :bigint not null
Expand Down Expand Up @@ -33,4 +34,16 @@ class AdopterFosterAccount < ApplicationRecord
scope :fosterers, -> {
joins(user: :roles).where(roles: {name: "fosterer"})
}

def deactivate
update(deactivated_at: Time.now) unless deactivated_at
end

def activate
update(deactivated_at: nil) if deactivated_at
end

def deactivated?
!!deactivated_at
end
end
1 change: 1 addition & 0 deletions app/models/concerns/authorizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
manage_tasks
view_organization_dashboard
manage_faqs
manage_adopter_foster_accounts
]
).freeze

Expand Down
6 changes: 6 additions & 0 deletions app/policies/organizations/adopter_foster_account_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ class Organizations::AdopterFosterAccountPolicy < ApplicationPolicy
pre_check :verify_organization!
pre_check :verify_active_staff!

alias_rule :deactivate?, :update_activation?, to: :activate?

def index?
permission?(:view_adopter_foster_accounts)
end

def activate?
permission?(:manage_adopter_foster_accounts)
end
end
7 changes: 7 additions & 0 deletions app/views/organizations/staff/fosterers/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<span>Joined</span>
<span><%= fosterer.created_at.strftime("%d %B, %Y") %></span>
</div>
<div class="d-flex justify-content-between pt-2">
<span>Deactivate</span>
<%= render "partials/deactivate_toggle", account: fosterer, role: 'fosterer' %>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -90,6 +94,9 @@
<td>
<%= fosterer.created_at.strftime("%d %B, %Y") %>
</td>
<td>
<%= render "partials/deactivate_toggle", account: fosterer, role: 'fosterer' %>
</td>
<td>
<div class="hstack gap-4">
<a href="#" class="fe fe-mail text-muted" data-bs-toggle="tooltip" data-placement="top" aria-label="Message" data-bs-original-title="Message"></a>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= turbo_stream.replace_all ".deactivate_toggle_#{@fosterer_account.id}", partial: "partials/deactivate_toggle",
locals: {account: @fosterer_account, role: "fosterer"} %>
10 changes: 0 additions & 10 deletions app/views/organizations/staff/staff/_deactivate_toggle.html.erb

This file was deleted.

4 changes: 2 additions & 2 deletions app/views/organizations/staff/staff/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</div>
<div class="d-flex justify-content-between pt-2">
<span>Deactivate</span>
<%= render "deactivate_toggle", staff: staff %>
<%= render "partials/deactivate_toggle", account: staff, role: 'staff'%>
</div>
</div>
</div>
Expand Down Expand Up @@ -104,7 +104,7 @@
<%= staff.created_at.strftime("%d %B, %Y") %>
</td>
<td>
<%= render "deactivate_toggle", staff: staff %>
<%= render "partials/deactivate_toggle", account: staff, role: 'staff' %>
</td>
<td>
<div class="hstack gap-4">
Expand Down
4 changes: 2 additions & 2 deletions app/views/organizations/staff/staff/update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<%= turbo_stream.replace_all ".staff_deactivate_toggle_#{@staff_account.id}", partial: "deactivate_toggle",
locals: {staff: @staff_account} %>
<%= turbo_stream.replace_all ".staff_deactivate_toggle_#{@staff_account.id}", partial: "partials/deactivate_toggle",
locals: {account: @staff_account, role: "staff"} %>
27 changes: 27 additions & 0 deletions app/views/partials/_deactivate_toggle.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<div class=<%= "deactivate_toggle_#{account.id}" %>>
<%= form_with model: account, url: send("staff_#{role}_update_activation_path", account), method: "post" do |form| %>
<div class='form-group d-flex justify-content-center'>
<div class="form-check form-switch">
<%= form.check_box :deactivated?, { class: "form-check-input",
role: "switch", id: "flexSwitchCheckChecked", onchange: "this.form.requestSubmit()" }, true, false %>
</div>
</div>
<% end %>
</div>

<%# url: url_for([role.to_sym, account, :update_activation])
fosterer_adopter_foster_account_update_activation_path %>
<%#
<div class=<%= "staff_deactivate_toggle_#{staff.id}"
<%= form_with model: staff, url: staff_staff_update_activation_path(staff), method: "post" do |form|
<div class='form-group d-flex justify-content-center'>
<div class="form-check form-switch">
<%= form.check_box :deactivated?,{ class: "form-check-input",
role: "switch", id: "flexSwitchCheckChecked", onchange: "this.form.requestSubmit()"}, true, false
</div>
</div>
<% end
</div>
%>
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@

resources :adoption_application_reviews, only: %i[index edit update]
resources :manage_fosters, only: %i[new create index edit update destroy]
resources :fosterers, only: %i[index]
resources :fosterers, only: %i[index] do
post "deactivate", to: "fosterers#deactivate"
post "activate", to: "fosterers#activate"
post "update_activation", to: "fosterers#update_activation"
end
resources :staff do
post "deactivate", to: "staff#deactivate"
post "activate", to: "staff#activate"
Expand All @@ -54,6 +58,7 @@
resources :dashboard, only: [:index]
resources :likes, only: [:index, :create, :destroy]
resources :adopter_applications, path: "applications", only: %i[index create update]

end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDeactivatedAtToAdopterFosterAccounts < ActiveRecord::Migration[7.1]
def change
add_column :adopter_foster_accounts, :deactivated_at, :datetime
end
end
3 changes: 2 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 d5558f2

Please sign in to comment.