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 3 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
1 change: 1 addition & 0 deletions app/models/adopter_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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, through: :question
has_one :organization, through: :form
Expand Down
27 changes: 27 additions & 0 deletions app/models/form_submission.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# == 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
belongs_to :person
belongs_to :organization
Copy link
Collaborator

@kasugaijin kasugaijin Jul 7, 2024

Choose a reason for hiding this comment

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

Check out other resources that belong to an organization (most of them) and you'll see we use the acts as tenant helper here. This is important for setting the tenancy, or scope, for a given record i.e., which org it belongs to, and is used by the acts as tenant scoping logic elsewhere.

acts_as_tenant(:organization)


has_many :adopter_applications
has_many :submitted_answers, class_name: "CustomForm::SubmittedAnswer"
end
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.

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: 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
11 changes: 11 additions & 0 deletions test/models/form_submission_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "test_helper"

class FormSubmissionTest < ActiveSupport::TestCase
context "associations" do
should belong_to(:person)
should belong_to(:organization)

should have_many(:adopter_applications)
should have_many(:submitted_answers)
end
end
Loading