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

355 filtering adopter applications #373

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
b8c7baf
fix path to applications partial
phonghpham Dec 10, 2023
c3bffd6
Fix how pet is referenced--not as instance var
phonghpham Dec 11, 2023
9ba22ba
refactor ternary for readability
phonghpham Dec 11, 2023
516080e
TDD AdoptionApplicationReviewsController
phonghpham Dec 11, 2023
5807304
add test for filtering by pet name
phonghpham Dec 11, 2023
17c93ea
Implement filtering in AdoptionApplicationReviewsController
phonghpham Dec 12, 2023
4be0e2e
WIP: filtering on applicant name
phonghpham Dec 13, 2023
e8899ec
Allowlist AdopterApplication model associations; finish implementing
phonghpham Dec 14, 2023
420ffd2
add test for filtering by application status
phonghpham Dec 16, 2023
6ad8614
add instance method for getting applications by status
phonghpham Dec 16, 2023
4bac6ce
implement filtering by application status
phonghpham Dec 16, 2023
fef0e67
add ransack search form for filtering adopter applications
phonghpham Dec 17, 2023
53f5ab4
add ransacker for mapping status enum
phonghpham Dec 17, 2023
dbdff46
query for adopter application associations without overwriting all ot…
phonghpham Dec 17, 2023
4073292
update test to target span where status rendered since all statuses a…
phonghpham Dec 17, 2023
6198e2b
Add explicit ransack param check to ensure pet name and applicant name
phonghpham Dec 17, 2023
262dd09
make ransack search matchers case insensitive
phonghpham Dec 17, 2023
e04a359
update applications to display pet name
phonghpham Dec 21, 2023
b7ddf34
fix view path
phonghpham Dec 21, 2023
4567e77
Explicitly pass in applications to render call of pet show view
phonghpham Dec 21, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@ class Organizations::AdoptionApplicationReviewsController < Organizations::BaseC
layout "dashboard"

def index
@pets = Pet.org_pets_with_apps(current_user.staff_account.organization_id)
@q = Pet.org_pets_with_apps(current_user.staff_account.organization_id).ransack(params[:q])
@pets_with_applications = @q.result.includes(:adopter_applications)
@pet = selected_pet

# Combining these into a single chained statement does not yield the same result due to how Ransack processes parameters.
if params[:q].present? && params[:q]["adopter_applications_status_eq"].present?
status_filter = params[:q]["adopter_applications_status_eq"]
@pets_with_applications = filter_by_application_status(@pets_with_applications, status_filter)
end
end

def filter_by_application_status(pets_relation, status_filter)
pets_relation.joins(:adopter_applications).where(adopter_applications: {status: status_filter})
end

def edit
Expand Down
12 changes: 12 additions & 0 deletions app/models/adopter_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,16 @@ def self.set_status_to_withdrawn(adopter_application)
def applicant_name
"#{adopter_account.user.last_name}, #{adopter_account.user.first_name}"
end

ransacker :applicant_name do
Arel.sql("CONCAT(users.last_name, ', ', users.first_name)")
end

ransacker :status, formatter: proc { |v| statuses[v] } do |parent|
parent.table[:status]
end

def self.ransackable_attributes(auth_object = nil)
["applicant_name", "status"]
end
end
2 changes: 1 addition & 1 deletion app/models/pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def self.ransackable_attributes(auth_object = nil)
end

def self.ransackable_associations(auth_object = nil)
[]
["adopter_applications"]
end

def self.ransackable_scopes(auth_object = nil)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<%# @pet, @pets are defined in the adoption_application_reviews controller %>

<% if @pet.nil? ? @collection = @pets : @collection = @pet %>
<% @collection = @pet.nil? ? @pets_with_applications : @pet %>

<% @collection.each do |pet| %>
<!--pet name-->
<div class="mb-4">
<%= render "partials/pet_applications", applications: pet.adopter_applications, pet: pet %>
<%= render "organizations/pets/tabs/applications", applications: pet.adopter_applications, pet: pet %>
</div>
<% end %>

<% end %>
42 changes: 28 additions & 14 deletions app/views/organizations/adoption_application_reviews/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,38 @@
<% p.content do %>
<section class="pb-5" id="about_us">
<div class="container">
<h5 class="mb-2">Filter by pet name</h5>
<%= bootstrap_form_with url: '/adopter_applications', method: :get do | f | %>

<div class="form-group">
<div class="row mb-2">
<div class="col-md-4 mb-3">
<%= f.collection_select :pet_id, @pets, :id, :name, { prompt: 'All' },
class: 'form-control bigger' %>
<!-- Ransack Search Form -->
<div class="row p-4 border rounded-5 mb-5 bg-white">
<div class="col-xl-12">
<%= search_form_for @q, url: adoption_application_reviews_path do |f| %>
<div class="row">
<div class="form-group mb-3 col-md-4">
<%= f.label :name_cont, "Pet Name" %>
<%= f.text_field :name_i_cont, class: "form-control", placeholder: "Enter Pet Name" %>
</div>
<div class="form-group mb-3 col-md-4">
<%= f.label :adopter_applications_applicant_name_cont, "Applicant Name" %>
<%= f.text_field :adopter_applications_applicant_name_i_cont, class: "form-control", placeholder: "Enter Applicant Name" %>
</div>
<div class="form-group mb-3 col-md-4">
<%= f.label :adopter_applications_status_eq, "Application Status" %>
<%= f.select :adopter_applications_status_eq, AdopterApplication.statuses.keys.map { |status| [status.titleize, status] }, { include_blank: 'All' }, class: "form-select" %>
</div>
</div>
<div class="col-md-4 mb-3">
<%= f.submit 'Submit', class: 'btn btn-outline-dark' %>
<div class="row">
<div class="col-md-12 text-right">
<%= f.submit "Search", class: "btn btn-primary" %>
<%= link_to "Clear filters", adoption_application_reviews_path, class: "btn btn-default" %>
</div>
</div>
</div> <!--row-->
</div>
<% end %>
<% end %>
</div>
</div>

<!-- Search Results -->
<%= render 'search_results' %>
</div> <!--container-->

</div> <!-- Container -->
</section>
<% end %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/organizations/pets/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<% p.content do %>
<div class="row">
<div class="col-md-12 col-xl-8 col-12">
<%= render partial: "organizations/pets/tabs/#{@active_tab}", locals: { pet: @pet } %>
<%= render partial: "organizations/pets/tabs/#{@active_tab}", locals: { applications: @pet.adopter_applications, pet: @pet} %>
</div>

<div class="col-md-12 col-xl-4 col-12">
Expand Down
4 changes: 2 additions & 2 deletions app/views/organizations/pets/tabs/_applications.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="card">
<div class="card-header">
<h4 class="mb-0">Applications</h4>
<h4 class="mb-0"><%= pet.name %></h4>
</div>
<div class="card-body table-responsive">
<table class="table mb-0 text-nowrap table-hover table-centered">
Expand All @@ -14,7 +14,7 @@
</tr>
</thead>
<tbody>
<% @pet.adopter_applications.each do |app|%>
<% applications.each do |app|%>
<tr>
<!-- applicant -->
<td class="align-middle">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require "test_helper"

class Organizations::AdoptionApplicationReviewsControllerTest < ActionDispatch::IntegrationTest
context "Filtering adoption applications" do
setup do
@user = create(:user, :verified_staff)
set_organization(@user.organization)
sign_in @user
end

teardown do
:after_teardown
end

context "by pet name" do
setup do
@pet1 = create(:pet, name: "Pango", organization: @user.staff_account.organization)
@pet2 = create(:pet, name: "Tycho", organization: @user.staff_account.organization)
adopter_account1 = create(:adopter_account, :with_adopter_profile, organization: @user.staff_account.organization)
adopter_account2 = create(:adopter_account, :with_adopter_profile, organization: @user.staff_account.organization)
create(:adopter_application, pet: @pet1, adopter_account: adopter_account1)
create(:adopter_application, pet: @pet2, adopter_account: adopter_account2)
end

should "return applications for a specific pet name" do
get adoption_application_reviews_url, params: {q: {name_i_cont: "Pango"}}
assert_response :success
assert_match "Pango", @response.body
refute_match "Tycho", @response.body
end
end

context "by applicant name" do
setup do
@pet = create(:pet, organization: @user.staff_account.organization)
adopter_account1 = create(:adopter_account, :with_adopter_profile,
user: create(:user, first_name: "David", last_name: "Attenborough",
organization: @user.staff_account.organization))
adopter_account2 = create(:adopter_account, :with_adopter_profile,
user: create(:user, first_name: "Jane", last_name: "Goodall",
organization: @user.staff_account.organization))
create(:adopter_application, pet: @pet, adopter_account: adopter_account1)
create(:adopter_application, pet: @pet, adopter_account: adopter_account2)
end

should "return applications for a specific applicant name" do
get adoption_application_reviews_url, params: {q: {adopter_applications_applicant_name_i_cont: "Attenborough"}}
assert_response :success
assert_match "Attenborough, David", @response.body
refute_match "Goodall, Jane", @response.body
end
end

context "Filtering by application status" do
setup do
@pet = create(:pet, organization: @user.staff_account.organization)
adopter_account1 = create(:adopter_account, :with_adopter_profile, organization: @user.staff_account.organization)
adopter_account2 = create(:adopter_account, :with_adopter_profile, organization: @user.staff_account.organization)
@application_under_review = create(:adopter_application, pet: @pet, adopter_account: adopter_account1, status: :under_review)
@application_awaiting_review = create(:adopter_application, pet: @pet, adopter_account: adopter_account2, status: :awaiting_review)
end

should "return pets only with applications of the specified status" do
get adoption_application_reviews_url, params: {q: {adopter_applications_status_eq: "under_review"}}
assert_response :success
assert_select "span.badge.bg-dark-info", text: "Under Review"
assert_select "span.badge", text: "Awaiting Review", count: 0
end
end
end
end