Skip to content

Instantly share code, notes, and snippets.

@aldesantis
Created January 27, 2016 21:43
Show Gist options
  • Save aldesantis/987b55a3d67a00f901de to your computer and use it in GitHub Desktop.
Save aldesantis/987b55a3d67a00f901de to your computer and use it in GitHub Desktop.
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