Skip to content
Open
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
2 changes: 2 additions & 0 deletions app/controllers/admin/forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Admin
class FormsController < AdminController
respond_to :html, :js

before_action :set_paper_trail_whodunnit

skip_before_action :verify_authenticity_token, only: [:js]
before_action :set_form_for_auth_check, only: [:example], prepend: true
# Only bypass authentication for example preview when this is a template form
Expand Down
1 change: 1 addition & 0 deletions app/controllers/admin/question_options_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Admin
class QuestionOptionsController < AdminController
before_action :set_paper_trail_whodunnit
before_action :set_question, only: %i[new create create_other show edit update destroy sort]
before_action :set_question_option, only: %i[show edit update destroy]

Expand Down
1 change: 1 addition & 0 deletions app/controllers/admin/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Admin
class QuestionsController < AdminController
before_action :set_paper_trail_whodunnit
before_action :set_form, only: %i[create show edit update sort destroy]
before_action :set_question, only: %i[show edit update destroy]

Expand Down
1 change: 1 addition & 0 deletions app/models/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class Form < ApplicationRecord
include AASM
has_paper_trail

belongs_to :organization
belongs_to :service, optional: true
Expand Down
2 changes: 2 additions & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Question < ApplicationRecord
has_paper_trail

belongs_to :form, counter_cache: true
belongs_to :form_section
has_many :question_options, dependent: :destroy
Expand Down
2 changes: 2 additions & 0 deletions app/models/question_option.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class QuestionOption < ApplicationRecord
has_paper_trail

belongs_to :question

before_validation :set_default_value_from_text
Expand Down
8 changes: 8 additions & 0 deletions spec/controllers/admin/forms_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@
put :update, params: { id: form.to_param, form: valid_attributes }, session: valid_session
expect(response).to redirect_to delivery_admin_form_path(form)
end

it 'records a version attributed to the current user' do
form = Form.create! valid_attributes
put :update, params: { id: form.to_param, form: { name: 'Audited Form Name' } }, session: valid_session
form.reload
expect(form.versions.last.event).to eq('update')
expect(form.versions.last.whodunnit).to eq(admin.id.to_s)
end
end

context 'with invalid params' do
Expand Down
10 changes: 10 additions & 0 deletions spec/controllers/admin/question_options_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@
put :update, params: { id: question_option.to_param, question_option: valid_attributes }, session: valid_session
expect(response).to redirect_to(question_option)
end

it 'records a version attributed to the current user' do
form = FactoryBot.create(:form, :open_ended_form, organization:)
question = FactoryBot.create(:question, form:, form_section: form.form_sections.first, question_type: 'checkbox', answer_field: 'answer_02')
question_option = QuestionOption.create!(question:, text: 'Option 1', position: 1)
put :update, params: { form_id: form.short_uuid, question_id: question.id, id: question_option.to_param, question_option: { text: 'Updated option text' } }, session: valid_session, format: :js
question_option.reload
expect(question_option.versions.last.event).to eq('update')
expect(question_option.versions.last.whodunnit).to eq(admin.id.to_s)
end
end

context 'with invalid params' do
Expand Down
9 changes: 9 additions & 0 deletions spec/controllers/admin/questions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@
put :update, params: { id: question.to_param, question: valid_attributes }, session: valid_session
expect(response).to redirect_to(question)
end

it 'records a version attributed to the current user' do
form = FactoryBot.create(:form, :open_ended_form, organization:)
question = form.questions.first
put :update, params: { form_id: form.short_uuid, id: question.to_param, question: { text: 'Audited question text' } }, session: valid_session
question.reload
expect(question.versions.last.event).to eq('update')
expect(question.versions.last.whodunnit).to eq(admin.id.to_s)
end
end

context 'with invalid params' do
Expand Down
21 changes: 21 additions & 0 deletions spec/models/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,25 @@
end
end
end

describe 'versioning' do
it 'records a version when a form is created' do
new_form = FactoryBot.create(:form, organization:)
expect(new_form.versions.count).to eq(1)
expect(new_form.versions.last.event).to eq('create')
end

it 'records a version with a changeset when a form is updated' do
form.update!(name: 'Updated Form Name')
expect(form.versions.last.event).to eq('update')
expect(form.versions.last.changeset).to have_key('name')
expect(form.versions.last.changeset['name'].last).to eq('Updated Form Name')
end

it 'records a version when a form is destroyed' do
new_form = FactoryBot.create(:form, organization:)
new_form.destroy!
expect(new_form.versions.last.event).to eq('destroy')
end
end
end
21 changes: 21 additions & 0 deletions spec/models/question_option_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,25 @@
expect(@question_option2.errors.messages).to eq({ question_option: ["only one 'other_option' can be true for a question"] })
end
end

describe 'versioning' do
let!(:question_option) { QuestionOption.create!(text: 'Option 1', question: option_question, position: 1) }

it 'records a version when a question option is created' do
expect(question_option.versions.count).to eq(1)
expect(question_option.versions.last.event).to eq('create')
end

it 'records a version with a changeset when a question option is updated' do
question_option.update!(text: 'Updated option text')
expect(question_option.versions.last.event).to eq('update')
expect(question_option.versions.last.changeset).to have_key('text')
expect(question_option.versions.last.changeset['text'].last).to eq('Updated option text')
end

it 'records a version when a question option is destroyed' do
question_option.destroy!
expect(question_option.versions.last.event).to eq('destroy')
end
end
end
20 changes: 20 additions & 0 deletions spec/models/question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,24 @@
end
end
end

describe 'versioning' do
it 'records a version when a question is created' do
created_question = FactoryBot.create(:question, form:, form_section: form.form_sections.first, answer_field: 'answer_02')
expect(created_question.versions.count).to eq(1)
expect(created_question.versions.last.event).to eq('create')
end

it 'records a version with a changeset when a question is updated' do
question.update!(text: 'Updated question text')
expect(question.versions.last.event).to eq('update')
expect(question.versions.last.changeset).to have_key('text')
expect(question.versions.last.changeset['text'].last).to eq('Updated question text')
end

it 'records a version when a question is destroyed' do
question.destroy!
expect(question.versions.last.event).to eq('destroy')
end
end
end