Skip to content

Instantly share code, notes, and snippets.

@justinfrench
Last active April 9, 2017 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinfrench/11175929 to your computer and use it in GitHub Desktop.
Save justinfrench/11175929 to your computer and use it in GitHub Desktop.
def validator_relevant?(validator)
return true unless validator.options.key?(:if) || validator.options.key?(:unless)
conditional = validator.options.key?(:if) ? validator.options[:if] : validator.options[:unless]
result = if conditional.respond_to?(:call)
object.instance_exec(&conditional)
elsif conditional.is_a?(::Symbol) && object.respond_to?(conditional)
object.send(conditional)
else
conditional
end
result = validator.options.key?(:unless) ? !result : !!result
not_required_through_negated_validation! if !result && [:presence, :inclusion, :length].include?(validator.kind)
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment