Skip to content

Instantly share code, notes, and snippets.

@forest
Created February 13, 2016 21:15
Show Gist options
  • Save forest/657fd558a9c05416cf81 to your computer and use it in GitHub Desktop.
Save forest/657fd558a9c05416cf81 to your computer and use it in GitHub Desktop.
nullify_blank.rb
module Virtus
class Attribute
# Attribute extension which nullifies blank attributes when coercion failed
#
module NullifyBlank
# @see [Attribute#coerce]
#
# @api public
def coerce(input)
output = super
if !value_coerced?(output) && input.blank?
nil
# Added to nullify anything that is blank not just strings.
elsif output.blank?
nil
else
output
end
end
end # NullifyBlank
end # Attribute
end # Virtus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment