From 8d4d0d84eef133888894af9c84c91d34a3f39142 Mon Sep 17 00:00:00 2001 From: J Smith Date: Fri, 8 Dec 2017 14:11:28 -0400 Subject: [PATCH] Fix the filtering of attributes validated with SchemaValidations::Validators::NotNilValidator This appears to be a special case where the `name` parameter of `#_filter_validation` is set to `SchemaValidations::Validators::NotNilValidator` and the actual name is found in the `:attribute` option in `opts`. Filtering-by-name using the `only`, `except`, etc. configurations see the wrong `name` value in these cases and will not be filtered correctly as a result. --- .../active_record/validations.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/schema_validations/active_record/validations.rb b/lib/schema_validations/active_record/validations.rb index 5fff849..1446799 100644 --- a/lib/schema_validations/active_record/validations.rb +++ b/lib/schema_validations/active_record/validations.rb @@ -198,7 +198,7 @@ def create_schema_validations? #:nodoc: end def validate_logged(method, arg, opts={}) #:nodoc: - if _filter_validation(method, arg) + if _filter_validation(method, arg, opts) msg = "[schema_validations] #{self.name}.#{method} #{arg.inspect}" msg += ", #{opts.inspect[1...-1]}" if opts.any? logger.debug msg if logger @@ -206,12 +206,18 @@ def validate_logged(method, arg, opts={}) #:nodoc: end end - def _filter_validation(macro, name) #:nodoc: + def _filter_validation(macro, name, opts) #:nodoc: config = schema_validations_config types = [macro] - if match = macro.to_s.match(/^validates_(.*)_of$/) - types << match[1].to_sym + + case macro.to_s + when /^validates_(.*)_of$/ + types << Regexp.last_match[1].to_sym + when 'validates_with' + types << name + name = opts[:attributes].first end + return false if config.only and not Array.wrap(config.only).include?(name) return false if config.except and Array.wrap(config.except).include?(name) return false if config.whitelist and Array.wrap(config.whitelist).include?(name)