diff --git a/app/models/concerns/pet_ransackable.rb b/app/models/concerns/pet_ransackable.rb new file mode 100644 index 000000000..ae042bdce --- /dev/null +++ b/app/models/concerns/pet_ransackable.rb @@ -0,0 +1,34 @@ +module PetRansackable + extend ActiveSupport::Concern + + class_methods do + def ransackable_attributes(auth_object = nil) + %w[name sex species breed placement_type application_paused published] + end + + def ransackable_associations(auth_object = nil) + ["adopter_applications"] + end + + def ransackable_scopes(auth_object = nil) + [:ransack_adopted, :ransack_birth_date] + end + + # Using string values to get around ransack bug: https://github.com/activerecord-hackery/ransack/issues/1375 + def ransack_adopted(adoption_state) + (adoption_state == "adopted") ? adopted : unadopted + end + + def ransack_birth_date(date) + start_date, end_date = date.split("/") + + if start_date != "none" && end_date != "none" + where("birth_date >= ? AND birth_date <= ?", start_date, end_date) + elsif start_date == "none" && end_date != "none" + where("birth_date <= ?", end_date) + elsif start_date != "none" && end_date == "none" + where("birth_date >= ?", start_date) + end + end + end +end diff --git a/app/models/pet.rb b/app/models/pet.rb index 5b17aebad..6719d9c48 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -28,6 +28,7 @@ # fk_rails_... (organization_id => organizations.id) # class Pet < ApplicationRecord + include PetRansackable include PetTaskable acts_as_tenant(:organization) @@ -64,8 +65,8 @@ class Pet < ApplicationRecord validate :sensible_placement_type - enum species: {"Dog" => 1, "Cat" => 2} - enum placement_type: ["Adoptable", "Fosterable", "Adoptable and Fosterable"] + enum :species, {"Dog" => 1, "Cat" => 2} + enum :placement_type, ["Adoptable", "Fosterable", "Adoptable and Fosterable"] WEIGHT_UNIT_LB = "lb".freeze WEIGHT_UNIT_KG = "kg".freeze @@ -117,33 +118,4 @@ def sensible_placement_type errors.add(:placement_type, I18n.t("activerecord.errors.models.pet.attributes.placement_type.sensible")) end end - - def self.ransackable_attributes(auth_object = nil) - ["name", "sex", "species", "breed"] - end - - def self.ransackable_associations(auth_object = nil) - ["adopter_applications"] - end - - def self.ransackable_scopes(auth_object = nil) - [:ransack_adopted, :ransack_birth_date] - end - - # Using string values to get around ransack bug: https://github.com/activerecord-hackery/ransack/issues/1375 - def self.ransack_adopted(adoption_state) - (adoption_state == "adopted") ? adopted : unadopted - end - - def self.ransack_birth_date(date) - start_date, end_date = date.split("/") - - if start_date != "none" && end_date != "none" - where("birth_date >= ? AND birth_date <= ?", start_date, end_date) - elsif start_date == "none" && end_date != "none" - where("birth_date <= ?", end_date) - elsif start_date != "none" && end_date == "none" - where("birth_date >= ?", start_date) - end - end end diff --git a/app/views/organizations/staff/pets/index.html.erb b/app/views/organizations/staff/pets/index.html.erb index 6af120aa9..ed0b5b653 100644 --- a/app/views/organizations/staff/pets/index.html.erb +++ b/app/views/organizations/staff/pets/index.html.erb @@ -23,8 +23,20 @@ <%= f.select :species_eq, Pet.species, {include_blank: 'All'}, class: "form-select" %>
- <%= f.label :ransack_adopted, "Status" %> - <%= f.select :ransack_adopted, [['Adopted', 'adopted'], ['Unadopted', 'unadopted']], {include_blank: 'All'}, class: "form-select" %> + <%= f.label :ransack_adopted, "Adoption Status" %> + <%= f.select :ransack_adopted, [['Adopted', 'adopted'], ['Not adopted', 'unadopted']], {include_blank: 'All'}, class: "form-select" %> +
+
+ <%= f.label :placement_type_eq, "Placement type" %> + <%= f.select :placement_type_eq, Pet.placement_types, {include_blank: 'All'}, class: "form-select" %> +
+
+ <%= f.label :application_paused_eq, "Paused" %> + <%= f.select :application_paused_eq, [['Yes', true], ['No', false]], {include_blank: 'All'}, class: "form-select" %> +
+
+ <%= f.label :published_eq, "Published" %> + <%= f.select :published_eq, [['Yes', true], ['No', false]], {include_blank: 'All'}, class: "form-select" %>
diff --git a/test/controllers/organizations/staff/pets_controller_test.rb b/test/controllers/organizations/staff/pets_controller_test.rb index 187a360f1..5563a0b4b 100644 --- a/test/controllers/organizations/staff/pets_controller_test.rb +++ b/test/controllers/organizations/staff/pets_controller_test.rb @@ -76,6 +76,42 @@ class Organizations::PetsControllerTest < ActionDispatch::IntegrationTest assert_equal 2, assigns[:pets].count assert_equal 2, assigns[:pets].count { |pet| pet.species == "Cat" } end + + should "filter by placement_type" do + create(:pet, placement_type: "Adoptable") + create(:pet, placement_type: "Fosterable") + create(:pet, placement_type: "Adoptable and Fosterable") + + get staff_pets_url, params: {q: {placement_type_eq: "0"}} + assert_response :success + + assert_equal 1, assigns[:pets].count + assert_equal 1, assigns[:pets].count { |pet| pet.placement_type == "Adoptable" } + end + + should "filter by application_paused" do + create(:pet, application_paused: false) + create(:pet, application_paused: true) + create(:pet, application_paused: true) + + get staff_pets_url, params: {q: {application_paused_eq: "true"}} + assert_response :success + + assert_equal 2, assigns[:pets].count + assert_equal 2, assigns[:pets].count { |pet| !!pet.application_paused } + end + + should "filter by published" do + create(:pet, published: true) + create(:pet, published: false) + create(:pet, published: false) + + get staff_pets_url, params: {q: {published_eq: "false"}} + assert_response :success + + assert_equal 2, assigns[:pets].count + assert_equal 2, assigns[:pets].count { |pet| !pet.published } + end end context "#new" do