From 5a7183f450df5046bf1663488bc662e02cfdc36e Mon Sep 17 00:00:00 2001 From: Matthew Phelan Date: Fri, 15 Oct 2021 13:18:57 +0100 Subject: [PATCH 1/3] CLCD-495 ready for review --- app/models/case_log.rb | 16 +++++++++++----- ...update_property_number_of_times_relet_type.rb | 5 +++++ db/schema.rb | 4 ++-- spec/requests/case_log_controller_spec.rb | 5 ++++- 4 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20211015090040_update_property_number_of_times_relet_type.rb diff --git a/app/models/case_log.rb b/app/models/case_log.rb index bcbbc432a..167925e8e 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -1,7 +1,7 @@ class CaseLogValidator < ActiveModel::Validator - # Methods need to be named 'validate_' followed by field name - # this is how the metaprogramming of the method name being - # call in the validate method works. + # Methods to be used on save and continue need to be named 'validate_' + # followed by field name this is how the metaprogramming of the method + # name being call in the validate method works. def validate_tenant_age(record) if record.tenant_age && !/^[1-9][0-9]?$|^120$/.match?(record.tenant_age.to_s) @@ -9,13 +9,19 @@ class CaseLogValidator < ActiveModel::Validator end end + def validate_property_number_of_times_relet(record) + if record.property_number_of_times_relet && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.property_number_of_times_relet.to_s) + record.errors.add :property_number_of_times_relet, "must be between 0 and 20" + end + end + def validate(record) # If we've come from the form UI we only want to validate the specific fields # that have just been submitted. If we're submitting a log via API or Bulk Upload # we want to validate all data fields. question_to_validate = options[:previous_page] if question_to_validate && respond_to?("validate_#{question_to_validate}") - public_send("validate_#{question_to_validate}", record) + public_send("validate_#{question_to_validate}", record) else # This assumes that all methods in this class other than this one are # validations to be run @@ -30,7 +36,7 @@ class CaseLog < ApplicationRecord before_save :update_status! attr_writer :previous_page - + enum status: { "in progress" => 0, "submitted" => 1 } AUTOGENERATED_FIELDS = %w[status created_at updated_at id].freeze diff --git a/db/migrate/20211015090040_update_property_number_of_times_relet_type.rb b/db/migrate/20211015090040_update_property_number_of_times_relet_type.rb new file mode 100644 index 000000000..4e436e2d2 --- /dev/null +++ b/db/migrate/20211015090040_update_property_number_of_times_relet_type.rb @@ -0,0 +1,5 @@ +class UpdatePropertyNumberOfTimesReletType < ActiveRecord::Migration[6.1] + def change + change_column :case_logs, :property_number_of_times_relet, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 3e73a03e0..7202cd339 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.define(version: 2021_10_13_113607) do +ActiveRecord::Schema.define(version: 2021_10_15_090040) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -84,7 +84,6 @@ ActiveRecord::Schema.define(version: 2021_10_13_113607) do t.string "property_void_date" t.string "property_major_repairs" t.string "property_major_repairs_date" - t.string "property_number_of_times_relet" t.string "property_wheelchair_accessible" t.string "net_income" t.string "net_income_frequency" @@ -131,6 +130,7 @@ ActiveRecord::Schema.define(version: 2021_10_13_113607) do t.boolean "reasonable_preference_reason_medical_grounds" t.boolean "reasonable_preference_reason_avoid_hardship" t.boolean "reasonable_preference_reason_do_not_know" + t.integer "property_number_of_times_relet" end end diff --git a/spec/requests/case_log_controller_spec.rb b/spec/requests/case_log_controller_spec.rb index cf874bb86..b4429c4dc 100644 --- a/spec/requests/case_log_controller_spec.rb +++ b/spec/requests/case_log_controller_spec.rb @@ -4,6 +4,7 @@ RSpec.describe CaseLogsController, type: :request do describe "POST #create" do let(:tenant_code) { "T365" } let(:tenant_age) { 35 } + let(:property_number_of_times_relet) { 12 } let(:property_postcode) { "SE11 6TY" } let(:api_username) { "test_user" } let(:api_password) { "test_password" } @@ -27,6 +28,7 @@ RSpec.describe CaseLogsController, type: :request do "tenant_code": tenant_code, "tenant_age": tenant_age, "property_postcode": property_postcode, + "property_number_of_times_relet": property_number_of_times_relet } end @@ -55,11 +57,12 @@ RSpec.describe CaseLogsController, type: :request do context "invalid json params" do let(:tenant_age) { 2000 } + let(:property_number_of_times_relet) { 21 } it "validates case log parameters" do json_response = JSON.parse(response.body) expect(response).to have_http_status(:unprocessable_entity) - expect(json_response["errors"]).to eq(["Tenant age must be between 0 and 120"]) + expect(json_response["errors"]).to match_array(["Tenant age must be between 0 and 120", "Property number of times relet must be between 0 and 20"]) end end From 9de3d9dcecf1ba8b7c4aa19f921fbe78e6bcb872 Mon Sep 17 00:00:00 2001 From: Matthew Phelan Date: Fri, 15 Oct 2021 13:25:14 +0100 Subject: [PATCH 2/3] finishing touches --- app/models/case_log.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 167925e8e..876e42766 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -21,7 +21,7 @@ class CaseLogValidator < ActiveModel::Validator # we want to validate all data fields. question_to_validate = options[:previous_page] if question_to_validate && respond_to?("validate_#{question_to_validate}") - public_send("validate_#{question_to_validate}", record) + public_send("validate_#{question_to_validate}", record) else # This assumes that all methods in this class other than this one are # validations to be run From 6cfdb9a0b7f5dadf470144b84ce2bac8954a85e2 Mon Sep 17 00:00:00 2001 From: Matthew Phelan Date: Fri, 15 Oct 2021 15:03:40 +0100 Subject: [PATCH 3/3] number of times relet validation unit tests --- spec/models/case_log_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index b456e6988..6b0805250 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -13,6 +13,18 @@ RSpec.describe Form, type: :model do it "validates age is over 0" do expect { CaseLog.create!(tenant_age: 0) }.to raise_error(ActiveRecord::RecordInvalid) end + + it "validates number of relets is a number" do + expect { CaseLog.create!(property_number_of_times_relet: "random") }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "validates number of relets is under 20" do + expect { CaseLog.create!(property_number_of_times_relet: 21) }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "validates number of relets is over 0" do + expect { CaseLog.create!(property_number_of_times_relet: 0) }.to raise_error(ActiveRecord::RecordInvalid) + end end describe "status" do