diff --git a/Gemfile b/Gemfile index 3df0e5577..9104b7e25 100644 --- a/Gemfile +++ b/Gemfile @@ -47,6 +47,7 @@ gem 'acts-as-list' gem 'aws-sdk-rails', '>= 3.8.0' gem 'aws-sdk-s3' gem 'carrierwave', '>= 2.2.1' +gem 'csv' gem 'devise', '>= 4.8.1' gem 'fog-aws', '>= 3.15.0' gem "jbuilder" diff --git a/Gemfile.lock b/Gemfile.lock index ab609c636..03fbf59a7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -205,6 +205,7 @@ GEM concurrent-ruby (1.3.5) connection_pool (2.5.0) crass (1.0.6) + csv (3.3.2) database_cleaner (2.1.0) database_cleaner-active_record (>= 2, < 3) database_cleaner-active_record (2.2.0) @@ -653,6 +654,7 @@ DEPENDENCIES bundler-audit capybara carrierwave (>= 2.2.1) + csv database_cleaner devise (>= 4.8.1) dotenv diff --git a/app/controllers/admin/questions_controller.rb b/app/controllers/admin/questions_controller.rb index 65e12e15d..5ed344ef6 100644 --- a/app/controllers/admin/questions_controller.rb +++ b/app/controllers/admin/questions_controller.rb @@ -2,7 +2,7 @@ module Admin class QuestionsController < AdminController - before_action :set_form, only: %i[new create show edit update sort destroy] + before_action :set_form, only: %i[create show edit update sort destroy] before_action :set_question, only: %i[show edit update destroy] def index @@ -13,12 +13,6 @@ def show render layout: false end - def new - @question = Question.new - set_defaults - render layout: false - end - def edit render layout: false end @@ -33,13 +27,12 @@ def sort end def create - next_position = @form.questions.size + 1 - @question = Question.new(question_params) - @question.position = next_position + @question = Question.new + set_defaults respond_to do |format| if @question.save - format.html { redirect_to questions_admin_form_path(@form), notice: 'Question was successfully created.' } + format.html { render :new, layout: false } format.json { render json: @question } else format.html { render :new } @@ -86,6 +79,8 @@ def set_defaults @question.text = 'New Question' @question.question_type = 'text_field' @question.answer_field = first_unused_answer_field + next_position = @form.questions.size + 1 + @question.position = next_position @question.save! end diff --git a/app/models/question.rb b/app/models/question.rb index 5f9674d24..56a30683d 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -6,6 +6,7 @@ class Question < ApplicationRecord has_many :question_options, dependent: :destroy validates :question_type, presence: true + validates :position, presence: true validate :validate_question_types validates :answer_field, uniqueness: { scope: :form_id } diff --git a/app/views/admin/forms/_add_question.html.erb b/app/views/admin/forms/_add_question.html.erb index 4602834bd..86b198bde 100644 --- a/app/views/admin/forms/_add_question.html.erb +++ b/app/views/admin/forms/_add_question.html.erb @@ -1,4 +1,5 @@ -<%= link_to new_admin_form_question_path(form), class: "usa-button form-add-question width-full margin-top-1" do %> +<%= link_to admin_form_questions_path(form), + class: "usa-button form-add-question width-full margin-top-1" do %> Add Question <% end %> diff --git a/app/views/components/forms/edit/_builder.html.erb b/app/views/components/forms/edit/_builder.html.erb index f4318c68e..bddfb51be 100644 --- a/app/views/components/forms/edit/_builder.html.erb +++ b/app/views/components/forms/edit/_builder.html.erb @@ -223,7 +223,7 @@ $(function() { var formSection = $(this).parents(".form-section-div")[0]; var formSectionId = $(formSection).attr("data-id"); $.ajax({ - type: "GET", + type: "POST", url: $(this).attr("href"), data: { "form_section_id": formSectionId }, success: function (data) { diff --git a/config/routes.rb b/config/routes.rb index ede79dce6..81783204f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -311,7 +311,7 @@ patch 'sort', to: 'form_sections#sort', as: :sort_sections patch 'update_title', to: 'form_sections#update_title', as: :inline_update end - resources :questions do + resources :questions, except: :new do member do patch 'question_options', to: 'question_options#sort', as: :sort_question_options end diff --git a/package-lock.json b/package-lock.json index e22fb5661..a04c19a64 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7834,9 +7834,9 @@ } }, "node_modules/undici": { - "version": "6.19.8", - "resolved": "https://registry.npmjs.org/undici/-/undici-6.19.8.tgz", - "integrity": "sha512-U8uCCl2x9TK3WANvmBavymRzxbfFYG+tAu+fgx3zxQy3qdagQqBLwJVrdyO1TBfUXvfKveMKJZhpvUYoOjM+4g==", + "version": "6.21.1", + "resolved": "https://registry.npmjs.org/undici/-/undici-6.21.1.tgz", + "integrity": "sha512-q/1rj5D0/zayJB2FraXdaWxbhWiNKDvu8naDT2dl1yTlvJp4BLtOcp2a5BvgGNQpYYJzau7tf1WgKv3b+7mqpQ==", "dev": true, "license": "MIT", "engines": { diff --git a/spec/controllers/admin/forms_controller_spec.rb b/spec/controllers/admin/forms_controller_spec.rb index 460b67b62..9c1816b9d 100644 --- a/spec/controllers/admin/forms_controller_spec.rb +++ b/spec/controllers/admin/forms_controller_spec.rb @@ -134,7 +134,7 @@ context 'with valid params' do before do - form.questions.create!(text: "Question one", question_type: :text_field, form_section: form.form_sections.first, answer_field: :answer_01) + form.questions.create!(text: "Question one", question_type: :text_field, form_section: form.form_sections.first, answer_field: :answer_01, position: 1) form.created_at = Time.now - 2.weeks 20.times { FactoryBot.create(:submission, form: form) } form.save! diff --git a/spec/controllers/admin/questions_controller_spec.rb b/spec/controllers/admin/questions_controller_spec.rb index 344817215..b38d68678 100644 --- a/spec/controllers/admin/questions_controller_spec.rb +++ b/spec/controllers/admin/questions_controller_spec.rb @@ -65,15 +65,6 @@ end end - describe 'GET #new' do - let(:form) { FactoryBot.create(:form, :open_ended_form, organization:) } - - it 'returns a success response' do - get :new, params: { form_id: form.short_uuid, form_section_id: form.form_sections.first.id }, session: valid_session - expect(response).to be_successful - end - end - describe 'GET #edit' do it 'returns a success response' do question = Question.create! valid_attributes diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index e0511aa6c..f84e8f8cc 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -9,6 +9,32 @@ let!(:question) { form.questions.first } let!(:new_question) { Question.create(form:, answer_field: 'answer_01') } + context 'unsaved question' do + let!(:unsaved_question) { FactoryBot.build(:question, form:, position: nil, answer_field: nil) } + + before do + unsaved_question.save + end + + it 'validates form section' do + expect(unsaved_question.errors.messages[:form_section]).to eq(["must exist"]) + end + + it 'ensures an answer_field' do + expect(unsaved_question.errors.messages[:answer_field]).to eq(["can't be blank"]) + end + + it 'ensures a unique answer_field' do + unsaved_question.answer_field = 'answer_01' + unsaved_question.save + expect(unsaved_question.errors.messages[:answer_field]).to eq(["has already been taken"]) + end + + it 'ensures position' do + expect(unsaved_question.errors.messages[:position]).to eq(["can't be blank"]) + end + end + context '' do before do expect(question.answer_field).to eq('answer_01')