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

Spike add multiple answers #509

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ gem "validate_url"
# For pausing pipelines
gem "aws-sdk-codepipeline", "~> 1.74"

gem "active_model_serializers"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is fine for proof of concept but we shouldn't use this gem as it's not fully released (and has gone into maintenance mode) https://github.com/rails-api/active_model_serializers?tab=readme-ov-file#alternatives

I think we should be able to do something like:

JSON.generate(Form.includes(:question_steps).where("form.id = ?", 1).to_h)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeh good point about the gem. I was just scouring the internet for something that we could use to spit out the JSON in the desired format, I'm sure we can find a way without using this particular gem.


group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[mri mingw x64_mingw]
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ GEM
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
active_model_serializers (0.10.14)
actionpack (>= 4.1)
activemodel (>= 4.1)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
activejob (7.1.3.4)
activesupport (= 7.1.3.4)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -114,6 +119,8 @@ GEM
bundler-audit (0.9.1)
bundler (>= 1.2.0, < 3)
thor (~> 1.0)
case_transform (0.2)
activesupport
concurrent-ruby (1.3.1)
config (5.5.1)
deep_merge (~> 1.2, >= 1.2.1)
Expand Down Expand Up @@ -145,6 +152,7 @@ GEM
reline (>= 0.4.2)
jmespath (1.6.2)
json (2.7.2)
jsonapi-renderer (0.2.2)
language_server-protocol (3.17.0.3)
lograge (0.14.0)
actionpack (>= 4)
Expand Down Expand Up @@ -344,6 +352,7 @@ PLATFORMS

DEPENDENCIES
aasm (~> 5.5)
active_model_serializers
acts_as_list
after_commit_everywhere (~> 1.4)
aws-sdk-codepipeline (~> 1.74)
Expand Down
1 change: 1 addition & 0 deletions app/models/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Form < ApplicationRecord

has_many :pages, -> { order(position: :asc) }, dependent: :destroy
has_many :made_live_forms, -> { order(created_at: :asc) }, dependent: :destroy
has_many :steps, -> { order(position: :asc) }, dependent: :destroy

validates :name, presence: true
validates :payment_url, url: true, allow_blank: true
Expand Down
2 changes: 2 additions & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Question < ApplicationRecord
end
3 changes: 3 additions & 0 deletions app/models/question_set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class QuestionSet < ApplicationRecord
has_many :steps, -> { order(position: :asc) }, class_name: "Step", foreign_key: "parent_question_set_id", dependent: :destroy
end
6 changes: 6 additions & 0 deletions app/models/step.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Step < ApplicationRecord
belongs_to :positionable, polymorphic: true
belongs_to :next_step, class_name: "Step", optional: true
belongs_to :form, optional: true
belongs_to :parent_question_set, class_name: "QuestionSet", optional: true
end
6 changes: 6 additions & 0 deletions app/serializers/form_snapshot_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class FormSnapshotSerializer < ActiveModel::Serializer
attributes :id, :name, :submission_email, :privacy_policy_url, :form_slug, :support_email, :support_phone,
:support_url, :support_url_text, :declaration_text, :what_happens_next_markdown, :payment_url, :start_page

has_many :steps
end
3 changes: 3 additions & 0 deletions app/serializers/question_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class QuestionSerializer < ActiveModel::Serializer
attributes :question_text
end
5 changes: 5 additions & 0 deletions app/serializers/question_set_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class QuestionSetSerializer < ActiveModel::Serializer
attributes :name

has_many :steps
end
6 changes: 6 additions & 0 deletions app/serializers/step_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class StepSerializer < ActiveModel::Serializer
attributes :id, :next_step_id, :position, :min_answers, :max_answers
attribute :positionable_type, key: :type

has_one :positionable, key: :data
end
9 changes: 9 additions & 0 deletions db/migrate/20240618151516_create_questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateQuestions < ActiveRecord::Migration[7.1]
def change
create_table :questions do |t|
t.string :question_text

t.timestamps
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20240618151558_create_question_sets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateQuestionSets < ActiveRecord::Migration[7.1]
def change
create_table :question_sets do |t|
t.string :name

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240618152147_create_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateSteps < ActiveRecord::Migration[7.1]
def change
create_table :steps do |t|
t.references :positionable, polymorphic: true, null: false
t.references :next_step, index: true, foreign_key: { to_table: :steps }
t.integer :position
t.references :parent_question_set, index: true, foreign_key: { to_table: :question_sets }

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240618175604_add_form_to_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFormToSteps < ActiveRecord::Migration[7.1]
def change
add_reference :steps, :form, foreign_key: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddMinAnswersAndMaxAnswersToSteps < ActiveRecord::Migration[7.1]
def change
add_column :steps, :min_answers, :integer
add_column :steps, :max_answers, :integer
end
end
34 changes: 33 additions & 1 deletion db/schema.rb

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

24 changes: 24 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,27 @@
what_happens_next_markdown: "Test",
)
all_question_types_form.make_live!

root_question_1 = Question.create!(question_text: "What is your name?")
question_set = QuestionSet.create!(name: "What addresses have you lived at in the past 3 years?")
multiple_answers_question = Question.create!(question_text: "What countries have you travelled to in the past year?")

step1 = all_question_types_form.steps.create!(positionable: root_question_1, position: 1)
step2 = all_question_types_form.steps.create!(positionable: question_set, position: 2, min_answers: 1, max_answers:5)
step3 = all_question_types_form.steps.create!(positionable: multiple_answers_question, position: 3, min_answers:1, max_answers:10)

step1.update!(next_step: step2)
step2.update!(next_step: step3)

set_question_1 = Question.create!(question_text: "What was your address?")
set_question_2 = Question.create!(question_text: "What date did you start living at this address?")
set_question_3 = Question.create!(question_text: "What date did you stop living at this address?")

set_step1 = question_set.steps.create!(positionable: set_question_1, position: 1)
set_step2 = question_set.steps.create!(positionable: set_question_2, position: 2)
set_step3 = question_set.steps.create!(positionable: set_question_3, position: 3)

set_step1.update!(next_step: set_step2)
set_step2.update!(next_step: set_step3)

# Step.all.as_json(include: :positionable)
5 changes: 5 additions & 0 deletions spec/factories/question_sets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :question_set do
name { "MyString" }
end
end
5 changes: 5 additions & 0 deletions spec/factories/questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :question do
question_text { "MyString" }
end
end
8 changes: 8 additions & 0 deletions spec/factories/steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :step do
positionable { nil }
next_step { nil }
position { 1 }
parent_question_set { nil }
end
end
5 changes: 5 additions & 0 deletions spec/models/question_set_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

RSpec.describe QuestionSet, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/question_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

RSpec.describe Question, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/step_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

RSpec.describe Step, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading