Skip to content

Commit

Permalink
ensure unique position for form_sections
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwoldatwork committed Mar 24, 2023
1 parent e8227ed commit 8bdac6b
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/models/form_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class FormSection < ApplicationRecord
has_many :questions

validates :position, presence: true
validates :position, uniqueness: { scope: :form_id }

after_commit do |form_section|
FormCache.invalidate(form_section.form.short_uuid) if form_section.persisted?
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/form_sections_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@

describe 'DELETE #destroy' do
let(:form) { FactoryBot.create(:form, organization:, user: admin) }
let!(:form_section) { FactoryBot.create(:form_section, form:) }
let!(:form_section) { form.form_sections.first }

before do
sign_in(admin)
Expand Down
3 changes: 1 addition & 2 deletions spec/controllers/admin/question_options_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@

describe 'GET #new' do
let(:form) { FactoryBot.create(:form, organization:, user: admin) }
let!(:form_section) { FactoryBot.create(:form_section, form:) }
let(:question) { FactoryBot.create(:question, :with_radio_buttons, form:, form_section:) }
let(:question) { FactoryBot.create(:question, :with_radio_buttons, form:, form_section: form.form_sections.first) }

it 'returns a success response' do
get :new, params: { form_id: form.id, question_id: question.id }, session: valid_session
Expand Down
3 changes: 1 addition & 2 deletions spec/controllers/admin/questions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@

describe 'GET #new' do
let(:form) { FactoryBot.create(:form, :open_ended_form, organization:, user: admin) }
let(:form_section) { FactoryBot.create(:form_section, form:, position: 1, title: 'Section1') }

it 'returns a success response' do
get :new, params: { form_id: form.short_uuid, form_section_id: form_section.id }, session: valid_session
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
Expand Down
2 changes: 2 additions & 0 deletions spec/factories/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,15 @@
FactoryBot.create(:question,
form: f,
answer_field: :answer_01,
position: 1,
question_type: 'text_field',
form_section: f.form_sections.first,
text: 'Test Text Field')

FactoryBot.create(:question,
form: f,
answer_field: :answer_02,
position: 2,
question_type: 'textarea',
form_section: f.form_sections.first,
text: 'Test Open Area')
Expand Down
11 changes: 5 additions & 6 deletions spec/features/admin/forms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@
end

describe 'with at least 2 Sections' do
let!(:form_section2) { FactoryBot.create(:form_section, form:) }
let!(:form_section2) { FactoryBot.create(:form_section, form:, position: 2) }

before do
visit questions_admin_form_path(form)
Expand Down Expand Up @@ -1019,7 +1019,7 @@

describe 'deleting Questions' do
let(:form2) { FactoryBot.create(:form, :custom, organization:, user: admin) }
let(:form_section2) { FactoryBot.create(:form_section, form: form2) }
let(:form_section2) { FactoryBot.create(:form_section, form: form2, position: 2) }
let!(:question) { FactoryBot.create(:question, form: form2, form_section: form_section2) }
let!(:user_role) { FactoryBot.create(:user_role, :form_manager, user: admin, form: form2) }

Expand Down Expand Up @@ -1344,7 +1344,7 @@

describe 'deleting Questions' do
let!(:form2) { FactoryBot.create(:form, :custom, organization:, user: touchpoints_manager) }
let!(:form_section2) { FactoryBot.create(:form_section, form: form2) }
let!(:form_section2) { FactoryBot.create(:form_section, form: form2, position: 2) }
let!(:question) { FactoryBot.create(:question, form: form2, form_section: form_section2) }

context 'with Form Manager permissions' do
Expand All @@ -1368,9 +1368,8 @@
let(:new_title) { 'New Form Section Title' }

before do
form2.reload
visit questions_admin_form_path(form2)
expect(find_all('.form-add-question').size).to eq(2)
expect(page).to have_selector('.form-add-question', count: 2)

find_all('.form-add-question').first.click
fill_in 'question_text', with: 'Question in Form Section 1'
Expand All @@ -1388,7 +1387,7 @@
end

it 'creates the question in the correct Form Section' do
within(('#form_section_1')) do
within('#form_section_1') do
expect(page).to have_content('Question in Form Section 1')
end
within('#form_section_2') do
Expand Down
1 change: 0 additions & 1 deletion spec/models/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
second_form_section = form.form_sections.create(title: 'Section 2', position: 2)
q5 = second_form_section.questions.create!(form: form, answer_field: 'answer_10', text: '10', question_type: 'text_field', position: 5)
q6 = second_form_section.questions.create!(form: form, answer_field: 'answer_04', text: '04', question_type: 'text_field', position: 6)
form.reload
end

it "returns a hash of questions, location_code, and 'standard' attributes" do
Expand Down

0 comments on commit 8bdac6b

Please sign in to comment.