Last active
August 29, 2015 14:13
-
-
Save XeeD/763fddaeaa6d41daf43a to your computer and use it in GitHub Desktop.
MobilePhoneLengthValidator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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