diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index a718756b93a2c310309e6a478e596fccc79c0288..522fa57822cc1c7a4e604f5686c4c61b101bc083 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -69,15 +69,11 @@ def extract_default(default) end private - delegate :extract_scale, to: :cast_type + delegate :extract_scale, :extract_precision, to: :cast_type def extract_limit(sql_type) $1.to_i if sql_type =~ /\((.*)\)/ end - - def extract_precision(sql_type) - $2.to_i if sql_type =~ /^(numeric|decimal|number)\((\d+)(,\d+)?\)/i - end end end # :startdoc: diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb index 80c79642f3339ea580b3ef21e4ec4e0733effc2b..302eb8a114a21ea4e857119dfb27e48f1c82cd13 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb @@ -35,8 +35,6 @@ class << self require 'active_record/connection_adapters/postgresql/array_parser' include PostgreSQL::ArrayParser end - - attr_accessor :money_precision end # :startdoc: @@ -121,17 +119,6 @@ def extract_limit(sql_type) else super end end - - # Extracts the precision from PostgreSQL-specific data types. - def extract_precision(sql_type) - if sql_type == 'money' - self.class.money_precision - elsif sql_type =~ /timestamp/i - $1.to_i if sql_type =~ /\((\d+)\)/ - else - super - end - end end end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb index 1e34c09c8813497fab38643d668b6c30730c6c5f..74e8f30457f732797184c69871aef7369d842a4f 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb @@ -5,6 +5,8 @@ module OID # :nodoc: class Money < Type::Decimal include Infinity + class_attribute :precision + def extract_scale(sql_type) 2 end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 7d1fa0dd08df5724708bd9a542d545bf6322a409..b0cbccb7ba478fe0e4e7c4f9e93983654869f930 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -713,7 +713,7 @@ def connect # Money type has a fixed precision of 10 in PostgreSQL 8.2 and below, and as of # PostgreSQL 8.3 it has a fixed precision of 19. PostgreSQLColumn.extract_precision # should know about this but can't detect it there, so deal with it here. - PostgreSQLColumn.money_precision = (postgresql_version >= 80300) ? 19 : 10 + OID::Money.precision = (postgresql_version >= 80300) ? 19 : 10 configure_connection rescue ::PG::Error => error diff --git a/activerecord/lib/active_record/connection_adapters/type/date_time.rb b/activerecord/lib/active_record/connection_adapters/type/date_time.rb index c34f4c5a53a1c8f109ea19245bf9dddb2f33d467..43203a4cd4deb7e317b1ea04e7c2a4808fb4d1f9 100644 --- a/activerecord/lib/active_record/connection_adapters/type/date_time.rb +++ b/activerecord/lib/active_record/connection_adapters/type/date_time.rb @@ -8,6 +8,10 @@ def type :datetime end + def extract_precision(sql_type) + $1.to_i if sql_type =~ /\((\d+)\)/ + end + private def cast_value(string) diff --git a/activerecord/lib/active_record/connection_adapters/type/numeric.rb b/activerecord/lib/active_record/connection_adapters/type/numeric.rb index a3379831cb0b1a66d73bee5123ce831dac9c2fc3..15081daf8d14dae8b825bfe0a316b189b91572c5 100644 --- a/activerecord/lib/active_record/connection_adapters/type/numeric.rb +++ b/activerecord/lib/active_record/connection_adapters/type/numeric.rb @@ -14,6 +14,10 @@ def type_cast_for_write(value) else super end end + + def extract_precision(sql_type) + $1.to_i if sql_type =~ /\((\d+)(,\d+)?\)/ + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/type/value.rb b/activerecord/lib/active_record/connection_adapters/type/value.rb index 52d9ed9bc0c9974ddcd53727e9142989cbdc3b79..6687c9a2a495168e759f53a238b82c4cd42d5250 100644 --- a/activerecord/lib/active_record/connection_adapters/type/value.rb +++ b/activerecord/lib/active_record/connection_adapters/type/value.rb @@ -3,8 +3,11 @@ module ConnectionAdapters module Type class Value # :nodoc: def type; end + def extract_scale(sql_type); end + def extract_precision(sql_type); end + def type_cast(value) cast_value(value) unless value.nil? end