Skip to content

Instantly share code, notes, and snippets.

@osbre
Created June 25, 2024 13:29
Show Gist options
  • Save osbre/f80cfe5d885b9dc7599b583e7788c1d1 to your computer and use it in GitHub Desktop.
Save osbre/f80cfe5d885b9dc7599b583e7788c1d1 to your computer and use it in GitHub Desktop.
module Types
class AppendSuffixType < ActiveRecord::Type::String
def initialize(suffix)
@suffix = suffix.to_s
end
def cast_value(value)
value.to_s + @suffix unless value.nil?
end
def serialize(value)
value.chomp(@suffix) unless value.nil?
end
def changed_in_place?(original_value_for_database, value)
original_value_for_database != serialize(value)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment