diff --git a/lib/tasks/generate_documentation.rake b/lib/tasks/generate_documentation.rake index 15388f2dc..fec1fbed0 100644 --- a/lib/tasks/generate_documentation.rake +++ b/lib/tasks/generate_documentation.rake @@ -465,4 +465,43 @@ namespace :generate_documentation do end end end + + desc "Generate documentation for lettings numeric validations" + task add_numeric_lettings_validations: :environment do + form = FormHandler.instance.forms["current_lettings"] + + form.numeric_questions.each do |question| + next unless question.min || question.max + + field = question.id + min = [question.prefix, question.min].join("") if question.min + max = [question.prefix, question.max].join("") if question.max + + error_message = I18n.t("validations.numeric.above_min", field:, min:) + validation_name = "minimum" + validation_description = "Field value is lower than the minimum value" + + if min && max + validation_name = "range" + error_message = I18n.t("validations.numeric.within_range", field:, min:, max:) + validation_description = "Field value is lower than the minimum value or higher than the maximum value" + end + + if Validation.where(validation_name:, field:).exists? + + Rails.logger.info("Validation #{validation_name} already exists for #{field}") + next + end + + Validation.create!(log_type: "lettings", + validation_name:, + description: validation_description, + field:, + error_message:, + case: validation_description, + section: form.get_question(field, nil)&.subsection&.id, + validation_type: validation_name, + hard_soft: "hard") + end + end end diff --git a/spec/lib/tasks/generate_documentation_spec.rb b/spec/lib/tasks/generate_documentation_spec.rb index a69f52d32..5b960db02 100644 --- a/spec/lib/tasks/generate_documentation_spec.rb +++ b/spec/lib/tasks/generate_documentation_spec.rb @@ -181,4 +181,37 @@ RSpec.describe "generate_documentation" do end end end + + describe ":add_numeric_lettings_validations", type: :task do + subject(:task) { Rake::Task["generate_documentation:add_numeric_lettings_validations"] } + + before do + Rake.application.rake_require("tasks/generate_documentation") + Rake::Task.define_task(:environment) + task.reenable + end + + context "when the rake task is run" do + it "creates new validation documentation records" do + expect { task.invoke }.to change(Validation, :count) + expect(Validation.where(validation_name: "minimum").count).to be_positive + expect(Validation.where(validation_name: "range").count).to be_positive + any_min_validation = Validation.where(validation_name: "minimum").first + expect(any_min_validation.description).to include("Field value is lower than the minimum value") + expect(any_min_validation.field).not_to be_empty + expect(any_min_validation.error_message).to include("must be at least") + expect(any_min_validation.case).to include("Field value is lower than the minimum value") + expect(any_min_validation.from).to be_nil + expect(any_min_validation.to).to be_nil + expect(any_min_validation.validation_type).to eq("minimum") + expect(any_min_validation.hard_soft).to eq("hard") + expect(any_min_validation.other_validated_models).to be_nil + end + + it "skips if the validation already exists in the database" do + task.invoke + expect { task.invoke }.not_to change(Validation, :count) + end + end + end end