From ea9a9e0571fc460d30811121e7fbbbd2158f171c Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 13 Jan 2025 15:10:25 +0000 Subject: [PATCH] Update validation documentation generating (#2894) * Update documentation generator to use relevant translation files * Add and use collection_year instead of from/to in log validations --- app/services/documentation_generator.rb | 58 +++++++++++-------- .../{financial.yml => financial.en.yml} | 0 .../20250110150609_add_collection_year.rb | 5 ++ db/schema.rb | 3 +- .../generate_lettings_documentation.rake | 1 + .../set_log_validation_collection_year.rake | 6 ++ ...set_log_validation_collection_year_spec.rb | 29 ++++++++++ spec/services/documentation_generator_spec.rb | 12 ++-- 8 files changed, 81 insertions(+), 33 deletions(-) rename config/locales/validations/lettings/{financial.yml => financial.en.yml} (100%) create mode 100644 db/migrate/20250110150609_add_collection_year.rb create mode 100644 lib/tasks/set_log_validation_collection_year.rake create mode 100644 spec/lib/tasks/set_log_validation_collection_year_spec.rb diff --git a/app/services/documentation_generator.rb b/app/services/documentation_generator.rb index d77a4e093..43426d8d4 100644 --- a/app/services/documentation_generator.rb +++ b/app/services/documentation_generator.rb @@ -18,14 +18,16 @@ class DocumentationGenerator next end - validation_source = method(meth).source + validation = method(meth) + validation_source = validation.source + file_path = validation.source_location[0] helper_methods_source = all_helper_methods.map { |helper_method| if validation_source.include?(helper_method.to_s) method(helper_method).source end }.compact.join("\n") - response = describe_hard_validation(client, meth, validation_source, helper_methods_source, form) + response = describe_hard_validation(client, meth, validation_source, helper_methods_source, form, file_path) next unless response begin @@ -41,19 +43,19 @@ class DocumentationGenerator def describe_bu_validations(client, form, row_parser_class, all_validation_methods, all_helper_methods, field_mapping_for_errors, log_type) all_validation_methods.each do |meth| - if LogValidation.where(validation_name: meth.to_s, bulk_upload_specific: true, from: form.start_date, log_type:).exists? + if LogValidation.where(validation_name: meth.to_s, bulk_upload_specific: true, collection_year: "#{form.start_date.year}/#{form.start_date.year + 1}", log_type:).exists? Rails.logger.info("Validation #{meth} already exists for #{form.start_date.year}") next end - - validation_source = row_parser_class.instance_method(meth).source + validation = row_parser_class.instance_method(meth) + validation_source = validation.source helper_methods_source = all_helper_methods.map { |helper_method| if validation_source.include?(helper_method.to_s) row_parser_class.instance_method(helper_method).source end }.compact.join("\n") - response = describe_hard_validation(client, meth, validation_source, helper_methods_source, form) + response = describe_hard_validation(client, meth, validation_source, helper_methods_source, form, validation.source_location[0]) next unless response begin @@ -69,7 +71,7 @@ class DocumentationGenerator def describe_soft_validations(client, all_validation_methods, all_helper_methods, log_type) validation_descriptions = {} - all_validation_methods.each do |meth| + all_validation_methods[0..5].each do |meth| validation_source = method(meth).source helper_methods_source = all_helper_methods.map { |helper_method| if validation_source.include?(helper_method.to_s) @@ -101,8 +103,8 @@ class DocumentationGenerator private - def describe_hard_validation(client, meth, validation_source, helper_methods_source, form) - en_yml = File.read("./config/locales/en.yml") + def describe_hard_validation(client, meth, validation_source, helper_methods_source, form, file_path) + en_yml = File.read(translation_file_path(form, file_path)) begin client.chat( @@ -166,14 +168,6 @@ private required: %w[error_message field], }, }, - from: { - type: :number, - description: "the year from which the validation starts. If this validation runs for logs with a startdate after a certain year, specify that year here, only if it is not specified in the validation method, leave this field blank", - }, - to: { - type: :number, - description: "the year in which the validation ends. If this validation runs for logs with a startdate before a certain year, specify that year here, only if it is not specified in the validation method, leave this field blank", - }, validation_type: { type: :string, enum: %w[presence format minimum maximum range inclusion length other], @@ -271,8 +265,7 @@ Look at these helper methods where needed to understand what is being checked in error_message: error["error_message"], case: case_info["case_description"], section: form.get_question(error["field"], nil)&.subsection&.id, - from: case_info["from"] || "", - to: case_info["to"] || "", + collection_year: "#{form.start_date.year}/#{form.start_date.year + 1}", validation_type: case_info["validation_type"], hard_soft: "hard", other_validated_models: case_info["other_validated_models"]) @@ -295,8 +288,7 @@ Look at these helper methods where needed to understand what is being checked in error_message: error["error_message"], case: case_info["case_description"], section: form.get_question(error_field, nil)&.subsection&.id, - from: form.start_date, - to: form.start_date + 1.year, + collection_year: "#{form.start_date.year}/#{form.start_date.year + 1}", validation_type: case_info["validation_type"], hard_soft: "hard", other_validated_models: case_info["other_validated_models"], @@ -333,7 +325,7 @@ Look at these helper methods where needed to understand what is being checked in return end - if LogValidation.where(validation_name: validation_depends_on_hash.keys.first, field: page_the_validation_applied_to.questions.first.id, from: form.start_date, log_type:).exists? + if LogValidation.where(validation_name: validation_depends_on_hash.keys.first, field: page_the_validation_applied_to.questions.first.id, collection_year: "#{form.start_date.year}/#{form.start_date.year + 1}", log_type:).exists? Rails.logger.info("Validation #{validation_depends_on_hash.keys.first} already exists for #{page_the_validation_applied_to.questions.first.id} for start year #{form.start_date.year}") return end @@ -360,12 +352,30 @@ Look at these helper methods where needed to understand what is being checked in error_message:, case: case_info, section: form.get_question(page_the_validation_applied_to.questions.first.id, nil)&.subsection&.id, - from: form.start_date, - to: form.start_date + 1.year, + collection_year: "#{form.start_date.year}/#{form.start_date.year + 1}", validation_type: result["validation_type"], hard_soft: "soft", other_validated_models: result["other_validated_models"]) Rails.logger.info("******** described #{validation_depends_on_hash.keys.first} for #{page_the_validation_applied_to.questions.first.id} ********") end + + TRANSLATION_FILE_MAPPINGS = { + "property" => "property_information", + }.freeze + + def translation_file_path(form, file_path) + return "./config/locales/validations/#{form.type}/#{form.start_date.year}/bulk_upload.en.yml" if file_path.include?("bulk_upload") + + file_name = file_path.split("/").last.gsub("_validations.rb", "") + translation_file_name = TRANSLATION_FILE_MAPPINGS[file_name] || file_name + + file_path = "./config/locales/validations/#{form.type}/#{translation_file_name}.en.yml" + return file_path if File.exist?(file_path) + + shared_file_path = "./config/locales/validations/#{translation_file_name}.en.yml" + return shared_file_path if File.exist?(shared_file_path) + + "./config/locales/en.yml" + end end diff --git a/config/locales/validations/lettings/financial.yml b/config/locales/validations/lettings/financial.en.yml similarity index 100% rename from config/locales/validations/lettings/financial.yml rename to config/locales/validations/lettings/financial.en.yml diff --git a/db/migrate/20250110150609_add_collection_year.rb b/db/migrate/20250110150609_add_collection_year.rb new file mode 100644 index 000000000..b56e347c0 --- /dev/null +++ b/db/migrate/20250110150609_add_collection_year.rb @@ -0,0 +1,5 @@ +class AddCollectionYear < ActiveRecord::Migration[7.2] + def change + add_column :log_validations, :collection_year, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index bd7672c24..08f6ae3b6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_12_06_142944) do +ActiveRecord::Schema[7.2].define(version: 2025_01_10_150609) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -450,6 +450,7 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_06_142944) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "checked" + t.string "collection_year" end create_table "merge_request_organisations", force: :cascade do |t| diff --git a/lib/tasks/generate_lettings_documentation.rake b/lib/tasks/generate_lettings_documentation.rake index 3dc8e71b4..e87fd8dca 100644 --- a/lib/tasks/generate_lettings_documentation.rake +++ b/lib/tasks/generate_lettings_documentation.rake @@ -83,6 +83,7 @@ namespace :generate_lettings_documentation do error_message:, case: validation_description, section: form.get_question(field, nil)&.subsection&.id, + collection_year: "#{form.start_date.year}/#{form.start_date.year + 1}", validation_type: validation_name, hard_soft: "hard") end diff --git a/lib/tasks/set_log_validation_collection_year.rake b/lib/tasks/set_log_validation_collection_year.rake new file mode 100644 index 000000000..9468d2b0f --- /dev/null +++ b/lib/tasks/set_log_validation_collection_year.rake @@ -0,0 +1,6 @@ +desc "Sets value for collection_year log validations depending on the from value" +task set_log_validation_collection_year: :environment do + LogValidation.all.each do |log_validation| + log_validation.update(collection_year: "#{log_validation.from.year}/#{log_validation.from.year + 1}") + end +end diff --git a/spec/lib/tasks/set_log_validation_collection_year_spec.rb b/spec/lib/tasks/set_log_validation_collection_year_spec.rb new file mode 100644 index 000000000..0e68a3b88 --- /dev/null +++ b/spec/lib/tasks/set_log_validation_collection_year_spec.rb @@ -0,0 +1,29 @@ +require "rails_helper" +require "rake" + +RSpec.describe "set_log_validation_collection_year" do + describe ":set_log_validation_collection_year", type: :task do + subject(:task) { Rake::Task["set_log_validation_collection_year"] } + + before do + Rake.application.rake_require("tasks/set_log_validation_collection_year") + Rake::Task.define_task(:environment) + task.reenable + end + + context "when the rake task is run" do + let(:user) { create(:user) } + + context "and version whodunnit exists for create" do + let!(:log_validation_2023) { LogValidation.create(from: Time.zone.local(2023, 4, 1), to: Time.zone.local(2024, 4, 1)) } + let!(:log_validation_2024) { LogValidation.create(from: Time.zone.local(2024, 4, 1), to: Time.zone.local(2025, 4, 1)) } + + it "sets collection_year" do + task.invoke + expect(log_validation_2023.reload.collection_year).to eq("2023/2024") + expect(log_validation_2024.reload.collection_year).to eq("2024/2025") + end + end + end + end +end diff --git a/spec/services/documentation_generator_spec.rb b/spec/services/documentation_generator_spec.rb index d214aaa9e..8c1d82726 100644 --- a/spec/services/documentation_generator_spec.rb +++ b/spec/services/documentation_generator_spec.rb @@ -106,8 +106,7 @@ describe DocumentationGenerator do expect(any_validation.field).not_to be_empty expect(any_validation.error_message).not_to be_empty expect(any_validation.case).to eq("Provided values fulfill the description") - expect(any_validation.from).not_to be_nil - expect(any_validation.to).not_to be_nil + expect(any_validation.collection_year).not_to be_nil expect(any_validation.validation_type).to eq("format") expect(any_validation.hard_soft).to eq("soft") expect(any_validation.other_validated_models).to eq("User") @@ -137,8 +136,7 @@ describe DocumentationGenerator do expect(any_validation.field).not_to be_empty expect(any_validation.error_message).not_to be_empty expect(any_validation.case).to eq("Provided values fulfill the description") - expect(any_validation.from).not_to be_nil - expect(any_validation.to).not_to be_nil + expect(any_validation.collection_year).not_to be_nil expect(any_validation.validation_type).to eq("format") expect(any_validation.hard_soft).to eq("soft") expect(any_validation.other_validated_models).to eq("User") @@ -165,8 +163,7 @@ describe DocumentationGenerator do expect(any_validation.field).to eq("ppostcode_full") expect(any_validation.error_message).to eq("Enter a valid postcode") expect(any_validation.case).to eq("Previous postcode is known and current postcode is blank") - expect(any_validation.from).not_to be_nil - expect(any_validation.to).not_to be_nil + expect(any_validation.collection_year).to eq("2023/2024") expect(any_validation.validation_type).to eq("format") expect(any_validation.hard_soft).to eq("hard") expect(any_validation.other_validated_models).to eq("User") @@ -218,8 +215,7 @@ describe DocumentationGenerator do expect(any_validation.field).to eq("ppostcode_full") expect(any_validation.error_message).to eq("Enter a valid postcode") expect(any_validation.case).to eq("Previous postcode is known and current postcode is blank") - expect(any_validation.from).not_to be_nil - expect(any_validation.to).not_to be_nil + expect(any_validation.collection_year).to eq("2023/2024") expect(any_validation.validation_type).to eq("format") expect(any_validation.hard_soft).to eq("hard") expect(any_validation.other_validated_models).to eq("User")