Browse Source

Enable better other field validation labels

pull/619/head
baarkerlounger 3 years ago
parent
commit
490ebee4f6
  1. 6
      app/models/validations/shared_validations.rb
  2. 2
      app/models/validations/tenancy_validations.rb
  3. 13
      spec/models/validations/tenancy_validations_spec.rb

6
app/models/validations/shared_validations.rb

@ -1,9 +1,9 @@
module Validations::SharedValidations module Validations::SharedValidations
def validate_other_field(record, value_other = nil, main_field = nil, other_field = nil) def validate_other_field(record, value_other = nil, main_field = nil, other_field = nil, main_label = nil, other_label = nil)
return unless main_field || other_field return unless main_field || other_field
main_field_label = main_field.to_s.humanize(capitalize: false) main_field_label = main_label || main_field.to_s.humanize(capitalize: false)
other_field_label = other_field.to_s.humanize(capitalize: false) other_field_label = other_label || other_field.to_s.humanize(capitalize: false)
if record[main_field] == value_other && record[other_field].blank? if record[main_field] == value_other && record[other_field].blank?
record.errors.add other_field.to_sym, I18n.t("validations.other_field_missing", main_field_label:, other_field_label:) record.errors.add other_field.to_sym, I18n.t("validations.other_field_missing", main_field_label:, other_field_label:)
end end

2
app/models/validations/tenancy_validations.rb

@ -30,7 +30,7 @@ module Validations::TenancyValidations
end end
def validate_other_tenancy_type(record) def validate_other_tenancy_type(record)
validate_other_field(record, 3, :tenancy, :tenancyother) validate_other_field(record, 3, :tenancy, :tenancyother, "tenancy type", "other tenancy type")
end end
def validate_joint_tenancy(record) def validate_joint_tenancy(record)

13
spec/models/validations/tenancy_validations_spec.rb

@ -198,8 +198,9 @@ RSpec.describe Validations::TenancyValidations do
describe "tenancy type validations" do describe "tenancy type validations" do
let(:field) { "validations.other_field_missing" } let(:field) { "validations.other_field_missing" }
let(:main_field_label) { "tenancy" } let(:main_field_label) { "tenancy type" }
let(:other_field_label) { "tenancyother" } let(:other_field) { "tenancyother" }
let(:other_field_label) { "other tenancy type" }
let(:expected_error) { I18n.t(field, main_field_label:, other_field_label:) } let(:expected_error) { I18n.t(field, main_field_label:, other_field_label:) }
context "when tenancy type is other" do context "when tenancy type is other" do
@ -208,13 +209,13 @@ RSpec.describe Validations::TenancyValidations do
it "validates that other tenancy type is provided" do it "validates that other tenancy type is provided" do
record.tenancyother = nil record.tenancyother = nil
tenancy_validator.validate_other_tenancy_type(record) tenancy_validator.validate_other_tenancy_type(record)
expect(record.errors[other_field_label]).to include(match(expected_error)) expect(record.errors[other_field]).to include(match(expected_error))
end end
it "expects that other tenancy type is provided" do it "expects that other tenancy type is provided" do
record.tenancyother = "Some other tenancy type" record.tenancyother = "Some other tenancy type"
tenancy_validator.validate_other_tenancy_type(record) tenancy_validator.validate_other_tenancy_type(record)
expect(record.errors[other_field_label]).to be_empty expect(record.errors[other_field]).to be_empty
end end
end end
@ -225,14 +226,14 @@ RSpec.describe Validations::TenancyValidations do
record.tenancy = 2 record.tenancy = 2
record.tenancyother = "Some other tenancy type" record.tenancyother = "Some other tenancy type"
tenancy_validator.validate_other_tenancy_type(record) tenancy_validator.validate_other_tenancy_type(record)
expect(record.errors[other_field_label]).to include(match(expected_error)) expect(record.errors[other_field]).to include(match(expected_error))
end end
it "expects that other tenancy type is not provided" do it "expects that other tenancy type is not provided" do
record.tenancy = 1 record.tenancy = 1
record.tenancyother = nil record.tenancyother = nil
tenancy_validator.validate_other_tenancy_type(record) tenancy_validator.validate_other_tenancy_type(record)
expect(record.errors[other_field_label]).to be_empty expect(record.errors[other_field]).to be_empty
end end
end end
end end

Loading…
Cancel
Save