class Spira::Validations::UniquenessValidator

Public Class Methods

new(options) click to toggle source
Calls superclass method
# File lib/spira/validations/uniqueness.rb, line 13
def initialize(options)
  super
  @klass = options.fetch(:class)
end

Public Instance Methods

setup(klass) click to toggle source
# File lib/spira/validations/uniqueness.rb, line 9
def setup(klass)
  @klass = klass
end
validate_each(record, attribute, value) click to toggle source
# File lib/spira/validations/uniqueness.rb, line 19
def validate_each(record, attribute, value)
  @klass.find_each(conditions: {attribute => value}) do |other_record|
    if other_record.subject != record.subject
      record.errors.add(attribute, "is already taken")
      break
    end
  end
end