Browse Source

Refactor format_ending

pull/2674/head
Manny Dinssa 2 years ago
parent
commit
bde6e31689
  1. 19
      app/helpers/formatting_helper.rb
  2. 12
      app/services/bulk_upload/lettings/year2023/row_parser.rb
  3. 8
      app/services/bulk_upload/lettings/year2024/row_parser.rb
  4. 12
      app/services/bulk_upload/sales/year2023/row_parser.rb
  5. 8
      app/services/bulk_upload/sales/year2024/row_parser.rb

19
app/helpers/formatting_helper.rb

@ -1,14 +1,21 @@
module FormattingHelper module FormattingHelper
def format_ending(value) def format_ending(text)
return value if value.blank? return text if text.blank?
modified_text = lowercase_first_letter(text)
ensure_sentence_ending(modified_text)
end
def ensure_sentence_ending(text)
return text if text.blank?
ends_with_any_punctuation = value.match?(/[[:punct:]]\z/) ends_with_any_punctuation = text.match?(/[[:punct:]]\z/)
ends_with_special_char = value.match?(/[%(){}\[\]]\z/) ends_with_special_char = text.match?(/[%(){}\[\]]\z/)
ends_with_any_punctuation && !ends_with_special_char ? value : "#{value}." ends_with_any_punctuation && !ends_with_special_char ? text : "#{text}."
end end
def downcase_first_letter(text) def lowercase_first_letter(text)
return text if text.blank? return text if text.blank?
text[0].downcase + text[1..] text[0].downcase + text[1..]

12
app/services/bulk_upload/lettings/year2023/row_parser.rb

@ -544,9 +544,9 @@ private
fields.each do |field| fields.each do |field|
if setup_question?(question) if setup_question?(question)
errors.add(field, I18n.t("validations.invalid_option", question: format_ending(downcase(QUESTIONS[field]))), category: :setup) errors.add(field, I18n.t("validations.invalid_option", question: format_ending(QUESTIONS[field])), category: :setup)
else else
errors.add(field, I18n.t("validations.invalid_option", question: format_ending(downcase(QUESTIONS[field])))) errors.add(field, I18n.t("validations.invalid_option", question: format_ending(QUESTIONS[field])))
end end
end end
end end
@ -764,14 +764,14 @@ private
fields.each do |field| fields.each do |field|
if errors.select { |e| fields.include?(e.attribute) }.none? if errors.select { |e| fields.include?(e.attribute) }.none?
question_text = question.error_display_label.presence || "this question." question_text = question.error_display_label.presence || "this question."
errors.add(field, I18n.t("validations.not_answered", question: downcase(question_text)), category: :setup) if field.present? errors.add(field, I18n.t("validations.not_answered", question: format_ending(question_text)), category: :setup) if field.present?
end end
end end
else else
fields.each do |field| fields.each do |field|
unless errors.any? { |e| fields.include?(e.attribute) } unless errors.any? { |e| fields.include?(e.attribute) }
question_text = question.error_display_label.presence || "this question." question_text = question.error_display_label.presence || "this question."
errors.add(field, I18n.t("validations.not_answered", question: downcase(question_text)), category: :not_answered) errors.add(field, I18n.t("validations.not_answered", question: format_ending(question_text)), category: :not_answered)
end end
end end
end end
@ -1607,8 +1607,4 @@ private
def is_carehome def is_carehome
field_127.present? ? 1 : 0 field_127.present? ? 1 : 0
end end
def downcase(text)
downcase_first_letter(text)
end
end end

8
app/services/bulk_upload/lettings/year2024/row_parser.rb

@ -568,9 +568,9 @@ private
fields.each do |field| fields.each do |field|
if setup_question?(question) if setup_question?(question)
errors.add(field, I18n.t("validations.invalid_option", question: format_ending(downcase(QUESTIONS[field]))), category: :setup) errors.add(field, I18n.t("validations.invalid_option", question: format_ending(QUESTIONS[field])), category: :setup)
else else
errors.add(field, I18n.t("validations.invalid_option", question: format_ending(downcase(QUESTIONS[field])))) errors.add(field, I18n.t("validations.invalid_option", question: format_ending(QUESTIONS[field])))
end end
end end
end end
@ -1642,8 +1642,4 @@ private
def bulk_upload_organisation def bulk_upload_organisation
Organisation.find(bulk_upload.organisation_id) Organisation.find(bulk_upload.organisation_id)
end end
def downcase(text)
downcase_first_letter(text)
end
end end

12
app/services/bulk_upload/sales/year2023/row_parser.rb

@ -1243,13 +1243,13 @@ private
if setup_question?(question) if setup_question?(question)
fields.each do |field| fields.each do |field|
unless errors.any? { |e| fields.include?(e.attribute) } unless errors.any? { |e| fields.include?(e.attribute) }
errors.add(field, I18n.t("validations.not_answered", question: downcase(question.error_display_label)), category: :setup) errors.add(field, I18n.t("validations.not_answered", question: question.error_display_label), category: :setup)
end end
end end
else else
fields.each do |field| fields.each do |field|
unless errors.any? { |e| fields.include?(e.attribute) } unless errors.any? { |e| fields.include?(e.attribute) }
errors.add(field, I18n.t("validations.not_answered", question: downcase(question.error_display_label)), category: :not_answered) errors.add(field, I18n.t("validations.not_answered", question: question.error_display_label), category: :not_answered)
end end
end end
end end
@ -1271,13 +1271,13 @@ private
fields.each do |field| fields.each do |field|
if errors[field].none? if errors[field].none?
block_log_creation! block_log_creation!
errors.add(field, I18n.t("validations.invalid_option", question: format_ending(downcase(QUESTIONS[field]))), category: :setup) errors.add(field, I18n.t("validations.invalid_option", question: format_ending(QUESTIONS[field])), category: :setup)
end end
end end
else else
fields.each do |field| fields.each do |field|
unless errors.any? { |e| fields.include?(e.attribute) } unless errors.any? { |e| fields.include?(e.attribute) }
errors.add(field, I18n.t("validations.invalid_option", question: format_ending(downcase(QUESTIONS[field])))) errors.add(field, I18n.t("validations.invalid_option", question: format_ending(QUESTIONS[field])))
end end
end end
end end
@ -1335,8 +1335,4 @@ private
errors.add(:field_35, "Buyer 1 cannot be a child under 16.") errors.add(:field_35, "Buyer 1 cannot be a child under 16.")
end end
end end
def downcase(text)
downcase_first_letter(text)
end
end end

8
app/services/bulk_upload/sales/year2024/row_parser.rb

@ -1407,13 +1407,13 @@ private
fields.each do |field| fields.each do |field|
if errors[field].none? if errors[field].none?
block_log_creation! block_log_creation!
errors.add(field, I18n.t("validations.invalid_option", question: format_ending(downcase(QUESTIONS[field]))), category: :setup) errors.add(field, I18n.t("validations.invalid_option", question: format_ending(QUESTIONS[field])), category: :setup)
end end
end end
else else
fields.each do |field| fields.each do |field|
unless errors.any? { |e| fields.include?(e.attribute) } unless errors.any? { |e| fields.include?(e.attribute) }
errors.add(field, I18n.t("validations.invalid_option", question: format_ending(downcase(QUESTIONS[field])))) errors.add(field, I18n.t("validations.invalid_option", question: format_ending(QUESTIONS[field])))
end end
end end
end end
@ -1509,8 +1509,4 @@ private
def bulk_upload_organisation def bulk_upload_organisation
Organisation.find(bulk_upload.organisation_id) Organisation.find(bulk_upload.organisation_id)
end end
def downcase(text)
downcase_first_letter(text)
end
end end

Loading…
Cancel
Save