Created
July 28, 2023 15:02
-
-
Save tylerpaige/a40e53544a1fe628e67a2ae27f3cd68c to your computer and use it in GitHub Desktop.
Rails custom serializer for newline-separated values
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
# Reads newline-separated text from the database as an array | |
# Note that when you present this field for content authors to edit | |
# you will need to join the value into a newline separated text. | |
class NewlineSerializer | |
def self.dump(value) | |
return value if value.is_a?(String) | |
return value.join("\r\n") if value.is_a?(Array) | |
end | |
def self.load(value) | |
return nil if value.blank? | |
return value.split("\r\n").map(&:strip).reject(&:blank?) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment