Skip to content

Commit

Permalink
#1069 Adoptable Pets Refactor - show collection by species (#1070)
Browse files Browse the repository at this point in the history
* add species url param

* remove species select from index

* update select for breed

* add species param to breed select

* add pet species button's to nav

* add species link to home page for logged in user

* adjust font size

* change dropdown to individual buttons

* change external form page to use individual buttons

* remove unused page

* use module

* change logout path

* i18n

* more i18n

* add species to header

* change link helper

* update tests for species specific / remove brital tests

* change test to use module

* add set_species and move breeds method to Pet model

* remove useless conditional

* update test

* comments
  • Loading branch information
jmilljr24 authored Oct 23, 2024
1 parent 6fb4c22 commit 64e94fd
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 246 deletions.
2 changes: 1 addition & 1 deletion app/controllers/concerns/organization_scopable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ def after_sign_in_path_for(resource_or_scope)
end

def after_sign_out_path_for(resource_or_scope)
adoptable_pets_path
root_path
end
end
92 changes: 52 additions & 40 deletions app/controllers/organizations/adoptable_pets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,60 @@
class Organizations::AdoptablePetsController < Organizations::BaseController
include ::Pagy::Backend

skip_before_action :authenticate_user!
skip_verify_authorized only: %i[index]
before_action :set_likes, only: %i[index show],
if: -> { allowed_to?(:index?, Like) }
helper_method :get_animals

def index
@q = authorized_scope(Pet.unadopted, with: Organizations::AdoptablePetPolicy).ransack(params[:q])
@pagy, paginated_adoptable_pets = pagy(
@q.result.includes(:adopter_applications, :matches, images_attachments: :blob),
limit: 9
)
@pets = paginated_adoptable_pets
end
module Organizations
class AdoptablePetsController < Organizations::BaseController
include ::Pagy::Backend

skip_before_action :authenticate_user!
skip_verify_authorized only: %i[index]
before_action :set_likes, only: %i[index show],
if: -> { allowed_to?(:index?, Like) }
before_action :set_species, only: %i[index]

def index
@q = authorized_scope(
case @species
when "dog"
Pet.Dog.unadopted
when "cat"
Pet.Cat.unadopted
else
redirect_back_or_to root_path and return
end,
with: Organizations::AdoptablePetPolicy
).ransack(params[:q])

def show
@adoptable_pet_info = CustomPage.first&.adoptable_pet_info
@pet = Pet.find(params[:id])
authorize! @pet, with: Organizations::AdoptablePetPolicy

if current_user&.latest_form_submission
@adoption_application =
AdopterApplication.find_by(
pet_id: @pet.id,
form_submission_id: current_user.latest_form_submission.id
) ||
@pet.adopter_applications.build(
form_submission: current_user.latest_form_submission
)
@pagy, paginated_adoptable_pets = pagy(
@q.result.includes(:adopter_applications, :matches, images_attachments: :blob),
limit: 9
)

@pets = paginated_adoptable_pets
end
end

private
def show
@adoptable_pet_info = CustomPage.first&.adoptable_pet_info
@pet = Pet.find(params[:id])
authorize! @pet, with: Organizations::AdoptablePetPolicy

def get_animals
Pet.species.keys.to_h do |s|
[s, authorized_scope(Pet.where(species: s).distinct.order(:breed), with: Organizations::AdoptablePetPolicy).pluck(:breed)]
if current_user&.latest_form_submission
@adoption_application =
AdopterApplication.find_by(
pet_id: @pet.id,
form_submission_id: current_user.latest_form_submission.id
) ||
@pet.adopter_applications.build(
form_submission: current_user.latest_form_submission
)
end
end
end

def set_likes
likes = current_user.person.likes
@likes_by_id = likes.index_by(&:pet_id)
private

def set_likes
likes = current_user.person.likes
@likes_by_id = likes.index_by(&:pet_id)
end

def set_species
@species = params[:species]
end
end
end
6 changes: 6 additions & 0 deletions app/models/pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ class Pet < ApplicationRecord
joins(:adopter_applications).where(adopter_applications: {status: status_filter})
}

# Returns an array of all published breeds; Can be filtered by species ('dog')
scope :breeds, ->(species = nil) {
query = species ? where(species: species.capitalize) : all
query.where(published: true).order(:breed).distinct.pluck(:breed)
}

attr_writer :toggle

# check if pet has any applications with adoption pending status
Expand Down
23 changes: 21 additions & 2 deletions app/views/layouts/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,27 @@
<!-- Collapse -->
<div class="justify-content-end collapse navbar-collapse" id="navbar-default3">
<ul class="navbar-nav">
<li class='nav-item'>
<%= active_link_to t('.pets'), adoptable_pets_path, class: 'nav-link' %>
<li class="nav-item dropdown">
<a
class="nav-link dropdown-toggle"
href="#"
role="button"
data-bs-toggle="dropdown"
aria-expanded="false"
>
<%= t(".pets") %>
</a>
<ul class="dropdown-menu">
<li>
<%= link_to t("general.dogs"),
adoptable_pets_path(species: "dog"),
class: "dropdown-item" %></li>

<li>
<%= link_to t("general.cats"),
adoptable_pets_path(species: "cat"),
class: "dropdown-item" %></li>
</ul>
</li>
<li class='nav-item'>
<%= active_link_to t('.contact'), new_contact_path, class: 'nav-link' %>
Expand Down
30 changes: 17 additions & 13 deletions app/views/organizations/adoptable_pets/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<!--page heading-->
<header class="pt-5 pb-5" >
<header class="pt-5 pb-5">
<div class="container">
<div class="text-center">
<h1 class="section-heading text-uppercase underline">
<%= t('.header') %>
</h1>
</div>
</div> <!--container-->
<div class="text-center">
<h1 class="section-heading text-uppercase underline">
<%= t("general.#{@species.pluralize}") %>
<%= t(".header") %>
</h1>
</div>
</div>
<!--container-->
</header>

<!--pet tiles-->
Expand All @@ -26,22 +28,24 @@
<%= f.label :sex_eq, "Sex" %>
<%= f.select :sex_eq, ["Male", "Female"], {include_blank: 'All'}, class: "form-select" %>
</div>
<div class="form-group">
<%= f.label :species_eq, "Species" %>
<%= f.select :species_eq, Pet.species, {include_blank: 'All'}, class: "form-select" %>
</div>
<div class="form-group">
<%= f.label :breed_eq, "Breed" %>
<%= f.select :breed_eq, get_animals, {include_blank: 'All'}, class: "form-select" %>
<%= f.select :breed_eq,
Pet.breeds(@species),
{ include_blank: "All" },
class: "form-select" %>
</div>
<div class="form-group">
<%= f.label :ransack_birth_date, "Age" %>
<%= f.select :ransack_birth_date, age_ranges, {include_blank: 'All'}, class: "form-select"%>
</div>
</div>
<div class='d-flex w-md-auto flex-md-row align-items-end' style="align-self: flex-end;">
<%= hidden_field_tag "species", @species %>
<%= f.submit class: "btn btn-primary" %>
<%= link_to "Clear filters", adoptable_pets_path, class: "btn btn-default"%>
<%= link_to "Clear filters",
adoptable_pets_path(species: @species),
class: "btn btn-default" %>
</div>
</div>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@
<% if params[:dashboard] %>
<p><%= t("organizations.adopter_fosterer.form_instructions.dashboard") %></p>
<% else %>
<p><%= t("organizations.adopter_fosterer.form_instructions.index", organization_name: Current.tenant.name) %></p>
<p><%= t(
"organizations.adopter_fosterer.form_instructions.index",
organization_name: Current.tenant.name,
) %></p>
<% end %>
<%= link_to 'View pets', adoptable_pets_path, class: "btn btn-primary float-end" %>
<div class="d-flex justify-content-end gap-2 pb-2">
<%= link_to " #{t("general.view")} #{t("general.dogs")}",
adoptable_pets_path(species: "dog"),
class: "btn btn-primary" %>
<%= link_to " #{t("general.view")} #{t("general.cats")}",
adoptable_pets_path(species: "cat"),
class: "btn btn-primary" %>
</div>
</div>
<iframe id="external-form"
src=<%= @form_url%>
Expand Down
17 changes: 10 additions & 7 deletions app/views/organizations/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@
</div>
<div class="d-flex justify-content-center">
<% if user_signed_in? %>
<%= link_to "Browse Pets", adoptable_pets_path, class: "btn btn-primary btn-lg rounded-1 ms-2" %>
<%= link_to "#{t("general.browse")} #{t("general.dogs")}",
adoptable_pets_path(species: "dog"),
class: "btn btn-primary btn-lg rounded-1 ms-2" %>
<%= link_to "#{t("general.browse")} #{t("general.cats")}",
adoptable_pets_path(species: "cat"),
class: "btn btn-primary btn-lg rounded-1 ms-2" %>
<% else %>
<%= link_to "Become an Adopter", new_user_registration_path, class: "btn btn-primary btn-lg rounded-1 ms-2" %>
<%= link_to t("general.become_an_adopter"),
new_user_registration_path,
class: "btn btn-primary btn-lg rounded-1 ms-2" %>
<% end %>
</div>
</div>
Expand Down Expand Up @@ -54,11 +62,6 @@
<% end %>
<% end %>
</div>
<!--//? Adopt all link -->
<div class="d-flex justify-content-end align-items-center mt-3">
<%= link_to "Browse All Pets", adoptable_pets_path, class: "text-primary fs-3 me-1" %>
<svg xmlns="http://www.w3.org/2000/svg" height="14" width="12.25" viewBox="0 0 448 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path fill="#5E3FCB" d="M438.6 278.6c12.5-12.5 12.5-32.8 0-45.3l-160-160c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L338.8 224 32 224c-17.7 0-32 14.3-32 32s14.3 32 32 32l306.7 0L233.4 393.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0l160-160z"/></svg>
</div>
</div>
</section>

Expand Down
Loading

0 comments on commit 64e94fd

Please sign in to comment.