Permalink
Browse files

[postgres] only type_cast on AR 4.0 + AR 3.1 column definition compat…

…ibility
  • Loading branch information...
1 parent 98e735d commit 44a9436e34615dfa9bb1be21d3653d3ae5ec717b @kares kares committed Apr 3, 2013
Showing with 31 additions and 22 deletions.
  1. +31 −22 lib/arjdbc/postgresql/adapter.rb
@@ -161,36 +161,40 @@ def default_value(default)
# Casts value (which is a String) to an appropriate instance.
def type_cast(value)
return if value.nil?
- return super if encoded?
+ return super if encoded? # respond_to?(:encoded?) only since AR-3.2
# NOTE: we do not use OID::Type
# @oid_type.type_cast value
case type
when :hstore then self.class.string_to_hstore value
when :json then self.class.string_to_json value
when :cidr, :inet, :macaddr then self.class.string_to_cidr value
- when :money
- # Because money output is formatted according to the locale, there are two
- # cases to consider (note the decimal separators):
- # (1) $12,345,678.12
- # (2) $12.345.678,12
- case value
- when /^-?\D+[\d,]+\.\d{2}$/ # (1)
- value.gsub!(/[^-\d.]/, '')
- when /^-?\D+[\d.]+,\d{2}$/ # (2)
- value.gsub!(/[^-\d,]/, '').sub!(/,/, '.')
- end
- self.class.value_to_decimal value
- when /point/ # TODO
- if String === value
- self.class.string_to_point value
- else
- value
- end
when :tsvector then value
- else super
+ else
+ case sql_type
+ when 'money'
+ # Because money output is formatted according to the locale, there
+ # are two cases to consider (note the decimal separators) :
+ # (1) $12,345,678.12
+ # (2) $12.345.678,12
+ case value
+ when /^-?\D+[\d,]+\.\d{2}$/ # (1)
+ value.gsub!(/[^-\d.]/, '')
+ when /^-?\D+[\d.]+,\d{2}$/ # (2)
+ value.gsub!(/[^-\d,]/, '')
+ value.sub!(/,/, '.')
+ end
+ self.class.value_to_decimal value
+ when /^point/
+ if value.is_a?(String)
+ self.class.string_to_point value
+ else
+ value
+ end
+ else super
+ end
end
- end
+ end if AR4_COMPAT
private
@@ -1420,7 +1424,12 @@ class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
def column(name, type = nil, options = {})
super
column = self[name]
- column.array = options[:array]
+ # NOTE: <= 3.1 no #new_column_definition hard-coded ColumnDef.new :
+ # column = self[name] || ColumnDefinition.new(@base, name, type)
+ # thus we simply do not support array column definitions on <= 3.1
+ if column.is_a?(ColumnDefinition)
+ column.array = options[:array]
+ end
self
end

0 comments on commit 44a9436

Please sign in to comment.