Skip to content

Instantly share code, notes, and snippets.

@tylerpaige
Created July 28, 2023 15:02
Show Gist options
  • Save tylerpaige/a40e53544a1fe628e67a2ae27f3cd68c to your computer and use it in GitHub Desktop.
Save tylerpaige/a40e53544a1fe628e67a2ae27f3cd68c to your computer and use it in GitHub Desktop.
Rails custom serializer for newline-separated values
# 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