Browse Source

validate nulls based on form question

pull/1148/head
Phil Lee 3 years ago
parent
commit
90d168a171
  1. 21
      app/services/bulk_upload/lettings/row_parser.rb
  2. 2
      spec/services/bulk_upload/lettings/row_parser_spec.rb

21
app/services/bulk_upload/lettings/row_parser.rb

@ -156,6 +156,7 @@ class BulkUpload::Lettings::RowParser
super
validate_data_types
validate_nulls
log.valid?
@ -165,12 +166,12 @@ class BulkUpload::Lettings::RowParser
end
end
private
def questions
log.form.subsections.flat_map { |ss| ss.applicable_questions(log) }
end
private
def log
@log ||= LettingsLog.new(attributes_for_log)
end
@ -179,10 +180,24 @@ private
field_mapping.find { |h| h[:attribute] == attribute }[:name]
end
def validate_nulls
field_mapping.each do |hash|
question = questions.find { |q| q.id == hash[:question_id] }
next unless question
completed = question.completed?(log)
unless completed
errors.add(hash[:name], :blank)
end
end
end
def field_mapping
[
{ name: :field_1, attribute: :lettype },
{ name: :field_7, attribute: :tenancycode },
{ name: :field_7, attribute: :tenancycode, question_id: "tenancycode" },
{ name: :field_134, attribute: :renewal },
]
end

2
spec/services/bulk_upload/lettings/row_parser_spec.rb

@ -92,7 +92,7 @@ RSpec.describe BulkUpload::Lettings::RowParser do
context "when null" do
let(:attributes) { { field_7: nil } }
xit "returns an error" do
it "returns an error" do
expect(parser.errors[:field_7]).to be_present
end
end

Loading…
Cancel
Save