From eab4d662f71d3e742f20d46f91ba6abb17ac3501 Mon Sep 17 00:00:00 2001 From: Yuji Kosakowski Date: Fri, 27 Dec 2024 19:31:36 -0700 Subject: [PATCH 1/2] Add unique species scope and test --- app/models/pet.rb | 3 +++ test/models/pet_test.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/app/models/pet.rb b/app/models/pet.rb index 9db914102..552477a70 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -95,6 +95,9 @@ class Pet < ApplicationRecord query.where(published: true).order(:breed).distinct.pluck(:breed) } + # Returns an array of unique species of an organization + scope :unique_species, -> { distinct.pluck(:species) } + attr_writer :toggle # check if pet has any applications with adoption pending status diff --git a/test/models/pet_test.rb b/test/models/pet_test.rb index 9b6f4fa26..6a559d0f9 100644 --- a/test/models/pet_test.rb +++ b/test/models/pet_test.rb @@ -83,6 +83,19 @@ class PetTest < ActiveSupport::TestCase assert_not Pet.with_photo.include?(pet_without_image) end end + + context ".unique_species" do + should "returns an array of unique species" do + create(:pet, species: "Dog") + create(:pet, species: "Cat") + create(:pet, species: "Dog") + + unique_species = Pet.unique_species + + assert_equal ["Cat", "Dog"], unique_species.sort + assert_equal 1, unique_species.count("Dog") + end + end end context "#has_adoption_pending?" do From 700e7cc64c9c99361163f2b3a5fa0f0354643975 Mon Sep 17 00:00:00 2001 From: Yuji Kosakowski Date: Fri, 27 Dec 2024 19:47:00 -0700 Subject: [PATCH 2/2] Ran linter --- app/models/pet.rb | 2 +- test/models/pet_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/pet.rb b/app/models/pet.rb index 552477a70..8acc5181d 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -97,7 +97,7 @@ class Pet < ApplicationRecord # Returns an array of unique species of an organization scope :unique_species, -> { distinct.pluck(:species) } - + attr_writer :toggle # check if pet has any applications with adoption pending status diff --git a/test/models/pet_test.rb b/test/models/pet_test.rb index 6a559d0f9..77df1e66f 100644 --- a/test/models/pet_test.rb +++ b/test/models/pet_test.rb @@ -89,9 +89,9 @@ class PetTest < ActiveSupport::TestCase create(:pet, species: "Dog") create(:pet, species: "Cat") create(:pet, species: "Dog") - + unique_species = Pet.unique_species - + assert_equal ["Cat", "Dog"], unique_species.sort assert_equal 1, unique_species.count("Dog") end