Browse Source

Log whenever a user encounters a validation error message to Logit (#654)

pull/657/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
20fef1f1fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/controllers/form_controller.rb
  2. 19
      spec/requests/form_controller_spec.rb

1
app/controllers/form_controller.rb

@ -13,6 +13,7 @@ class FormController < ApplicationController
else else
redirect_path = "case_log_#{@page.id}_path" redirect_path = "case_log_#{@page.id}_path"
session[:errors] = @case_log.errors.to_json session[:errors] = @case_log.errors.to_json
Rails.logger.info "User triggered validation(s) on: #{@case_log.errors.map(&:attribute).join(', ')}"
redirect_to(send(redirect_path, @case_log)) redirect_to(send(redirect_path, @case_log))
end end
else else

19
spec/requests/form_controller_spec.rb

@ -174,26 +174,33 @@ RSpec.describe FormController, type: :request do
} }
end end
before do
post "/logs/#{case_log.id}/form", params:
end
context "with invalid answers" do context "with invalid answers" do
let(:page) { Capybara::Node::Simple.new(response.body) } let(:page) { Capybara::Node::Simple.new(response.body) }
let(:answer) { 2000 } let(:answer) { 2000 }
let(:valid_answer) { 20 } let(:valid_answer) { 20 }
before do
allow(Rails.logger).to receive(:info)
end
it "re-renders the same page with errors if validation fails" do it "re-renders the same page with errors if validation fails" do
post "/logs/#{case_log.id}/form", params: params
expect(response).to redirect_to("/logs/#{case_log.id}/#{page_id.dasherize}") expect(response).to redirect_to("/logs/#{case_log.id}/#{page_id.dasherize}")
follow_redirect! follow_redirect!
expect(page).to have_content("There is a problem") expect(page).to have_content("There is a problem")
end end
it "resets errors when fixed" do it "resets errors when fixed" do
post "/logs/#{case_log.id}/form", params: params
post "/logs/#{case_log.id}/form", params: valid_params post "/logs/#{case_log.id}/form", params: valid_params
get "/logs/#{case_log.id}/#{page_id.dasherize}" get "/logs/#{case_log.id}/#{page_id.dasherize}"
expect(page).not_to have_content("There is a problem") expect(page).not_to have_content("There is a problem")
end end
it "logs that validation was triggered" do
expect(Rails.logger).to receive(:info).with("User triggered validation(s) on: age1").once
post "/logs/#{case_log.id}/form", params: params
end
end end
context "with valid answers" do context "with valid answers" do
@ -209,6 +216,10 @@ RSpec.describe FormController, type: :request do
} }
end end
before do
post "/logs/#{case_log.id}/form", params:
end
it "re-renders the same page with errors if validation fails" do it "re-renders the same page with errors if validation fails" do
expect(response).to have_http_status(:redirect) expect(response).to have_http_status(:redirect)
end end

Loading…
Cancel
Save