diff --git a/app/controllers/organizations/adoptable_pets_controller.rb b/app/controllers/organizations/adoptable_pets_controller.rb index 0b60f6d20..f3273f089 100644 --- a/app/controllers/organizations/adoptable_pets_controller.rb +++ b/app/controllers/organizations/adoptable_pets_controller.rb @@ -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 diff --git a/app/controllers/organizations/adopter_fosterer/external_form_controller.rb b/app/controllers/organizations/adopter_fosterer/external_form_controller.rb new file mode 100644 index 000000000..4fe51a9e4 --- /dev/null +++ b/app/controllers/organizations/adopter_fosterer/external_form_controller.rb @@ -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 diff --git a/app/controllers/organizations/staff/organizations_controller.rb b/app/controllers/organizations/staff/organizations_controller.rb index e0bd8644a..ac8a497d2 100644 --- a/app/controllers/organizations/staff/organizations_controller.rb +++ b/app/controllers/organizations/staff/organizations_controller.rb @@ -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 diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 6209f416b..5c0822429 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -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 @@ -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 diff --git a/app/models/adopter_application.rb b/app/models/adopter_application.rb index 31a2484d7..29b1a5a2c 100644 --- a/app/models/adopter_application.rb +++ b/app/models/adopter_application.rb @@ -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 diff --git a/app/models/concerns/authorizable.rb b/app/models/concerns/authorizable.rb index a82f54ebf..76ff79366 100644 --- a/app/models/concerns/authorizable.rb +++ b/app/models/concerns/authorizable.rb @@ -19,6 +19,7 @@ def permission?(name) manage_likes view_adopted_pets read_pet_tasks + view_external_form ].freeze FOSTERER_PERMISSIONS = %i[ diff --git a/app/models/form_submission.rb b/app/models/form_submission.rb index 07d5b4662..3c0db0f6a 100644 --- a/app/models/form_submission.rb +++ b/app/models/form_submission.rb @@ -32,4 +32,6 @@ class FormSubmission < ApplicationRecord has_many :adopter_applications has_many :form_answers, dependent: :destroy + + delegate :user, to: :person end diff --git a/app/models/organization.rb b/app/models/organization.rb index 46ca35fe0..54f296126 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -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 # diff --git a/app/models/person.rb b/app/models/person.rb index ed4ddb669..1ed6734f3 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 4b32dee16..3696be4ec 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) diff --git a/app/policies/adopter_application_policy.rb b/app/policies/adopter_application_policy.rb index eb124d81c..9d911d504 100644 --- a/app/policies/adopter_application_policy.rb +++ b/app/policies/adopter_application_policy.rb @@ -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? @@ -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! diff --git a/app/policies/organizations/adopter_fosterer/external_form_policy.rb b/app/policies/organizations/adopter_fosterer/external_form_policy.rb new file mode 100644 index 000000000..a52533cae --- /dev/null +++ b/app/policies/organizations/adopter_fosterer/external_form_policy.rb @@ -0,0 +1,9 @@ +module Organizations + module AdopterFosterer + class ExternalFormPolicy < ApplicationPolicy + def index? + permission?(:view_external_form) + end + end + end +end diff --git a/app/views/layouts/adopter_foster_dashboard.html.erb b/app/views/layouts/adopter_foster_dashboard.html.erb index ac40c2b36..c28b5f3c4 100644 --- a/app/views/layouts/adopter_foster_dashboard.html.erb +++ b/app/views/layouts/adopter_foster_dashboard.html.erb @@ -82,6 +82,13 @@ FAQ <% end %> + <% if Current.organization.external_form_url.present? %> + + <% end %>