Skip to content

Commit

Permalink
Change matches table to handle adoptions and fosters (#655)
Browse files Browse the repository at this point in the history
* Add migration for match_type to Matches

* Add enum to model and allow params in controller

* Update seeds with match_type

* Add match_type param to new adoption button

* Update tests with match_type

* Update policy tests with match_type

* Add test for enum validation
  • Loading branch information
jmilljr24 authored Apr 18, 2024
1 parent b493411 commit 3465174
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/organizations/matches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def destroy
private

def match_params
params.require(:match).permit(:pet_id, :adopter_foster_account_id)
params.require(:match).permit(:pet_id, :adopter_foster_account_id, :match_type, :start_date, :end_date)
end

def set_pet
Expand Down
5 changes: 5 additions & 0 deletions app/models/match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# Table name: matches
#
# id :bigint not null, primary key
# end_date :datetime
# match_type :integer not null
# start_date :datetime
# created_at :datetime not null
# updated_at :datetime not null
# adopter_foster_account_id :bigint not null
Expand Down Expand Up @@ -30,6 +33,8 @@ class Match < ApplicationRecord

after_create_commit :send_reminder

enum :match_type, [:adoption, :foster]

def send_reminder
MatchMailer.reminder(self).deliver_later
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
<!--create adoption button-->
<%= button_to "New Adoption", matches_path, method: :post,
params: {match:
{pet_id: app.pet_id, adopter_foster_account_id: app.adopter_foster_account_id}
{pet_id: app.pet_id,
adopter_foster_account_id: app.adopter_foster_account_id,
match_type: :adoption}
},
class: "btn btn-outline-primary",
data: { turbo_method: :post,
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20240418120309_add_match_type_to_matches.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddMatchTypeToMatches < ActiveRecord::Migration[7.1]
def change
add_column :matches, :match_type, :integer, null: false
add_column :matches, :start_date, :datetime
add_column :matches, :end_date, :datetime
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion db/seeds/01_alta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@

@match = Match.create!(
pet_id: Pet.first.id,
adopter_foster_account_id: @adopter_foster_account_one.id
adopter_foster_account_id: @adopter_foster_account_one.id,
match_type: :adoption
)

10.times do
Expand Down
3 changes: 2 additions & 1 deletion db/seeds/02_baja.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@

@match = Match.create!(
pet_id: Pet.first.id,
adopter_foster_account_id: @adopter_foster_account_one.id
adopter_foster_account_id: @adopter_foster_account_one.id,
match_type: :adoption
)

10.times do
Expand Down
5 changes: 3 additions & 2 deletions test/controllers/organizations/matches_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Organizations::MatchesControllerTest < ActionDispatch::IntegrationTest
@params = {
match: {
pet_id: pet.id,
adopter_foster_account_id: adopter_foster_account.id
adopter_foster_account_id: adopter_foster_account.id,
match_type: :adoption
}
}
end
Expand All @@ -37,7 +38,7 @@ class Organizations::MatchesControllerTest < ActionDispatch::IntegrationTest

context "#destroy" do
setup do
@match = create(:match)
@match = create(:match, match_type: :adoption)
end

should "be authorized" do
Expand Down
2 changes: 1 addition & 1 deletion test/factories/pets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
end

trait :adopted do
match { association :match, pet: instance }
match { association :match, pet: instance, match_type: :adoption }
adopter_applications { build_list(:adopter_application, 1, :successful_applicant, pet: instance) }
end

Expand Down
2 changes: 1 addition & 1 deletion test/mailers/match_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class MatchMailerTest < ActionMailer::TestCase
test "reminder" do
match = create(:match)
match = create(:match, match_type: :adoption)
email = MatchMailer.reminder(match)

assert_emails 1 do
Expand Down
4 changes: 4 additions & 0 deletions test/models/match_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class MatchTest < ActiveSupport::TestCase
should belong_to(:adopter_foster_account)
end

context "validations" do
should define_enum_for(:match_type)
end

setup do
@match = build_stubbed(:match)
end
Expand Down
6 changes: 3 additions & 3 deletions test/policies/organizations/match_policy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Organizations::MatchPolicyTest < ActiveSupport::TestCase

context "#destroy?" do
setup do
@match = create(:match)
@match = create(:match, match_type: :adoption)
@policy = -> {
Organizations::MatchPolicy.new(@match, user: @user)
}
Expand Down Expand Up @@ -146,7 +146,7 @@ class Organizations::MatchPolicyTest < ActiveSupport::TestCase
context "when match belongs to a different organization" do
setup do
ActsAsTenant.with_tenant(create(:organization)) do
@match = create(:match)
@match = create(:match, match_type: :adoption)
end
end

Expand Down Expand Up @@ -180,7 +180,7 @@ class Organizations::MatchPolicyTest < ActiveSupport::TestCase
context "when match belongs to a different organization" do
setup do
ActsAsTenant.with_tenant(create(:organization)) do
@match = create(:match)
@match = create(:match, match_type: :adoption)
end
end

Expand Down

0 comments on commit 3465174

Please sign in to comment.