Skip to content
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -653,6 +654,7 @@ DEPENDENCIES
bundler-audit
capybara
carrierwave (>= 2.2.1)
csv
database_cleaner
devise (>= 4.8.1)
dotenv
Expand Down
17 changes: 6 additions & 11 deletions app/controllers/admin/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 }
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/forms/_add_question.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
<span class="fa fa-plus"></span>
Add Question
<% end %>
2 changes: 1 addition & 1 deletion app/views/components/forms/edit/_builder.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion spec/controllers/admin/forms_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
9 changes: 0 additions & 9 deletions spec/controllers/admin/questions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions spec/models/question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down