diff --git a/lib/drop_privileges_validator.rb b/lib/drop_privileges_validator.rb new file mode 100644 index 0000000..f735ee1 --- /dev/null +++ b/lib/drop_privileges_validator.rb @@ -0,0 +1,27 @@ +module ActiveModel + module Validations + class DropPrivilegesValidator < EachValidator + def initialize(options) + super + setup!(options[:class]) + end + + def validate_each(record, attribute, _value) + record.errors.add(attribute, options[:message]) if record.drop_privileges + end + + private + + def setup!(klass) + klass.send(:attr_reader, :drop_privileges) unless klass.method_defined?(:drop_privileges) + klass.send(:attr_writer, :drop_privileges) unless klass.method_defined?(:drop_privileges=) + end + end + + module HelperMethods + def validates_drop_privileges(*attr_names) + validates_with DropPrivilegesValidator, _merge_attributes(attr_names) + end + end + end +end