Skip to content

Commit

Permalink
795 Add additional filters on staff pet view (rubyforgood#1046)
Browse files Browse the repository at this point in the history
* Add published/paused/placement_type filters to staff pet view

* Move ransack methods on Pet model to concern
  • Loading branch information
coalest authored Oct 9, 2024
1 parent 180a937 commit 380b703
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 33 deletions.
34 changes: 34 additions & 0 deletions app/models/concerns/pet_ransackable.rb
Original file line number Diff line number Diff line change
@@ -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
34 changes: 3 additions & 31 deletions app/models/pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# fk_rails_... (organization_id => organizations.id)
#
class Pet < ApplicationRecord
include PetRansackable
include PetTaskable

acts_as_tenant(:organization)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
16 changes: 14 additions & 2 deletions app/views/organizations/staff/pets/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@
<%= f.select :species_eq, Pet.species, {include_blank: 'All'}, class: "form-select" %>
</div>
<div class="form-group">
<%= 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" %>
</div>
<div class="form-group">
<%= f.label :placement_type_eq, "Placement type" %>
<%= f.select :placement_type_eq, Pet.placement_types, {include_blank: 'All'}, class: "form-select" %>
</div>
<div class="form-group">
<%= f.label :application_paused_eq, "Paused" %>
<%= f.select :application_paused_eq, [['Yes', true], ['No', false]], {include_blank: 'All'}, class: "form-select" %>
</div>
<div class="form-group">
<%= f.label :published_eq, "Published" %>
<%= f.select :published_eq, [['Yes', true], ['No', false]], {include_blank: 'All'}, class: "form-select" %>
</div>
</div>
<div class='d-flex flex-column w-100 w-md-auto flex-md-row gap-2'>
Expand Down
36 changes: 36 additions & 0 deletions test/controllers/organizations/staff/pets_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 380b703

Please sign in to comment.