Created
February 13, 2016 21:15
-
-
Save forest/657fd558a9c05416cf81 to your computer and use it in GitHub Desktop.
nullify_blank.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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