Skip to content

Instantly share code, notes, and snippets.

@fxn
Last active December 15, 2015 23:59
Show Gist options
  • Select an option

  • Save fxn/5344725 to your computer and use it in GitHub Desktop.

Select an option

Save fxn/5344725 to your computer and use it in GitHub Desktop.
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.class_eval do
prepend Module.new {
def mpq?
type == :mpq
end
def simplified_type(field_type)
field_type == 'mpq' ? :mpq : super
end
def klass
mpq? ? Rational : super
end
def type_cast(string)
if mpq?
string.nil? ? nil : string.to_r
else
super
end
end
def type_cast_code(var_name)
mpq? ? "#{var_name}.to_r" : super
end
}
class << self
prepend Module.new {
def extract_value_from_default(default)
default =~ /\A'(.*)'::mpq\z/ ? $1 : super
end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment