-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
900 - AdopterFoster: Add adopted pets (#910)
* Adopted Pets controller, Route, Policy setup * Basic layout for adopted pets * Added functionality to display files for adopted pets * Test cases for Adopted pets * Lint fix * Code cleanup by adding files view in shared folder * Removed unwanted helper file * Moves attachmet files logic to new controller for code clean up * Lint fix * Updated adopted_pets query to fetch details from Match model * Test fix to create match record * Fix policy test * Lint fix * Removed integration and system test files for adopted_pets * Controller spec * Lint fix * Fix failing test * Lint fix
- Loading branch information
1 parent
2f6d742
commit 3470658
Showing
14 changed files
with
277 additions
and
8 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
app/controllers/organizations/adopter_fosterer/adopted_pets/files_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Organizations | ||
module AdopterFosterer | ||
module AdoptedPets | ||
class FilesController < Organizations::BaseController | ||
skip_verify_authorized only: %i[index] | ||
|
||
def index | ||
@pet = Pet.find(params[:adopted_pet_id]) | ||
respond_to do |format| | ||
format.html # Regular HTML response | ||
format.turbo_stream do | ||
render turbo_stream: turbo_stream.replace( | ||
"pet_files", | ||
partial: "organizations/shared/file_attachment_table", | ||
locals: {pet: @pet} | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
app/controllers/organizations/adopter_fosterer/adopted_pets_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module Organizations | ||
module AdopterFosterer | ||
class AdoptedPetsController < Organizations::BaseController | ||
before_action :context_authorize! | ||
layout "adopter_foster_dashboard" | ||
|
||
def index | ||
@adopted_pets = authorized_scope(Match.adoptions, with: Organizations::AdopterFosterer::MatchPolicy) | ||
end | ||
|
||
private | ||
|
||
def context_authorize! | ||
authorize! with: Organizations::AdopterFosterer::MatchPolicy | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static targets = ["card"] | ||
|
||
connect() { | ||
this.defaultBackgroundColor = "bg-white" | ||
this.selectedBackgroundColor = "bg-gray-300" | ||
} | ||
|
||
selectCard(event) { | ||
this.cardTargets.forEach(card => { | ||
card.classList.remove(this.selectedBackgroundColor) | ||
card.classList.add(this.defaultBackgroundColor) | ||
}) | ||
|
||
const selectedCard = event.currentTarget.querySelector(".card") | ||
selectedCard.classList.remove(this.defaultBackgroundColor) | ||
selectedCard.classList.add(this.selectedBackgroundColor) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/policies/organizations/adopter_fosterer/match_policy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Organizations | ||
module AdopterFosterer | ||
class MatchPolicy < ApplicationPolicy | ||
pre_check :verify_adopter_foster_account! | ||
|
||
relation_scope do |relation| | ||
relation.where(adopter_foster_account_id: user.adopter_foster_account.id) | ||
end | ||
|
||
def index? | ||
permission?(:view_adopted_pets) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
app/views/organizations/adopter_fosterer/adopted_pets/index.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<%= render DashboardPageComponent.new do |c| %> | ||
<% c.with_header_title { "Adopted Pets" } %> | ||
<% c.with_body do %> | ||
<div data-controller="card"> | ||
<div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-4 mb-5"> | ||
<% @adopted_pets.each_with_index do |adopted_pet, index| %> | ||
<div class="col"> | ||
<%= link_to adopter_fosterer_adopted_pet_files_path(adopted_pet.pet), data: { turbo_stream: true, action: "click->card#selectCard" } do %> | ||
<div class="card h-100 bg-white border-0 shadow-sm" data-card-target="card"> | ||
<div class="card-body d-flex align-items-center"> | ||
<div class="me-3 flex-shrink-0" style="width: 50px; height: 50px;"> | ||
<%= image_tag(adopted_pet.pet.images.attached? ? adopted_pet.pet.images.first : 'coming_soon.jpg', | ||
class: 'rounded-circle', | ||
style: "width: 100%; height: 100%; object-fit: cover;") %> | ||
</div> | ||
<h5 class="card-title mb-0"><%= adopted_pet.pet.name %></h5> | ||
</div> | ||
</div> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> | ||
<div id="pet_files"> | ||
<!-- This will be replaced with the pet files table when a pet is clicked --> | ||
</div> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<div class="mb-4"> | ||
<%= render "attachment_form", instance: @pet, title: 'Files', url: attach_files_staff_pet_path(@pet), attachment_type: 'files' %> | ||
<%= render "file_attachment_table", pet: @pet %> | ||
<%= render "organizations/shared/file_attachment_table", pet: @pet %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
test/controllers/organizations/adopter_fosterer/adopted_pets_controller_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require "test_helper" | ||
require "action_policy/test_helper" | ||
|
||
class Organizations::AdopterFosterer::AdoptedPetsControllerTest < ActionDispatch::IntegrationTest | ||
context "authorization" do | ||
include ActionPolicy::TestHelper | ||
|
||
setup do | ||
@organization = ActsAsTenant.current_tenant | ||
@adopter_foster_account = create(:adopter_foster_account) | ||
sign_in @adopter_foster_account.user | ||
end | ||
|
||
context "#index" do | ||
should "be authorized" do | ||
get adopter_fosterer_adopted_pets_url | ||
assert_response :success | ||
end | ||
|
||
should "have authorized scope" do | ||
ActsAsTenant.with_tenant(@organization) do | ||
assert_have_authorized_scope( | ||
type: :active_record_relation, | ||
with: Organizations::AdopterFosterer::MatchPolicy | ||
) do | ||
get adopter_fosterer_adopted_pets_url | ||
end | ||
end | ||
end | ||
|
||
should "return only adoption matches for the user's adopter_fosterer_account" do | ||
ActsAsTenant.with_tenant(@organization) do | ||
user_adoption_match = create(:match, :adoption, adopter_foster_account: @adopter_foster_account, organization: @organization) | ||
other_account_adoption_match = create(:match, :adoption, organization: @organization) | ||
other_org_adoption_match = create(:match, :adoption) | ||
|
||
get adopter_fosterer_adopted_pets_url | ||
|
||
assert_includes assigns(:adopted_pets), user_adoption_match | ||
assert_not_includes assigns(:adopted_pets), other_account_adoption_match | ||
assert_not_includes assigns(:adopted_pets), other_org_adoption_match | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
test/policies/organizations/adopter_fosterer/match_policy_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
require "test_helper" | ||
|
||
class Organizations::AdopterFosterer::MatchPolicyTest < ActiveSupport::TestCase | ||
include PetRescue::PolicyAssertions | ||
|
||
setup do | ||
@organization = ActsAsTenant.current_tenant | ||
@policy = -> { | ||
Organizations::AdopterFosterer::MatchPolicy.new( | ||
Match, user: @user, organization: @organization | ||
) | ||
} | ||
end | ||
|
||
context "relation_scope" do | ||
setup do | ||
@user = create(:user) | ||
@adopter_foster_account = create(:adopter_foster_account, user: @user, organization: @organization) | ||
@pet = create(:pet) | ||
@adopted_application = create(:adopter_application, adopter_foster_account: @adopter_foster_account, pet: @pet, status: :adoption_made) | ||
@match = create(:match, adopter_foster_account: @adopter_foster_account, pet: @pet, match_type: :adoption) | ||
|
||
@other_user = create(:user) | ||
@other_account = create(:adopter_foster_account, user: @other_user, organization: @organization) | ||
@other_pet = create(:pet) | ||
create(:adopter_application, adopter_foster_account: @other_account, pet: @other_pet, status: :adoption_made) | ||
@other_match = create(:match, adopter_foster_account: @other_account, pet: @other_pet, match_type: :adoption) | ||
|
||
ActsAsTenant.with_tenant(create(:organization)) do | ||
create(:match, adopter_foster_account: @adopter_foster_account, match_type: :adoption) | ||
end | ||
end | ||
|
||
should "return only the user's adopted pets" do | ||
scoped = @policy.call.apply_scope(Match.all, type: :active_record_relation) | ||
|
||
assert_equal 1, scoped.count | ||
assert_includes scoped, @match | ||
end | ||
end | ||
|
||
context "rules" do | ||
context "#index?" do | ||
setup do | ||
@action = -> { @policy.call.apply(:index?) } | ||
end | ||
|
||
context "when user is nil" do | ||
setup { @user = nil } | ||
should "return false" do | ||
assert_equal false, @action.call | ||
end | ||
end | ||
|
||
context "when user is adopter" do | ||
setup do | ||
@user = create(:adopter) | ||
@adopter_foster_account = create(:adopter_foster_account, user: @user, organization: @organization) | ||
end | ||
|
||
should "return true" do | ||
assert_equal true, @action.call | ||
end | ||
end | ||
|
||
context "when user is staff" do | ||
setup { @user = create(:admin) } | ||
should "return false" do | ||
assert_equal false, @action.call | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "pre_check" do | ||
setup do | ||
@user = create(:user) | ||
@action = -> { @policy.call.apply(:index?) } | ||
end | ||
|
||
context "when user has an adopter_foster_account" do | ||
setup do | ||
create(:adopter_foster_account, user: @user, organization: @organization) | ||
end | ||
|
||
should "not raise an error" do | ||
assert_nothing_raised { @action.call } | ||
end | ||
end | ||
end | ||
end |