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

Custom form questions #597

Merged
merged 7 commits into from
Jul 25, 2020
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [Unreleased]
### Enhancements
* Allow custom questions to be added to Ask and Offer forms #597

## [0.2.5] - 2020-07-24
### Bugfixes
* Only show visible subcategories for any visible category #594
Expand Down
7 changes: 7 additions & 0 deletions app/blueprints/form_blueprint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class FormBlueprint < Blueprinter::Base
identifier :id

association :questions, blueprint: QuestionBlueprint do |form|
CustomFormQuestion.for_form(form).ordered
end
end
11 changes: 11 additions & 0 deletions app/blueprints/question_blueprint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class QuestionBlueprint < Blueprinter::Base
identifier :id

fields(
:name,
:input_type,
:is_required,
:option_list,
:hint_text,
)
end
1 change: 1 addition & 0 deletions app/controllers/asks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def render_form(submission)
@json = {
submission: SubmissionBlueprint.render_as_hash(submission),
configuration: ConfigurationBlueprint.render_as_hash(nil),
form: FormBlueprint.render_as_hash(@form),
}.to_json

render :new
Expand Down
1 change: 1 addition & 0 deletions app/controllers/offers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def render_form(submission)
@json = {
submission: SubmissionBlueprint.render_as_hash(submission),
configuration: ConfigurationBlueprint.render_as_hash(nil),
form: FormBlueprint.render_as_hash(@form),
}.to_json

render :new
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ def set_form_dropdowns
end

def submission_params
params.require(:submission).permit(:person_id, :service_area_id, :body, :form_name, :privacy_level_requested)
params.require(:submission).permit(
:body,
:created_at,
:form_name,
:person_id,
:privacy_level_requested,
:service_area_id,
)
end
end
17 changes: 14 additions & 3 deletions app/forms/submission_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ class SubmissionForm < BaseForm
with_options default: nil do
integer :id
record :service_area
hash :listing_attributes, strip: false
hash :location_attributes, strip: false
hash :person_attributes, strip: false
hash :listing_attributes, strip: false
hash :location_attributes, strip: false
hash :person_attributes, strip: false
hash :responses_attributes, strip: false
string :form_name
string :privacy_level_requested # fixme: not submitted as yet
end
Expand All @@ -13,6 +14,7 @@ def execute
build_location
build_person
build_listing
build_submission_responses
build_submission
end

Expand Down Expand Up @@ -40,6 +42,14 @@ def build_submission
end
end

def build_submission_responses
@submission_responses = responses_attributes.each_with_object([]) do |(custom_form_question_id, answer), obj|
response = SubmissionResponseForm.build custom_form_question_id: custom_form_question_id,
string_response: answer
obj << response
end
end

def submission_attributes
given_inputs
.slice(
Expand All @@ -51,6 +61,7 @@ def submission_attributes
body: body_json,
person: @person,
listings: [@listing],
submission_responses: @submission_responses,
)
end

Expand Down
19 changes: 19 additions & 0 deletions app/forms/submission_response_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class SubmissionResponseForm < BaseForm
with_options default: nil do
integer :id
integer :custom_form_question_id
string :string_response
boolean :boolean_response
date :date_response
# datetime :datetime_response
integer :integer_response
string :text_response
array :array_response, default: []
end

def execute
SubmissionResponse.find_or_new(id).tap do |submission_response|
submission_response.attributes = given_inputs
end
end
end
29 changes: 29 additions & 0 deletions app/javascript/components/forms/CustomQuestions.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<template>
<div>
<b-field
v-for="{id, name} in questions"
:key="id"
:label-for="fieldName(id)"
:label="name"
custom-class="is-medium"
>
<b-input :name="fieldName(id)" />
</b-field>
</div>
</template>

<script>
import {fieldNameWithPrefix} from 'utils/form'

export default {
props: {
questions: Array,
fieldNamePrefix: String,
},
methods: {
fieldName(id) {
return fieldNameWithPrefix(this.fieldNamePrefix, id.toString())
},
},
}
</script>
2 changes: 2 additions & 0 deletions app/javascript/components/forms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AuthTokenInput from './AuthTokenInput'
import CategoryFields from './CategoryFields'
import CommentsField from './CommentsField'
import ContactFields from './ContactFields'
import CustomQuestions from './CustomQuestions'
import DeleteButton from './DeleteButton'
import ErrorMessages from './ErrorMessages'
import FeedbackButton from './FeedbackButton'
Expand All @@ -16,6 +17,7 @@ export {
CategoryFields,
CommentsField,
ContactFields,
CustomQuestions,
DeleteButton,
ErrorMessages,
FeedbackButton,
Expand Down
8 changes: 8 additions & 0 deletions app/javascript/pages/Ask.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
:person="person"
/><SpacerField />

<CustomQuestions
fieldNamePrefix="submission[responses_attributes]"
:questions="form.questions"
/><SpacerField />

<CategoryFields
:fieldNamePrefix="withListingPrefix('tag_list[]')"
:categories="configuration.categories"
Expand Down Expand Up @@ -57,6 +62,7 @@ import {
CategoryFields,
CommentsField,
ContactFields,
CustomQuestions,
ErrorMessages,
LocationFields,
NameField,
Expand All @@ -71,6 +77,7 @@ export default {
CategoryFields,
CommentsField,
ContactFields,
CustomQuestions,
ErrorMessages,
LocationFields,
NameField,
Expand All @@ -81,6 +88,7 @@ export default {
props: {
submission: Object,
configuration: Object,
form: Object,
},
data() {
return {
Expand Down
8 changes: 8 additions & 0 deletions app/javascript/pages/Offer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
v-bind="location"
/><SpacerField />

<CustomQuestions
fieldNamePrefix="submission[responses_attributes]"
:questions="form.questions"
/><SpacerField />

<CategoryFields
:fieldNamePrefix="withListingPrefix('tag_list[]')"
:categories="configuration.categories"
Expand Down Expand Up @@ -74,6 +79,7 @@ import {
CategoryFields,
ContactFields,
CommentsField,
CustomQuestions,
ErrorMessages,
LocationFields,
NameField,
Expand All @@ -98,6 +104,7 @@ export default {
CategoryFields,
ContactFields,
CommentsField,
CustomQuestions,
ErrorMessages,
LocationFields,
NameField,
Expand All @@ -108,6 +115,7 @@ export default {
props: {
submission: Object,
configuration: Object,
form: Object,
},
data() {
return {
Expand Down
4 changes: 4 additions & 0 deletions app/models/custom_form_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ class CustomFormQuestion < ApplicationRecord
scope :translated_name_stem, ->(stem) { joins(:mobility_string_translations).
where("mobility_string_translations.key = 'name' AND mobility_string_translations.locale = 'en'").
where("mobility_string_translations.value ILIKE ?", "%#{stem}%") }

scope :for_form, ->(form) { joins(:form_questions).where(form_questions: {form: form}) }

scope :ordered, ->() { order(:display_order) }
end
2 changes: 1 addition & 1 deletion app/models/form.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Form < ApplicationRecord
belongs_to :organization
has_many :form_questions
has_many :questions, class_name: 'FormQuestion'
end
5 changes: 5 additions & 0 deletions spec/factories/custom_form_questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :custom_form_question do
name { "My custom question" }
end
end
4 changes: 2 additions & 2 deletions spec/factories/submission_responses.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryBot.define do
factory :submission_response do
submission { nil }
custom_form_question { nil }
submission
custom_form_question
boolean_response { false }
date_response { "2020-05-12" }
datetime_response { "2020-05-12 23:13:57" }
Expand Down
38 changes: 32 additions & 6 deletions spec/forms/submission_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
let(:contact_method) { create :contact_method_email }
let(:location_type) { create :location_type }
let(:service_area) { create :service_area }
let(:question_1) { create :custom_form_question }
let(:question_2) { create :custom_form_question }

describe 'creating a new submission' do
let(:params) {{
Expand All @@ -27,6 +29,10 @@
email: 'we@together.coop',
name: 'Harriet Tubman',
},
responses_attributes: {
question_1.id.to_s => "answer 1",
question_2.id.to_s => "answer 2"
},
}}

subject(:submission) { SubmissionForm.build params }
Expand Down Expand Up @@ -117,13 +123,25 @@
end
end

describe 'has_many submission_responses' do
subject(:submission_responses) { submission.submission_responses }

it 'builds SubmissionResponses' do
expect(submission_responses.first.string_response).to eq("answer 1")
end

it 'builds SubmissionResponses' do
expect(submission_responses.length).to eq(2)
end
end

describe 'submission capture' do
let(:json) { JSON.parse(submission.body) }

it 'captures all inputs given' do
expect(json.keys).to contain_exactly(
'form_name', 'listing_attributes', 'location_attributes',
'person_attributes', 'privacy_level_requested', 'service_area'
'person_attributes', 'responses_attributes', 'privacy_level_requested', 'service_area'
)
end

Expand Down Expand Up @@ -178,7 +196,7 @@
end
end

describe 'updating an existing submission' do
pending 'updating an existing submission' do
let(:existing_listing) { create :offer, state: :unmatched, description: 'keep' }
let(:existing_location) { create :location, city: 'Chicago', zip: '10101' }
let(:existing_person) { create :person, location: existing_location, name: 'old name', email: 'keep@me.org' }
Expand All @@ -189,6 +207,7 @@
form_name: 'Offer_form',
privacy_level_requested: 'volunteers',
)}
let(:existing_response) { create :submission_response, submission: existing_submission }

let(:params) {{
id: existing_submission.id,
Expand All @@ -206,25 +225,31 @@
id: existing_person.id,
name: 'new name',
},
responses_attributes: {
existing_response.custom_form_question_id.to_s => "updated answer",
},
}}

let(:submission) { SubmissionForm.build params }

it 'returns the existing records' do
expect(submission.listings.first.id).to be existing_listing.id
expect(submission.submission_responses.first.id).to be existing_response.id
expect(submission.person.location.id).to be existing_location.id
expect(submission.person.id).to be existing_person.id
expect(submission.id).to be existing_submission.id
end

it 'applies pending changes to submission and nested objects' do
expect(submission.submission_responses.first.string_response).to be_changed
expect(submission.listings.first).to be_changed
expect(submission.person.location).to be_changed
expect(submission.person).to be_changed
expect(submission).to be_changed # TODO: should submissions be editable?
end

it 'applies new values to submission and nested objects' do
expect(submission.submission_responses.first.state).to eq 'updated answer'
expect(submission.listings.first.state).to eq 'matched'
expect(submission.person.location.city).to eq 'Shikaakwa'
expect(submission.person.name).to eq 'new name'
Expand All @@ -245,10 +270,11 @@

it 'does not create any new objects on save' do
expect { submission.save }
.to change(Listing, :count).by(0)
.and change(Location, :count).by(0)
.and change(Person, :count).by(0)
.and change(Submission, :count).by(0)
.to change(Listing, :count).by(0)
.and change(Location, :count).by(0)
.and change(Person, :count).by(0)
.and change(Submission, :count).by(0)
.and change(SubmissionResponse, :count).by(0)
end
end
end
Expand Down
Loading