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" %>