diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-02-15 14:21:01 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-02-15 14:23:17 -0700 |
commit | 265dcb779dd4bfd9745e0131f585749d7034c3c7 (patch) | |
tree | b522ffcf5a29bf0a260d811a0d1cbcb84f2262bb | |
parent | 8c837e5fcc2a3ac639b3b93b0024bd2c20d3b6ed (diff) | |
download | rails-265dcb779dd4bfd9745e0131f585749d7034c3c7.tar.gz rails-265dcb779dd4bfd9745e0131f585749d7034c3c7.tar.bz2 rails-265dcb779dd4bfd9745e0131f585749d7034c3c7.zip |
Register adapter specific types with the global type registry
We do this in the adapter classes specifically, so the types aren't
registered if we don't use that adapter. Constants under the PostgreSQL
namespace for example are never loaded if we're using mysql.
7 files changed, 39 insertions, 67 deletions
diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb index 2c475f3cda..87d4bc8578 100644 --- a/activerecord/lib/active_record/attributes.rb +++ b/activerecord/lib/active_record/attributes.rb @@ -210,7 +210,7 @@ module ActiveRecord super attributes_to_define_after_schema_loads.each do |name, (type, options)| if type.is_a?(Symbol) - type = connection.type_for_attribute_options(type, **options.except(:default)) + type = ActiveRecord::Type.lookup(type, **options.except(:default)) end define_attribute(name, type, **options.slice(:default)) diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index 947e11c7bf..1ac909da2e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -144,29 +144,8 @@ module ActiveRecord binds.map(&:value_for_database) end - def type_for_attribute_options(type_name, **options) - klass = type_classes_with_standard_constructor.fetch(type_name, Type::Value) - klass.new(**options) - end - private - def type_classes_with_standard_constructor - { - big_integer: Type::BigInteger, - binary: Type::Binary, - boolean: Type::Boolean, - date: Type::Date, - date_time: Type::DateTime, - decimal: Type::Decimal, - float: Type::Float, - integer: Type::Integer, - string: Type::String, - text: Type::Text, - time: Type::Time, - } - end - def types_which_need_no_typecasting [nil, Numeric, String] end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 0a9e599c3c..84bfab43bb 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -942,9 +942,8 @@ module ActiveRecord end end - def type_classes_with_standard_constructor - super.merge(string: MysqlString) - end + ActiveRecord::Type.register(:string, MysqlString, adapter: :mysql) + ActiveRecord::Type.register(:string, MysqlString, adapter: :mysql2) end end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb index b8d0e26f85..b7755c4593 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb @@ -67,23 +67,6 @@ module ActiveRecord type_map.lookup(column.oid, column.fmod, column.sql_type) end - def type_for_attribute_options( - type_name, - array: false, - range: false, - **options - ) - if array - subtype = type_for_attribute_options(type_name, **options) - OID::Array.new(subtype) - elsif range - subtype = type_for_attribute_options(type_name, **options) - OID::Range.new(subtype) - else - super(type_name, **options) - end - end - private def _quote(value) @@ -122,27 +105,6 @@ module ActiveRecord super end end - - def type_classes_with_standard_constructor - super.merge( - bit: OID::Bit, - bit_varying: OID::BitVarying, - binary: OID::Bytea, - cidr: OID::Cidr, - date_time: OID::DateTime, - decimal: OID::Decimal, - enum: OID::Enum, - hstore: OID::Hstore, - inet: OID::Inet, - json: OID::Json, - jsonb: OID::Jsonb, - money: OID::Money, - point: OID::Point, - uuid: OID::Uuid, - vector: OID::Vector, - xml: OID::Xml, - ) - end end end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 6d7e1075d7..6d25b53b21 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -813,6 +813,25 @@ module ActiveRecord return unless coder_class coder_class.new(oid: row['oid'], name: row['typname']) end + + ActiveRecord::Type.add_modifier({ array: true }, OID::Array, adapter: :postgresql) + ActiveRecord::Type.add_modifier({ range: true }, OID::Range, adapter: :postgresql) + ActiveRecord::Type.register(:bit, OID::Bit, adapter: :postgresql) + ActiveRecord::Type.register(:bit_varying, OID::BitVarying, adapter: :postgresql) + ActiveRecord::Type.register(:binary, OID::Bytea, adapter: :postgresql) + ActiveRecord::Type.register(:cidr, OID::Cidr, adapter: :postgresql) + ActiveRecord::Type.register(:date_time, OID::DateTime, adapter: :postgresql) + ActiveRecord::Type.register(:decimal, OID::Decimal, adapter: :postgresql) + ActiveRecord::Type.register(:enum, OID::Enum, adapter: :postgresql) + ActiveRecord::Type.register(:hstore, OID::Hstore, adapter: :postgresql) + ActiveRecord::Type.register(:inet, OID::Inet, adapter: :postgresql) + ActiveRecord::Type.register(:json, OID::Json, adapter: :postgresql) + ActiveRecord::Type.register(:jsonb, OID::Jsonb, adapter: :postgresql) + ActiveRecord::Type.register(:money, OID::Money, adapter: :postgresql) + ActiveRecord::Type.register(:point, OID::Point, adapter: :postgresql) + ActiveRecord::Type.register(:uuid, OID::Uuid, adapter: :postgresql) + ActiveRecord::Type.register(:vector, OID::Vector, adapter: :postgresql) + ActiveRecord::Type.register(:xml, OID::Xml, adapter: :postgresql) end end end diff --git a/activerecord/lib/active_record/type.rb b/activerecord/lib/active_record/type.rb index 7ce0370d8b..cddd56a20d 100644 --- a/activerecord/lib/active_record/type.rb +++ b/activerecord/lib/active_record/type.rb @@ -27,9 +27,7 @@ module ActiveRecord class << self attr_accessor :registry # :nodoc: - def register(*args) - registry.register(*args) - end + delegate :register, :add_modifier, to: :registry def lookup(*args, adapter: current_adapter_name, **kwargs) registry.lookup(*args, adapter: adapter, **kwargs) @@ -41,5 +39,17 @@ module ActiveRecord ActiveRecord::Base.connection.adapter_name.downcase.to_sym end end + + register(:big_integer, Type::BigInteger, override: false) + register(:binary, Type::Binary, override: false) + register(:boolean, Type::Boolean, override: false) + register(:date, Type::Date, override: false) + register(:date_time, Type::DateTime, override: false) + register(:decimal, Type::Decimal, override: false) + register(:float, Type::Float, override: false) + register(:integer, Type::Integer, override: false) + register(:string, Type::String, override: false) + register(:text, Type::Text, override: false) + register(:time, Type::Time, override: false) end end diff --git a/activerecord/lib/active_record/type/adapter_specific_registry.rb b/activerecord/lib/active_record/type/adapter_specific_registry.rb index ce9ff6a557..5f71b3cb94 100644 --- a/activerecord/lib/active_record/type/adapter_specific_registry.rb +++ b/activerecord/lib/active_record/type/adapter_specific_registry.rb @@ -1,6 +1,7 @@ module ActiveRecord + # :stopdoc: module Type - class AdapterSpecificRegistry # :nodoc: + class AdapterSpecificRegistry def initialize @registrations = [] end @@ -136,4 +137,6 @@ module ActiveRecord class TypeConflictError < StandardError end + + # :startdoc: end |