1
0
Fork 0
This repository has been archived on 2023-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/app/validators/application_each_validator.rb

40 lines
792 B
Ruby
Raw Normal View History

2019-03-25 03:04:53 +00:00
# frozen_string_literal: true
class ApplicationEachValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
self.class::Validation.new(self, record, attribute, value).call
end
class Validation
attr_reader :value
delegate :options, to: :@validator
def initialize(validator, record, attribute, value)
@validator = validator
@record = record
@attribute = attribute
@value = value
end
def call
catch :stop_validating do
perform
end
end
def perform
raise NotImplementedError, "#{self.class}#perform"
end
def error(*args)
@record.errors.add @attribute, *args
end
def error!(*args)
error(*args)
throw :stop_validating
end
end
end