Created
January 27, 2016 21:43
-
-
Save aldesantis/987b55a3d67a00f901de to your computer and use it in GitHub Desktop.
This file contains hidden or 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 DivisibilityValidator < ActiveModel::EachValidator | |
attr_reader :base | |
def initialize(options) | |
super | |
@base = options[:base] | |
fail ArgumentError, 'You must pass the :base option!' unless base | |
end | |
def validate_each(record, attribute, value) | |
return if value.blank? && options[:allow_blank] | |
return if value.present? && value % base == 0 | |
record.errors.add attribute, :not_divisible, base: base | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment