Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

851 create form submission model #856

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/models/adopter_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,29 @@
# created_at :datetime not null
# updated_at :datetime not null
# adopter_foster_account_id :bigint not null
# form_submission_id :bigint not null
# organization_id :bigint not null
# pet_id :bigint not null
#
# Indexes
#
# index_adopter_applications_on_account_and_pet (pet_id,adopter_foster_account_id) UNIQUE
# index_adopter_applications_on_adopter_foster_account_id (adopter_foster_account_id)
# index_adopter_applications_on_form_submission_id (form_submission_id)
# index_adopter_applications_on_organization_id (organization_id)
# index_adopter_applications_on_pet_id (pet_id)
#
# Foreign Keys
#
# fk_rails_... (adopter_foster_account_id => adopter_foster_accounts.id)
# fk_rails_... (form_submission_id => form_submissions.id)
# fk_rails_... (pet_id => pets.id)
#
class AdopterApplication < ApplicationRecord
acts_as_tenant(:organization)
belongs_to :pet, touch: true
belongs_to :adopter_foster_account
belongs_to :form_submission

broadcasts_refreshes

Expand Down
22 changes: 13 additions & 9 deletions app/models/custom_form/submitted_answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,32 @@
#
# Table name: submitted_answers
#
# id :bigint not null, primary key
# question_snapshot :json not null
# value :json not null
# created_at :datetime not null
# updated_at :datetime not null
# question_id :bigint not null
# user_id :bigint not null
# id :bigint not null, primary key
# question_snapshot :json not null
# value :json not null
# created_at :datetime not null
# updated_at :datetime not null
# form_submission_id :bigint not null
# question_id :bigint not null
# user_id :bigint not null
#
# Indexes
#
# index_submitted_answers_on_question_id (question_id)
# index_submitted_answers_on_user_id (user_id)
# index_submitted_answers_on_form_submission_id (form_submission_id)
# index_submitted_answers_on_question_id (question_id)
# index_submitted_answers_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (form_submission_id => form_submissions.id)
# fk_rails_... (question_id => questions.id)
# fk_rails_... (user_id => users.id)
#
module CustomForm
class SubmittedAnswer < ApplicationRecord
belongs_to :question
belongs_to :user
belongs_to :form_submission

has_one :form, class_name: "CustomForm::Form", through: :question
has_one :organization, through: :form
Expand Down
28 changes: 28 additions & 0 deletions app/models/form_submission.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# == Schema Information
#
# Table name: form_submissions
#
# id :bigint not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :bigint not null
# person_id :bigint not null
#
# Indexes
#
# index_form_submissions_on_organization_id (organization_id)
# index_form_submissions_on_person_id (person_id)
#
# Foreign Keys
#
# fk_rails_... (organization_id => organizations.id)
# fk_rails_... (person_id => people.id)
#

class FormSubmission < ApplicationRecord
kasugaijin marked this conversation as resolved.
Show resolved Hide resolved
acts_as_tenant(:organization)
belongs_to :person

has_many :adopter_applications
has_many :submitted_answers, class_name: "CustomForm::SubmittedAnswer"
end
1 change: 1 addition & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ class Organization < ApplicationRecord

has_one :profile, dependent: :destroy, class_name: "OrganizationProfile", required: true
has_one :location, through: :profile
has_one :form_submission, dependent: :destroy
has_one :custom_page, dependent: :destroy
end
1 change: 1 addition & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Person < ApplicationRecord
include Avatarable

acts_as_tenant(:organization)
has_one :form_submission, dependent: :destroy

validates :name, presence: true
validates :email, presence: true,
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20240628222914_create_form_submissions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateFormSubmissions < ActiveRecord::Migration[7.1]
def change
create_table :form_submissions do |t|
t.references :person, null: false, foreign_key: true
t.references :organization, null: false, foreign_key: true

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFormSubmissionToSubmittedAnswers < ActiveRecord::Migration[7.1]
def change
add_reference :submitted_answers, :form_submission, null: false, foreign_key: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFormSubmissionToAdopterApplications < ActiveRecord::Migration[7.1]
def change
add_reference :adopter_applications, :form_submission, null: false, foreign_key: true
end
end
19 changes: 18 additions & 1 deletion db/schema.rb

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

7 changes: 5 additions & 2 deletions db/seeds/01_alta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,17 +401,20 @@
end_date: upcoming_end_date
)

@form_submission = FormSubmission.new(organization: @organization, person: Person.new(name: "John Doe", email: "[email protected]"))

10.times do
adopter_application = AdopterApplication.new(
notes: Faker::Lorem.paragraph,
profile_show: true,
status: rand(0..4),
adopter_foster_account: AdopterFosterAccount.all.sample,
pet: Pet.all.sample
pet: Pet.all.sample,
form_submission: @form_submission
)

# Prevent duplicate adopter applications.
redo if AdopterApplication.where(pet_id: adopter_application.pet_id,
redo if AdopterApplication.where(pet_id: adopter_application.pet_id, form_submission_id: adopter_application.form_submission_id,
adopter_foster_account_id: adopter_application.adopter_foster_account_id).exists?

if adopter_application.valid?
Expand Down
5 changes: 4 additions & 1 deletion db/seeds/02_baja.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,16 @@
end_date: upcoming_end_date
)

@form_submission = FormSubmission.new(organization: @organization, person: Person.new(name: "John Doe", email: "[email protected]"))

10.times do
adopter_application = AdopterApplication.new(
notes: Faker::Lorem.paragraph,
profile_show: true,
status: rand(0..4),
adopter_foster_account: AdopterFosterAccount.all.sample,
pet: Pet.all.sample
pet: Pet.all.sample,
form_submission: @form_submission
)

# Prevent duplicate adopter applications.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class Organizations::AdopterFosterer::AdopterApplicationsControllerTest < Action

should "count the total number of applications" do
organization = ActsAsTenant.current_tenant
form_submission = create(:form_submission)
adopter_foster_account = create(:adopter_foster_account, user: @user, organization: organization)
create_list(:adopter_application, 2, adopter_foster_account: adopter_foster_account)
create_list(:adopter_application, 2, adopter_foster_account: adopter_foster_account, form_submission: form_submission)

get adopter_fosterer_dashboard_index_path

Expand Down Expand Up @@ -68,7 +69,8 @@ class Organizations::AdopterFosterer::AdopterApplicationsControllerTest < Action

context "#update" do
setup do
@adopter_application = create(:adopter_application, user: @user)
@form_submission = create(:form_submission)
@adopter_application = create(:adopter_application, user: @user, form_submission: @form_submission)
@params = {adopter_application: {
status: "withdrawn"
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Organizations::Staff::AdoptionApplicationReviewsControllerTest < ActionDis

setup do
@organization = ActsAsTenant.current_tenant
@adopter_application = create(:adopter_application)
@form_submission = create(:form_submission)
@adopter_application = create(:adopter_application, form_submission: @form_submission)

user = create(:staff)
sign_in user
Expand Down Expand Up @@ -88,8 +89,8 @@ class Organizations::Staff::AdoptionApplicationReviewsControllerTest < ActionDis
@pet2 = create(:pet, name: "Tycho")
adopter_foster_account1 = create(:adopter_foster_account, :with_profile)
adopter_foster_account2 = create(:adopter_foster_account, :with_profile)
create(:adopter_application, pet: @pet1, adopter_foster_account: adopter_foster_account1)
create(:adopter_application, pet: @pet2, adopter_foster_account: adopter_foster_account2)
create(:adopter_application, pet: @pet1, adopter_foster_account: adopter_foster_account1, form_submission: create(:form_submission))
create(:adopter_application, pet: @pet2, adopter_foster_account: adopter_foster_account2, form_submission: create(:form_submission))
end

should "return applications for a specific pet name" do
Expand All @@ -107,8 +108,8 @@ class Organizations::Staff::AdoptionApplicationReviewsControllerTest < ActionDis
user: create(:user, first_name: "David", last_name: "Attenborough"))
adopter_foster_account2 = create(:adopter_foster_account, :with_profile,
user: create(:user, first_name: "Jane", last_name: "Goodall"))
create(:adopter_application, pet: @pet, adopter_foster_account: adopter_foster_account1)
create(:adopter_application, pet: @pet, adopter_foster_account: adopter_foster_account2)
create(:adopter_application, pet: @pet, adopter_foster_account: adopter_foster_account1, form_submission: create(:form_submission))
create(:adopter_application, pet: @pet, adopter_foster_account: adopter_foster_account2, form_submission: create(:form_submission))
end

should "return applications for a specific applicant name" do
Expand All @@ -124,8 +125,8 @@ class Organizations::Staff::AdoptionApplicationReviewsControllerTest < ActionDis
@pet = create(:pet)
adopter_foster_account1 = create(:adopter_foster_account, :with_profile)
adopter_foster_account2 = create(:adopter_foster_account, :with_profile)
@application_under_review = create(:adopter_application, pet: @pet, adopter_foster_account: adopter_foster_account1, status: :under_review)
@application_awaiting_review = create(:adopter_application, pet: @pet, adopter_foster_account: adopter_foster_account2, status: :awaiting_review)
@application_under_review = create(:adopter_application, pet: @pet, adopter_foster_account: adopter_foster_account1, status: :under_review, form_submission: create(:form_submission))
@application_awaiting_review = create(:adopter_application, pet: @pet, adopter_foster_account: adopter_foster_account2, status: :awaiting_review, form_submission: create(:form_submission))
end

should "return pets only with applications of the specified status" do
Expand Down
1 change: 1 addition & 0 deletions test/factories/adopter_applications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
end

pet
form_submission

trait :adoption_pending do
status { 2 }
Expand Down
6 changes: 6 additions & 0 deletions test/factories/form_submisison.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :form_submission do
association :person
association :organization
end
end
13 changes: 7 additions & 6 deletions test/integration/adoption_application_reviews_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

class AdoptionApplicationReviewsTest < ActionDispatch::IntegrationTest
setup do
@awaiting_review_app = create(:adopter_application, status: :awaiting_review)
@under_review_app = create(:adopter_application, status: :under_review)
@adoption_pending_app = create(:adopter_application, :adoption_pending)
@withdrawn_app = create(:adopter_application, :withdrawn)
@successful_applicant_app = create(:adopter_application, status: :successful_applicant)
@adoption_made_app = create(:adopter_application, status: :adoption_made)
@form_submission = create(:form_submission)
@awaiting_review_app = create(:adopter_application, status: :awaiting_review, form_submission: @form_submission)
@under_review_app = create(:adopter_application, status: :under_review, form_submission: @form_submission)
@adoption_pending_app = create(:adopter_application, :adoption_pending, form_submission: @form_submission)
@withdrawn_app = create(:adopter_application, :withdrawn, form_submission: @form_submission)
@successful_applicant_app = create(:adopter_application, status: :successful_applicant, form_submission: @form_submission)
@adoption_made_app = create(:adopter_application, status: :adoption_made, form_submission: @form_submission)
@organization = create(:organization)
@custom_page = create(:custom_page, organization: @organization)
Current.organization = @organization
Expand Down
13 changes: 10 additions & 3 deletions test/models/adopter_application_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

class AdopterApplicationTest < ActiveSupport::TestCase
setup do
@application = create(:adopter_application)
@form_submission = create(:form_submission)
@application = create(:adopter_application, form_submission: @form_submission)
end

context "associations" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I guess this was missing before! Thanks for adding.

should belong_to(:pet).touch(true)
should belong_to(:adopter_foster_account)
should belong_to(:form_submission)
end

context "self.retire_applications" do
context "when some applications match pet_id and some do not" do
setup do
@selected_applications = Array.new(3) {
create(:adopter_application, pet_id: @application.pet_id)
create(:adopter_application, pet_id: @application.pet_id, form_submission: @form_submission)
}
@unselected_applications = Array.new(2) {
create(:adopter_application)
create(:adopter_application, form_submission: @form_submission)
}
end

Expand Down
1 change: 1 addition & 0 deletions test/models/custom_form/submitted_answer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class CustomForm::SubmittedAnswerTest < ActiveSupport::TestCase
context "associations" do
should belong_to(:question)
should belong_to(:user)
should belong_to(:form_submission)

should have_one(:form).through(:question)
should have_one(:organization).through(:form)
Expand Down
9 changes: 9 additions & 0 deletions test/models/form_submission_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "test_helper"

class FormSubmissionTest < ActiveSupport::TestCase
context "associations" do
should belong_to(:person)
should have_many(:adopter_applications)
should have_many(:submitted_answers)
end
end
Loading
Loading