diff --git a/app/controllers/organizations/adoption_application_reviews_controller.rb b/app/controllers/organizations/adoption_application_reviews_controller.rb index 6ba3a253d..65536b714 100644 --- a/app/controllers/organizations/adoption_application_reviews_controller.rb +++ b/app/controllers/organizations/adoption_application_reviews_controller.rb @@ -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 diff --git a/app/models/adopter_application.rb b/app/models/adopter_application.rb index f1853e3fd..17b530b34 100644 --- a/app/models/adopter_application.rb +++ b/app/models/adopter_application.rb @@ -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 diff --git a/app/models/pet.rb b/app/models/pet.rb index c0fb7749c..1f71c0f64 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -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) diff --git a/app/views/organizations/adoption_application_reviews/_search_results.html.erb b/app/views/organizations/adoption_application_reviews/_search_results.html.erb index ce1471f60..65f3eb36b 100644 --- a/app/views/organizations/adoption_application_reviews/_search_results.html.erb +++ b/app/views/organizations/adoption_application_reviews/_search_results.html.erb @@ -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| %>
diff --git a/test/controllers/organizations/adoption_application_reviews_controller_test.rb b/test/controllers/organizations/adoption_application_reviews_controller_test.rb new file mode 100644 index 000000000..5135f88b0 --- /dev/null +++ b/test/controllers/organizations/adoption_application_reviews_controller_test.rb @@ -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 |