Skip to content

Commit

Permalink
updating validation for location to have underscore when the value is…
Browse files Browse the repository at this point in the history
… more than one word
  • Loading branch information
juddin927 committed Sep 6, 2024
1 parent 88e8199 commit d5e4851
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Site < ApplicationRecord

# Removing blank spaces from Site name entered
before_validation :check_and_prompt_name_correction, if: -> { name.present? && (name.start_with?("FITS") || name.start_with?("MOJO")) && new_record? }
before_validation :enforce_uppercase_prefix, if: -> { name.present? && new_record? }

validates :name, presence: true
validates :name, uniqueness: { case_sensitive: false }, unless: :skip_uniqueness_validation?
Expand Down Expand Up @@ -73,14 +74,29 @@ def self.ransackable_attributes(_auth_object = nil)

# rubocop:enable Lint/IneffectiveAccessModifier

def enforce_uppercase_prefix
if name.present?
suggested_name = name.sub(/\A(fits|mojo)/i) { |match| match.upcase }

# Check if the name needs to be corrected
if name != suggested_name
errors.add(:name, "Suggested Name: '#{suggested_name}'. Please confirm if this is acceptable.")
throw :abort
end
end
end

def check_and_prompt_name_correction
cleaned_name = name.strip.gsub(/\s*-\s*/, "-").gsub(/\s+/, "")
cleaned_name = name.strip
cleaned_parts = cleaned_name.split('-').first(3).join('-').gsub(/\s+/, "")
location_part = cleaned_name.split('-').size > 3 ? cleaned_name.split('-')[3..].join('-').strip.gsub(/\s+/, "_") : ""
corrected_name = location_part.present? ? "#{cleaned_parts}-#{location_part}" : cleaned_parts

if name != cleaned_name
errors.add(:name, "Suggested Name: '#{cleaned_name}'. Please confirm if this is acceptable.")
if name != corrected_name
errors.add(:name, "Suggested Name: '#{corrected_name}'. Please confirm if this is acceptable.")
throw :abort
else
self.name = cleaned_name
self.name = corrected_name
end
end
end

0 comments on commit d5e4851

Please sign in to comment.