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

[アンケート機能]アンケート用の質問の一覧、作成、編集ができるようにしたい #5370

Merged
merged 38 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fa63dbc
質問の追加まで
daiki0381 Aug 15, 2022
d4ed345
シングルクオートに変更した
daiki0381 Aug 15, 2022
4065532
孫モデルのUnpermitted parameterの解消
daiki0381 Aug 15, 2022
6b01a79
バリデーションの追加
daiki0381 Aug 15, 2022
2f09006
通知の追加
daiki0381 Aug 15, 2022
ca724fc
質問一覧と質問編集
daiki0381 Aug 15, 2022
7afd7e0
空行を追加した
daiki0381 Aug 15, 2022
31b235f
テストの追加
daiki0381 Aug 15, 2022
80c4f15
選択肢が0個の時にバリデーションをかける
daiki0381 Aug 15, 2022
ab90427
管理者・メンター以外アクセスできないようにする
daiki0381 Aug 15, 2022
166fbd9
質問作成を質問編集に変更した
daiki0381 Aug 15, 2022
94fd443
編集時に他の質問形式をクリックして、何も入力しなくてもバリデーションが効かないバグの修正
daiki0381 Aug 18, 2022
96ed27b
必須を必要に変更した
daiki0381 Aug 18, 2022
74268f2
質問一覧の修正
daiki0381 Aug 18, 2022
6b37af7
%wの[]を()に変更した
daiki0381 Aug 18, 2022
2000b37
%wの()を[]に変更した
daiki0381 Aug 18, 2022
43ce823
曜日のテストを修正した
daiki0381 Aug 18, 2022
dce9f05
更新者のカラムを作成した
daiki0381 Sep 1, 2022
426c09d
creatorとupdaterのアソシエーションを設定した
daiki0381 Sep 1, 2022
5765a53
inverse_ofの修正
daiki0381 Sep 1, 2022
1c2efed
lメソッドを使ってリファクタリング
daiki0381 Sep 2, 2022
b54900c
switch_initializationの削除
daiki0381 Sep 2, 2022
359162f
creatorとupdaterの関連付けの修正
daiki0381 Sep 2, 2022
a44568b
indexとeditのテストを追加
daiki0381 Sep 5, 2022
4d4fdaf
アンケート質問作成、一覧のデザイン
machida Sep 21, 2022
f04fd68
質問作成画面のデザイン調整
machida Sep 28, 2022
3360aae
質問一覧、作成、編集のレイアウト調整
machida Sep 28, 2022
746ec46
編集アイコンをクリックするようにした
daiki0381 Sep 28, 2022
4bde07f
question_titleカラムとquestion_descriptionカラムとquestion_formatカラムの名前を変更した
daiki0381 Sep 30, 2022
822e2ff
title_of_reason_for_choiceカラムとdescription_of_reason_for_choiceカラムの名前を…
daiki0381 Oct 12, 2022
31ca3e2
start_of_scaleカラムとend_of_scaleカラムの名前を変更した
daiki0381 Oct 12, 2022
3163dd9
updaterカラムを削除した
daiki0381 Oct 12, 2022
f7e620a
creatorをuserに変更した
daiki0381 Oct 12, 2022
210b960
テストのTime.zone.todayをTime.currentに修正した
daiki0381 Oct 23, 2022
8910388
normalize_blank_valuesメソッドの変数名の修正
daiki0381 Oct 23, 2022
13c0c2a
normalize_blankメソッドの共通化
daiki0381 Oct 25, 2022
98350fa
concernを使用して共通化
daiki0381 Oct 26, 2022
d55c336
update時にvalidationが走らない問題の解消
daiki0381 Nov 10, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
+border(horizontal, solid 1px $background-semi-shade)

.admin-table-item__title
+text-block(1.5rem 1.45, $side)
+text-block(1.5rem 1.45, $default-text)

.admin-table__label
+text-block(.75rem 1.4, $main center 600)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ $checkbox-size: .875rem

.checkboxes__item
label
+text-block(.9375rem 1.4, block $semi-muted-text)
+text-block(.875rem 1.4, block $semi-muted-text)
+position(relative)
+hover-link
cursor: pointer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.form-added-choice
.form-added-choice + &
margin-top: 1rem

.form-added-choice__inner
+media-breakpoint-up(md)
display: flex
align-items: flex-start
gap: .75rem

.form-added-choice__col
&.is-main
flex: 1

.form-added-choice__items
display: flex
+media-breakpoint-down(sm)
gap: 1rem

.form-added-choice__action
+media-breakpoint-down(sm)
margin-top: .75rem
display: flex
justify-content: flex-end

.form-added-choice__label
display: flex
height: 2.75rem
align-items: center
&.is-count
width: 1.5rem
text-align: center
justify-content: center
.form-item__vertical-separator &
height: 1.5rem
15 changes: 15 additions & 0 deletions app/assets/stylesheets/application/blocks/form/_form-item.sass
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,18 @@
border-top: dashed 1px $warning
color: $default-text
font-weight: 400

.form-item__add-times
+media-breakpoint-up(md)
margin-top: -.125rem
+media-breakpoint-down(sm)
margin-top: -1.25rem

.form-item__add-choice
+media-breakpoint-up(md)
margin-top: 1rem
+media-breakpoint-down(sm)
margin-top: 1rem

.form-item__option
margin-top: .5rem
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
.form-item__times
display: flex
align-items: flex-end
+media-breakpoint-down(sm)
display: block

.form-selects
display: flex

Expand Down
34 changes: 16 additions & 18 deletions app/assets/stylesheets/application/blocks/form/_form-times.sass
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
.form-item__add-times
+media-breakpoint-up(md)
margin-top: -.125rem
.form-times
display: flex
align-items: flex-end
+media-breakpoint-down(sm)
margin-top: -1.25rem
display: block

.form-item__times-inner
.form-times__inner
margin-bottom: 1rem
+media-breakpoint-up(md)
display: flex
align-items: flex-end

.form-item__times-items
.form-times__items
display: flex
+media-breakpoint-down(sm)
gap: 1rem

.form-item__time
.form-times__action
+media-breakpoint-down(sm)
margin-top: .75rem
display: flex
justify-content: flex-end
#tasks .form-item:first-child &
display: none

.form-times__time
display: flex
flex-direction: column
margin-right: 1rem
+media-breakpoint-down(sm)
margin-right: 0
flex-basis: 50%

.form-item__time-label
.form-times__time-label
+text-block(.75rem 1.4 0 .5rem, block)

.form-item__times-action
+media-breakpoint-down(sm)
margin-top: .75rem
display: flex
justify-content: flex-end

.form-item__times-action
#tasks .form-item:first-child &
display: none
3 changes: 3 additions & 0 deletions app/assets/stylesheets/atoms/_a-block-check.sass
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.a-block-check
height: 100%

.a-block-check__label
background-color: $input-background
height: 100%
Expand Down
59 changes: 59 additions & 0 deletions app/controllers/survey_questions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

class SurveyQuestionsController < ApplicationController
before_action :set_survey_question, only: %i[edit update]
before_action :require_admin_or_mentor_login

def index
@survey_questions = SurveyQuestion.all
end

def new
@survey_question = SurveyQuestion.new
@linear_scale = @survey_question.build_linear_scale
@radio_button = @survey_question.build_radio_button
@check_box = @survey_question.build_check_box
@radio_button_choices = @radio_button.radio_button_choices.build
@check_box_choices = @check_box.check_box_choices.build
end

def create
@survey_question = SurveyQuestion.new(survey_question_params)
@survey_question.user_id = current_user.id

if @survey_question.save
redirect_to survey_questions_path, notice: '質問を保存しました。'
else
render action: :new
end
end

def edit; end

def update
if @survey_question.update(survey_question_params)
redirect_to survey_questions_path, notice: '質問を保存しました。'
else
render action: :edit
end
end

private

def set_survey_question
@survey_question = SurveyQuestion.find(params[:id])
end

def survey_question_params
params.require(:survey_question).permit(:title, :description, :format, :answer_required,
linear_scale_attributes:
%i[id first last reason_for_choice_required] +
%i[title_of_reason description_of_reason],
radio_button_attributes:
%i[id title_of_reason description_of_reason] +
[radio_button_choices_attributes: %i[id choices reason_for_choice_required _destroy]],
check_box_attributes:
%i[id title_of_reason description_of_reason] +
[check_box_choices_attributes: %i[id choices reason_for_choice_required _destroy]])
end
end
1 change: 1 addition & 0 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import '../products.js'
import '../courses-categories.js'
import '../courses-practices.js'
import '../no_learn.js'
import '../survey_question.js'
import '../searchables.js'
import '../niconico_calendar.js'
import '../mentor-mode.js'
Expand Down
101 changes: 101 additions & 0 deletions app/javascript/survey_question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
document.addEventListener('DOMContentLoaded', () => {
const radioButtonChoices = document.getElementsByName(
'survey_question[format]'
)
const radioButtonElements = document.querySelectorAll('.radio_button')
const checkBoxElements = document.querySelectorAll('.check_box')
const linearScaleElements = document.querySelectorAll('.linear_scale')
const radioButtonTitleOfReasonForChoice = document.getElementById(
'survey_question_radio_button_attributes_title_of_reason'
)
const checkBoxTitleOfReasonForChoice = document.getElementById(
'survey_question_check_box_attributes_title_of_reason'
)
const linearScaleTitleOfReasonForChoice = document.getElementById(
'survey_question_linear_scale_attributes_title_of_reason'
)
const radioButtonDescriptionOfReasonForChoice = document.getElementById(
'survey_question_radio_button_attributes_description_of_reason'
)
const checkBoxDescriptionOfReasonForChoice = document.getElementById(
'survey_question_check_box_attributes_description_of_reason'
)
const linearScaleDescriptionOfReasonForChoice = document.getElementById(
'survey_question_linear_scale_attributes_description_of_reason'
)
const startOfScale = document.getElementById(
'survey_question_linear_scale_attributes_first'
)
const endOfScale = document.getElementById(
'survey_question_linear_scale_attributes_last'
)
const reasonForChoiceRequired = document.getElementById(
'survey_question_linear_scale_attributes_reason_for_choice_required'
)
const switchIsHidden = (radioButtonChoice) => {
if (
radioButtonChoice.value === 'text_field' ||
radioButtonChoice.value === 'text_area'
) {
radioButtonElements.forEach((radioButtonElement) => {
radioButtonElement.classList.add('is-hidden')
})
checkBoxElements.forEach((checkBoxElement) => {
checkBoxElement.classList.add('is-hidden')
})
linearScaleElements.forEach((linearScaleElement) => {
linearScaleElement.classList.add('is-hidden')
})
} else if (radioButtonChoice.value === 'radio_button') {
radioButtonElements.forEach((radioButtonElement) => {
radioButtonElement.classList.remove('is-hidden')
})
checkBoxElements.forEach((checkBoxElement) => {
checkBoxElement.classList.add('is-hidden')
})
linearScaleElements.forEach((linearScaleElement) => {
linearScaleElement.classList.add('is-hidden')
})
} else if (radioButtonChoice.value === 'check_box') {
radioButtonElements.forEach((radioButtonElement) => {
radioButtonElement.classList.add('is-hidden')
})
checkBoxElements.forEach((checkBoxElement) => {
checkBoxElement.classList.remove('is-hidden')
})
linearScaleElements.forEach((linearScaleElement) => {
linearScaleElement.classList.add('is-hidden')
})
} else if (radioButtonChoice.value === 'linear_scale') {
radioButtonElements.forEach((radioButtonElement) => {
radioButtonElement.classList.add('is-hidden')
})
checkBoxElements.forEach((checkBoxElement) => {
checkBoxElement.classList.add('is-hidden')
})
linearScaleElements.forEach((linearScaleElement) => {
linearScaleElement.classList.remove('is-hidden')
})
}
}

radioButtonChoices.forEach((radioButtonChoice) => {
radioButtonChoice.checked && switchIsHidden(radioButtonChoice)
radioButtonChoice.addEventListener('change', () => {
const removeFieldsElements = document.querySelectorAll('.remove_fields')
removeFieldsElements.forEach((removeFieldsElement) => {
removeFieldsElement.click()
})
radioButtonTitleOfReasonForChoice.value = null
radioButtonDescriptionOfReasonForChoice.value = null
checkBoxTitleOfReasonForChoice.value = null
checkBoxDescriptionOfReasonForChoice.value = null
linearScaleTitleOfReasonForChoice.value = null
linearScaleDescriptionOfReasonForChoice.value = null
startOfScale.value = null
endOfScale.value = null
reasonForChoiceRequired.checked = false
switchIsHidden(radioButtonChoice)
})
})
})
13 changes: 13 additions & 0 deletions app/models/check_box.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class CheckBox < ApplicationRecord
belongs_to :survey_question
has_many :check_box_choices, dependent: :destroy
accepts_nested_attributes_for :check_box_choices, allow_destroy: true
validates_associated :check_box_choices

with_options if: -> { survey_question.format == 'check_box' }, presence: true do
validates :check_box_choices
validates :title_of_reason
end
end
7 changes: 7 additions & 0 deletions app/models/check_box_choice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class CheckBoxChoice < ApplicationRecord
belongs_to :check_box

validates :choices, presence: true, if: -> { check_box.survey_question.format == 'check_box' }
end
11 changes: 11 additions & 0 deletions app/models/linear_scale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class LinearScale < ApplicationRecord
belongs_to :survey_question

with_options if: -> { survey_question.format == 'linear_scale' }, presence: true do
validates :first
validates :last
validates :title_of_reason
end
end
13 changes: 13 additions & 0 deletions app/models/radio_button.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class RadioButton < ApplicationRecord
belongs_to :survey_question
has_many :radio_button_choices, dependent: :destroy
accepts_nested_attributes_for :radio_button_choices, allow_destroy: true
validates_associated :radio_button_choices

with_options if: -> { survey_question.format == 'radio_button' }, presence: true do
validates :radio_button_choices
validates :title_of_reason
end
end
7 changes: 7 additions & 0 deletions app/models/radio_button_choice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class RadioButtonChoice < ApplicationRecord
belongs_to :radio_button

validates :choices, presence: true, if: -> { radio_button.survey_question.format == 'radio_button' }
end
24 changes: 24 additions & 0 deletions app/models/survey_question.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

class SurveyQuestion < ApplicationRecord
belongs_to :user
has_one :linear_scale, dependent: :destroy
accepts_nested_attributes_for :linear_scale, allow_destroy: true
validates_associated :linear_scale
has_one :radio_button, dependent: :destroy
accepts_nested_attributes_for :radio_button, allow_destroy: true
validates_associated :radio_button
has_one :check_box, dependent: :destroy
accepts_nested_attributes_for :check_box, allow_destroy: true
validates_associated :check_box

enum format: {
text_area: 0,
text_field: 1,
radio_button: 2,
check_box: 3,
linear_scale: 4
}, _prefix: true

validates :title, presence: true
end
Loading