Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
Last active May 31, 2021 07:29
Show Gist options
  • Save alpaca-tc/8b65ca576afafb70a19d0a5581fa0632 to your computer and use it in GitHub Desktop.
Save alpaca-tc/8b65ca576afafb70a19d0a5581fa0632 to your computer and use it in GitHub Desktop.
空白を除去するString型
class StrippedString < ActiveModel::Type::String
# 左右の空白を除去した文字列クラス
#
# @param [Object] value
#
# @return [String, nil]
def cast(value)
super(value)&.strip&.presence
end
# 与えられた値をDBのクエリ用に変換する
#
# @param value [Object]
#
# @return [String, NilClass]
def serialize(value)
cast(value)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment