Add question types to surveys#1398
Conversation
|
|
||
| def label_for_question(survey, question) | ||
| return survey.published? ? t('.question') : "#{icon("move")} #{t('.question')}".html_safe if question.persisted? | ||
| "#{icon("move")} #{t('.question')} #${questionLabelPosition}".html_safe |
There was a problem hiding this comment.
Prefer double-quoted strings inside interpolations.
| end | ||
|
|
||
| def label_for_question(survey, question) | ||
| return survey.published? ? t('.question') : "#{icon("move")} #{t('.question')}".html_safe if question.persisted? |
There was a problem hiding this comment.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Prefer double-quoted strings inside interpolations.
1a25fa9 to
cabe8ee
Compare
Codecov Report
@@ Coverage Diff @@
## feature/surveys #1398 +/- ##
===================================================
+ Coverage 94.86% 94.89% +0.02%
===================================================
Files 444 445 +1
Lines 7554 7593 +39
===================================================
+ Hits 7166 7205 +39
Misses 388 388 |
| described_class.from_model(survey) | ||
| described_class.from_model(survey).with_context({ | ||
| current_feature: survey.feature | ||
| }) |
There was a problem hiding this comment.
Indent the right brace the same as the first position after the preceding left parenthesis.
| subject do | ||
| described_class.from_model(survey) | ||
| described_class.from_model(survey).with_context({ | ||
| current_feature: survey.feature |
There was a problem hiding this comment.
Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis.
|
|
||
| subject do | ||
| described_class.from_model(survey) | ||
| described_class.from_model(survey).with_context({ |
There was a problem hiding this comment.
Redundant curly braces around a hash parameter.
| subject do | ||
| described_class.from_model(survey_answer).with_context({ | ||
| current_feature: survey.feature | ||
| }) |
There was a problem hiding this comment.
Indent the right brace the same as the first position after the preceding left parenthesis.
|
|
||
| subject do | ||
| described_class.from_model(survey_answer).with_context({ | ||
| current_feature: survey.feature |
There was a problem hiding this comment.
Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis.
| let!(:survey_answer) { create(:survey_answer, user: user, survey: survey, question: survey_question) } | ||
|
|
||
| subject do | ||
| described_class.from_model(survey_answer).with_context({ |
There was a problem hiding this comment.
Redundant curly braces around a hash parameter.
cabe8ee to
b14d909
Compare
b14d909 to
29bbda5
Compare
| }, | ||
| position: 0 | ||
| position: 0, | ||
| question_type: "short_answer" |
| "position" => "0" | ||
| "position" => "0", | ||
| "question_type" => "short_answer", | ||
| "answer_options" => [] |
| end | ||
|
|
||
| def label_for_question(survey, question) | ||
| return survey.published? ? t(".question") : "#{icon("move")} #{t(".question")}".html_safe |
| "${tabsId}" | ||
| end | ||
|
|
||
| def label_for_question(survey, question) |
There was a problem hiding this comment.
Unused method argument - question. If it's necessary, use _ or _question as an argument name to indicate that it won't be used.
| end | ||
| end | ||
| end | ||
| end No newline at end of file |
|
@beagleknight, thanks for your PR! By analyzing the history of the files in this pull request, we identified @lastpotion and @mrcasals to be potential reviewers. |
|
|
||
| # Private: answer options should be mapped manually. | ||
| def map_model(model) | ||
| self.answer_options = model.answer_options |
There was a problem hiding this comment.
This shouldn't be needed :/
There was a problem hiding this comment.
It is needed! I did some tests with Virtus stuff and the problem is answer_options is not a real model, just a serialized field and he doesn't know how to build the actual form field.
There was a problem hiding this comment.
Doesn't it call the method? I mean, you are not changing anything here. We usually use this method because of some form field names not matching the model method, so we need to manually set them. Here you are not doing anything special, you're calling the same method name that you are setting so it should be done automatically :/
| # Custom helpers, scoped to the surveys engine. | ||
| # | ||
| module ApplicationHelper | ||
| def tabs_id_for_question(question) |
There was a problem hiding this comment.
I will do a PR with all the documentation because the API is changing too much.
| belongs_to :survey, class_name: Survey, foreign_key: "decidim_survey_id" | ||
|
|
||
| def answer_options | ||
| self[:answer_options] || [] |
There was a problem hiding this comment.
This smells like you're missing a default: [] value in the migration that adds this column
| @@ -0,0 +1,5 @@ | |||
| class AddAnswerOptionsToSurveysQuestions < ActiveRecord::Migration[5.0] | |||
| def change | |||
| add_column :decidim_surveys_survey_questions, :answer_options, :jsonb | |||
There was a problem hiding this comment.
Add a default non-nil value. See the comment I left in the model
There was a problem hiding this comment.
default: [] should be goos enough, if you need an array here
josepjaume
left a comment
There was a problem hiding this comment.
Nice!! Outstanding work!
|
|
||
| # Private: answer options should be mapped manually. | ||
| def map_model(model) | ||
| self.answer_options = model.answer_options |
|
@josepjaume @mrcasals 🤷♂️ |
| # Rectify can't handle a hash when using the from_model method so | ||
| # the answer options must be converted to struct. | ||
| def answer_options | ||
| self[:answer_options].map { |option| OpenStruct.new(option) } |
There was a problem hiding this comment.
This makes me cry so hard

🎩 What? Why?
This adds question types to survey questions:
In this PR I also included a lot of refactors for the JS code. I built a mini library to add/remove dynamic fields from a form. I have not included documentation yet because the code is changing in every PR. I save the final docs before merging the feature branch.
📌 Related Issues
📋 Subtasks
📷 Screenshots (optional)
👻 GIF