Skip to content

Commit

Permalink
925 refactor adopter flow (rubyforgood#997)
Browse files Browse the repository at this point in the history
* Add route, controller, view for embedded google form

* Add external form url to organization model

* Update after sign in route

* Replace temp hard coded form url

* Add route for creating a form submission

* Add controller to create form submission, update external form view

* Remove index, null constraint on adopter applications for adopter foster account.

* Add person association, form_submission delegation

* Update strong params

* Update adopter application model associations, status enum, policy

* Add user delegation

* Fix applicant? method

* Replace adopter application, remove old profile policy reference

* Add translation

* Update tests

* Use service object to create form submissions

* Update organizations controller, form to accept external form url

* Update adopter applications to replace adopter foster account with form submission

* Update seeds

* Remove adopter foster account reference from pet, adoptable pets and application reviews controllers

* Add external form to dashboard

* Fix failing test

* Add form instructions

* Update registrations controller

* Fix conditional for show action

* Lint

* Make revisions

* Update seeds

* Update seeds

* Update adopter factory
  • Loading branch information
MooseCowBear authored Oct 9, 2024
1 parent c536b22 commit 180a937
Show file tree
Hide file tree
Showing 32 changed files with 164 additions and 56 deletions.
8 changes: 4 additions & 4 deletions app/controllers/organizations/adoptable_pets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def show
@pet = Pet.find(params[:id])
authorize! @pet, with: Organizations::AdoptablePetPolicy

if current_user
if current_user&.latest_form_submission
@adoption_application =
AdopterApplication.find_by(
pet: @pet,
form_submission: current_user.person.form_submission
pet_id: @pet.id,
form_submission_id: current_user.latest_form_submission.id
) ||
@pet.adopter_applications.build(
form_submission: current_user.person.form_submission
form_submission: current_user.latest_form_submission
)
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Organizations
module AdopterFosterer
class ExternalFormController < ApplicationController
layout :form_layout

def index
authorize! with: ExternalFormPolicy

@form_url = Current.organization.external_form_url
end

private

def form_layout
return "adopter_foster_dashboard" if params[:dashboard]

"application"
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def organization_params
:facebook_url,
:instagram_url,
:donation_url,
:external_form_url,
locations_attributes: %i[id city_town country province_state]
)
end
Expand Down
11 changes: 9 additions & 2 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ def new
respond_with resource
end

# MARK: only adopters are created through this route. Adopters need both the adoper role and a form submission to attach their applications to
def create
super do |resource|
if resource.persisted?
resource.add_role(:adopter, Current.organization)
resource.person.form_submissions.create
end
end
end
Expand Down Expand Up @@ -55,8 +57,13 @@ def account_update_params
end

def after_sign_up_path_for(resource)
allowed_to?(:index?, with: Organizations::AdopterFosterDashboardPolicy, context: {organization: Current.organization}) ?
adopter_fosterer_dashboard_index_path : root_path
return root_path unless allowed_to?(:index?, with: Organizations::AdopterFosterDashboardPolicy, context: {organization: Current.organization})

if Current.organization.external_form_url
adopter_fosterer_external_form_index_path
else
adopter_fosterer_dashboard_index_path
end
end

# check for id (i.e., record saved) and send mail
Expand Down
3 changes: 2 additions & 1 deletion app/models/adopter_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class AdopterApplication < ApplicationRecord
:adoption_pending,
:withdrawn,
:successful_applicant,
:adoption_made]
:adoption_made,
:awaiting_data]

# remove adoption_made status as not necessary for staff
def self.app_review_statuses
Expand Down
1 change: 1 addition & 0 deletions app/models/concerns/authorizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def permission?(name)
manage_likes
view_adopted_pets
read_pet_tasks
view_external_form
].freeze

FOSTERER_PERMISSIONS = %i[
Expand Down
2 changes: 2 additions & 0 deletions app/models/form_submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ class FormSubmission < ApplicationRecord

has_many :adopter_applications
has_many :form_answers, dependent: :destroy

delegate :user, to: :person
end
21 changes: 11 additions & 10 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
#
# Table name: organizations
#
# id :bigint not null, primary key
# donation_url :text
# email :string not null
# facebook_url :text
# instagram_url :text
# name :string not null
# phone_number :string
# slug :string not null
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint not null, primary key
# donation_url :text
# email :string not null
# external_form_url :text
# facebook_url :text
# instagram_url :text
# name :string not null
# phone_number :string
# slug :string not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
Expand Down
7 changes: 4 additions & 3 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class Person < ApplicationRecord

acts_as_tenant(:organization)

has_one :form_submission, dependent: :destroy
has_many :form_answers, through: :form_submission
has_many :adopter_applications, through: :form_submission
has_one :latest_form_submission, -> { order(created_at: :desc) }, class_name: "FormSubmission"
has_many :form_submissions, dependent: :destroy
has_many :form_answers, through: :form_submissions
has_many :adopter_applications, through: :form_submissions
has_many :likes, dependent: :destroy
has_many :liked_pets, through: :likes, source: :pet
has_many :matches # , dependent: :destroy
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class User < ApplicationRecord

before_save :downcase_email

delegate :latest_form_submission, to: :person

# get user accounts for staff in a given organization
def self.organization_staff(org_id)
User.includes(:staff_account)
Expand Down
6 changes: 3 additions & 3 deletions app/policies/adopter_application_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class AdopterApplicationPolicy < ApplicationPolicy
pre_check :verify_pet_appliable!, only: %i[create?]

relation_scope do |relation|
return relation.none unless user.person.form_submission
return relation.none unless user.latest_form_submission

relation.where(form_submission_id: user.person.form_submission.id)
relation.where(form_submission_id: user.latest_form_submission.id)
end

def update?
Expand Down Expand Up @@ -35,7 +35,7 @@ def already_applied?
end

def verify_form_submission!
deny! unless user.person.form_submission.present?
deny! unless user.latest_form_submission.present?
end

def verify_pet_appliable!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Organizations
module AdopterFosterer
class ExternalFormPolicy < ApplicationPolicy
def index?
permission?(:view_external_form)
end
end
end
end
7 changes: 7 additions & 0 deletions app/views/layouts/adopter_foster_dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@
<i class="fe fe-help-circle nav-icon"></i> FAQ
<% end %>
</li>
<% if Current.organization.external_form_url.present? %>
<li class="nav-item">
<%= active_link_to adopter_fosterer_external_form_index_path(dashboard: true), class: "nav-link" do %>
<i class="fe fe-help-circle nav-icon"></i> My Info
<% end %>
</li>
<% end %>
<li class="nav-item">
<%= active_link_to new_dev_contact_path, class: "nav-link" do %>
<i class="nav-icon fe fe-message-circle me-2"></i> Feedback
Expand Down
10 changes: 1 addition & 9 deletions app/views/organizations/adoptable_pets/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
%>
<% end %>
<% else %>
<% if AdopterApplication.exists?(form_submission: current_user.person.form_submission, pet: @pet) %>
<% if AdopterApplication.exists?(form_submission: current_user.latest_form_submission, pet: @pet) %>
<div class='d-flex align-items-center mt-3'>
<h4 class="me-2 mb-0">
<%= "#{t('organizations.adoptable_pets.show.application_status')} #{@adoption_application.human_enum_name(:status) || t('.status.default')}" %>
Expand All @@ -157,14 +157,6 @@
<%= image_tag('green_check.png', height: '21') %>
<% end %>
</div>
<% elsif %>
<%# FIXME: This section was missed during the remove of AdopterFosterProfile but I think it requires FormSubmission to be ready to refactor %>
<%# Once the form_submission CRUD and policy are created, should use conditional like the below %>
<%# elsif allowed_to?(:create?, FormSubmission) %>
<h4 class='mb-4 mt-3'>
<%= t('.complete_your_profile') %>
</h4>
<%= link_to t('.complete_my_profile'), "#", class: 'custom-btn-pink' %>
<% end %>
<% end %>
<% else %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class="d-flex flex-column align-items-center mb-4">
<div class="my-4 mx-8">
<% 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>
<% end %>
<%= link_to 'View pets', adoptable_pets_path, class: "btn btn-primary float-end" %>
</div>
<iframe id="external-form"
src=<%= @form_url%>
width="90%"
height="645"
>Loading…</iframe>
</div>
4 changes: 4 additions & 0 deletions app/views/organizations/staff/organizations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<div class="form-group">
<%= form.text_field :email, placeholder: "[email protected]", class: 'form-control' %>
</div>

<div class="form-group">
<%= form.text_field :external_form_url, label: "Adoption Form", placeholder: "https://example.com", class: 'form-control' %>
</div>
</form>
</div>

Expand Down
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ en:
adoption_pending: "Adoption Pending"
withdrawn: "Withdrawn"
successful_applicant: "Successful Applicant"
awaiting_data: "Awaiting Data"
tabs:
overview:
status:
Expand Down Expand Up @@ -626,6 +627,9 @@ en:
destroy:
success: "%{name} removed from your liked pets."
failed: "Error, %{name} not removed from your liked pets."
form_instructions:
index: "Please complete this form. %{organization_name} needs this information in order to process your application(s)."
dashboard: If you need to update your information, you can do so by filling out a new copy of this form. Please only submit a new copy if there has been a change. Unnecessary submissions may delay the processing of your application(s).
attachments:
purge:
success: "Attachment removed"
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
resources :files, only: [:index], module: :adopted_pets
resources :tasks, only: [:index], module: :adopted_pets
end
resources :external_form, only: %i[index]
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddExternalFormUrlToOrganizations < ActiveRecord::Migration[7.1]
def change
add_column :organizations, :external_form_url, :text
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ChangeAdopterFosterAccountNullOnAdopterApplications < ActiveRecord::Migration[7.1]
def change
remove_index :adopter_applications, column: [:adopter_foster_account_id], name: :index_adopter_applications_on_adopter_foster_account_id
change_column_null(:adopter_applications, :adopter_foster_account_id, true)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemovePetAdopterFosterAccountIndexFromAdopterApplications < ActiveRecord::Migration[7.1]
def change
remove_index :adopter_applications, column: [:pet_id, :adopter_foster_account_id], name: :index_adopter_applications_on_account_and_pet
end
end
1 change: 1 addition & 0 deletions db/schema.rb

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

11 changes: 6 additions & 5 deletions db/seeds/01_alta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
slug: "alta",
email: "[email protected]",
phone_number: "250 816 8212",
custom_page: CustomPage.new(hero: "Where every paw finds a home", about: "Alta was founded by an incredible group of ladies in April of 2020. Our initial goal was to have both a rescue and a spay/neuter clinic, however, we quickly realized that it would be more efficient to separate into two organizations.")
custom_page: CustomPage.new(hero: "Where every paw finds a home", about: "Alta was founded by an incredible group of ladies in April of 2020. Our initial goal was to have both a rescue and a spay/neuter clinic, however, we quickly realized that it would be more efficient to separate into two organizations."),
external_form_url: "https://docs.google.com/forms/d/e/1FAIpQLSf9bI-kboxyQQB5I1W5pt0R25u9pHoXI7o3jQHKu1P4K-61mA/viewform?embedded=true"
)

ActsAsTenant.with_tenant(@organization) do
Expand Down Expand Up @@ -75,7 +76,7 @@

@user_adopter_one.add_role(:adopter, @organization)

@adopter_one.create_form_submission!
@adopter_one.form_submissions.create!

@adopter_two = Person.create!(
email: "[email protected]",
Expand All @@ -94,7 +95,7 @@

@user_adopter_two.add_role(:adopter, @organization)

@adopter_two.create_form_submission!
@adopter_two.form_submissions.create!

@adopter_three = Person.create!(
email: "[email protected]",
Expand All @@ -113,7 +114,7 @@

@user_adopter_three.add_role(:adopter, @organization)

@adopter_three.create_form_submission!
@adopter_three.form_submissions.create!

@fosterer_one = Person.create!(
email: "[email protected]",
Expand Down Expand Up @@ -236,7 +237,7 @@

match_application = AdopterApplication.create!(
pet_id: Pet.first.id,
form_submission_id: @adopter_one.form_submission.id,
form_submission_id: @adopter_one.form_submissions.first.id,
status: :successful_applicant
)

Expand Down
8 changes: 4 additions & 4 deletions db/seeds/02_baja.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

@user_adopter_one.add_role(:adopter, @organization)

@adopter_one.create_form_submission!
@adopter_one.form_submissions.create!

@adopter_two = Person.create!(
email: "[email protected]",
Expand All @@ -94,7 +94,7 @@

@user_adopter_two.add_role(:adopter, @organization)

@adopter_two.create_form_submission!
@adopter_two.form_submissions.create!

@adopter_three = Person.create!(
email: "[email protected]",
Expand All @@ -113,7 +113,7 @@

@user_adopter_three.add_role(:adopter, @organization)

@adopter_three.create_form_submission!
@adopter_three.form_submissions.create!

@fosterer_one = Person.create!(
email: "[email protected]",
Expand Down Expand Up @@ -189,7 +189,7 @@

match_application = AdopterApplication.create!(
pet_id: Pet.first.id,
form_submission_id: @adopter_one.form_submission.id,
form_submission_id: @adopter_one.form_submissions.first.id,
status: :successful_applicant
)

Expand Down
Loading

0 comments on commit 180a937

Please sign in to comment.