Skip to content

Instantly share code, notes, and snippets.

@kirs
Created April 10, 2014 10:28
Show Gist options
  • Save kirs/10365962 to your computer and use it in GitHub Desktop.
Save kirs/10365962 to your computer and use it in GitHub Desktop.
module ActiveRecord::ConnectionAdapters::PostgreSQLColumn::Cast
# See https://github.com/rails/rails/pull/11477
def quote_and_escape(value)
case value
when "NULL"
value
else
"\"#{value.gsub(/(["\\])/, '\\\\\1')}\""
end
end
# See https://github.com/rails/rails/pull/11159
def array_to_string(value, column, adapter, should_be_quoted = false)
casted_values = value.map do |val|
if val == "NULL"
"\"#{val}\""
elsif Array === val # Special handling of multidimensional arrays
adapter.type_cast(val, column, true)
else
casted_val = adapter.type_cast(val, column, true)
if String === casted_val
quote_and_escape(casted_val)
else
casted_val
end
end
end
"{#{casted_values.join(',')}}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment