I have two custom validation method for two class attributes. Both share the same code, but for different attrs. Could you help me reduce code duplication here?
validates validation_fcn_for_attr_1
validates validation_fcn_for_attr_2
def validation_fcn_for_attr1:
.
<same body with attr1>
.
end
def validation_fcn_for_attr2:
.
<same body with attr2>
.
end
Here is my use case:
validate :verify_valid_locations
validate :verify_loactions_for_vacation
validate :another_validation_fcn
validates :travel_dist, number: {greater_than: 0}
def verify_valid_locations
acceptable_locations = ["C", "T", "G"]
unless locations.in? acceptable_locations
errors.add(:base, 'Acceptable towns for move are "C", "T", "G"')
end
end
def verify_loactions_for_vacation
acceptable_locations = ["C", "T", "G"]
unless vacation_locations.in? acceptable_locations
errors.add(:base, 'Acceptable towns for paid vacation are "C", "T", "G"')
end
end