Skip to content

Instantly share code, notes, and snippets.

@XeeD
Last active August 29, 2015 14:13
Show Gist options
  • Save XeeD/763fddaeaa6d41daf43a to your computer and use it in GitHub Desktop.
Save XeeD/763fddaeaa6d41daf43a to your computer and use it in GitHub Desktop.
MobilePhoneLengthValidator
class MobilePhoneLengthValidator < ActiveModel::Validations::LengthValidator
def initialize(*)
super
@options = options.dup
end
def validate_each(*)
options.merge!(configured_validations)
super
end
def kind
:length
end
def check_validity!
# The :maximum key is set dynamically
end
private
def configured_validations
Country.
config_for!(:my_settings, :validations, :mobile_phone, :length).
symbolize_keys
end
end
class MobilePhoneLengthValidator < ActiveModel::EachValidator
def validate_each(*args)
validator = ActiveModel::Validations::LengthValidator.new(options)
validator.validate_each(*args)
end
def kind
:length
end
def options
@options.merge(configuration).merge(attributes: @attributes)
end
private
def configuration
Country.
config_for!(:my_settings, :validations, :mobile_phone, :length).
symbolize_keys
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment