docbrown/app/validators/enabled_countries_hash_validator.rb
PJ 3f922921e5
Admin setting to control enabled countries for billboard geotargeting (#20083)
* use instance setting for enabled target geolocations

* add validation for enabled geolocations setting

* a start on the UI?

* backend tweaks for UI

* proper crack at autocomplete component

* fix region targeting toggle

* e2e spec
2023-09-12 12:06:00 -04:00

21 lines
814 B
Ruby

class EnabledCountriesHashValidator < ActiveModel::EachValidator
VALID_HASH_VALUES = %i[with_regions without_regions].freeze
def validate_each(record, attribute, value)
if value.blank? || !value.is_a?(Hash)
record.errors.add(attribute,
options[:message] || I18n.t("validators.iso3166_hash_validator.is_blank"))
return
end
unless value.keys.all? { |key| ISO3166::Country.codes.include? key }
record.errors.add(attribute,
options[:message] || I18n.t("validators.iso3166_hash_validator.invalid_key"))
end
return if value.values.all? { |value| VALID_HASH_VALUES.include? value }
record.errors.add(attribute,
options[:message] || I18n.t("validators.iso3166_hash_validator.invalid_value"))
end
end