Browse Source
* Working except for selenium checkbox issues * Remove old debug message * Make sure we only run the stimulus init once per page load * Revert that * Rubocop * Use data targets rather than id lookup, so that coupling is explicit * Use fetch * Schema update * No schema changes introduced here * Add a request spec for the new controller * Schema order did change * map over forEach * Update factory fields * Int for booleans :( * Fix spec fields * Lazy createpull/80/head
Daniel Baark
3 years ago
committed by
GitHub
14 changed files with 142 additions and 28 deletions
@ -0,0 +1,20 @@
|
||||
class SoftValidationsController < ApplicationController |
||||
def show |
||||
@case_log = CaseLog.find(params[:case_log_id]) |
||||
page_key = request.env["PATH_INFO"].split("/")[-2] |
||||
form = FormHandler.instance.get_form("2021_2022") |
||||
page = form.all_pages[page_key] |
||||
if page_requires_soft_validation_override?(page) |
||||
errors = @case_log.soft_errors.values.first |
||||
render json: { show: true, label: errors.message, hint: errors.hint_text } |
||||
else |
||||
render json: { show: false } |
||||
end |
||||
end |
||||
|
||||
private |
||||
|
||||
def page_requires_soft_validation_override?(page) |
||||
@case_log.soft_errors.present? && @case_log.soft_errors.keys.first == page["soft_validations"]&.keys&.first |
||||
end |
||||
end |
@ -0,0 +1,28 @@
|
||||
import { Controller } from "@hotwired/stimulus" |
||||
|
||||
export default class extends Controller { |
||||
static targets = [ "override" ] |
||||
|
||||
initialize() { |
||||
let url = window.location.href + "/soft_validations" |
||||
let div = this.overrideTarget |
||||
fetch(url, { headers: { accept: "application/json" } }) |
||||
.then(response => response.json()) |
||||
.then((response) => { |
||||
if(response["show"]){ |
||||
div.style.display = "block" |
||||
let innerHTML = div.innerHTML |
||||
innerHTML = innerHTML.replace("soft-validations-placeholder-message", response["label"]) |
||||
innerHTML = innerHTML.replace("soft-validations-placeholder-hint-text", response["hint"]) |
||||
div.innerHTML = innerHTML |
||||
} else { |
||||
div.style.display = "none" |
||||
let buttons = document.getElementsByName(`case_log[override_net_income_validation][]`) |
||||
Object.entries(buttons).map(([idx, button]) => { |
||||
button.checked = false |
||||
}) |
||||
} |
||||
} |
||||
) |
||||
} |
||||
} |
@ -1,11 +1,14 @@
|
||||
<div class="govuk-form-group govuk-form-group--error"> |
||||
<%= f.govuk_check_boxes_fieldset @case_log.soft_errors.keys.first, |
||||
legend: { text: @case_log.soft_errors.values.first.message, size: "l" }, |
||||
hint: { text: @case_log.soft_errors.values.first.hint_text } do %> |
||||
<div class="govuk-form-group govuk-form-group--error" |
||||
data-controller="soft-validations" |
||||
data-soft-validations-target="override" |
||||
style='display:none;'> |
||||
|
||||
<%= f.govuk_check_box @case_log.soft_errors.keys.first, @case_log.soft_errors.keys.first, |
||||
label: { text: "Yes" }, |
||||
checked: f.object.send(@case_log.soft_errors.keys.first) |
||||
<%= f.govuk_check_boxes_fieldset page_info["soft_validations"]&.keys&.first, |
||||
legend: { text: "soft-validations-placeholder-message", size: "l" }, |
||||
hint: { text: "soft-validations-placeholder-hint-text" } do %> |
||||
|
||||
<%= f.govuk_check_box page_info["soft_validations"]&.keys&.first, page_info["soft_validations"]&.keys&.first, |
||||
label: { text: "Yes" } |
||||
%> |
||||
<% end %> |
||||
</div> |
||||
|
@ -0,0 +1,9 @@
|
||||
class ChangeNetIncomeOveride < ActiveRecord::Migration[6.1] |
||||
def up |
||||
change_column :case_logs, :override_net_income_validation, "integer USING CAST(override_net_income_validation AS integer)" |
||||
end |
||||
|
||||
def down |
||||
change_column :case_logs, :override_net_income_validation, "boolean USING override_net_income_validation::boolean" |
||||
end |
||||
end |
@ -0,0 +1,39 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe SoftValidationsController, type: :request do |
||||
let(:params) { { case_log_id: case_log.id } } |
||||
let(:url) { "/case_logs/#{case_log.id}/net_income/soft_validations" } |
||||
|
||||
before do |
||||
get url, params: {} |
||||
end |
||||
|
||||
describe "GET #show" do |
||||
context "Soft validation overide required" do |
||||
let(:case_log) { FactoryBot.create(:case_log, :soft_validations_triggered) } |
||||
|
||||
it "returns a success response" do |
||||
expect(response).to be_successful |
||||
end |
||||
|
||||
it "returns a json with the soft validation fields" do |
||||
json_response = JSON.parse(response.body) |
||||
expect(json_response["show"]).to eq(true) |
||||
expect(json_response["label"]).to match(/Are you sure this is correct?/) |
||||
end |
||||
end |
||||
|
||||
context "Soft validation overide not required" do |
||||
let(:case_log) { FactoryBot.create(:case_log, :in_progress) } |
||||
|
||||
it "returns a success response" do |
||||
expect(response).to be_successful |
||||
end |
||||
|
||||
it "returns a json with the soft validation fields" do |
||||
json_response = JSON.parse(response.body) |
||||
expect(json_response["show"]).to eq(false) |
||||
end |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue