How can I refactor this coordinate validation routine so that I can use
this method with other fields of the same type, for instance, with
“full_last_position” and also maintain the cleanliness of the code and
avoiding repeating method code?
class Provider < ActiveRecord::Base
include UtilsModule
attr_accessor :full_target_area_coordinate, :full_last_position # this
field validates and saves coordinate
validate :full_target_area_coordinate, :is_valid_coordinate?
def is_valid_coordinate? # searches local geo_codes table for valid
zip
coord = self.full_target_area_coordinate.to_str
match = coord.match(/^(-?\d{1,2}.\d{6}),\s*(-?\d{1,2}.\d{6})$/)
if match.nil? # invalid format
errors.add(:target_area_coordinate, :invalid)
return false
end
self.target_area_coordinate =
UtilsModule.convert_coordinate(self.full_target_area_coordinate)
return true
end
end