1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #26282 from kamipo/add_type_default_value

Add `Type.default_value` and use it everywhere for internal
This commit is contained in:
Sean Griffin 2016-08-31 10:06:43 -04:00 committed by GitHub
commit c14c0e3cf0
9 changed files with 13 additions and 15 deletions

View file

@ -187,7 +187,7 @@ module ActiveRecord
class Null < Attribute # :nodoc: class Null < Attribute # :nodoc:
def initialize(name) def initialize(name)
super(name, nil, Type::Value.new) super(name, nil, Type.default_value)
end end
def type_cast(*) def type_cast(*)

View file

@ -437,7 +437,7 @@ module ActiveRecord
type_map.fetch(oid, fmod, sql_type) { type_map.fetch(oid, fmod, sql_type) {
warn "unknown OID #{oid}: failed to recognize type of '#{column_name}'. It will be treated as String." warn "unknown OID #{oid}: failed to recognize type of '#{column_name}'. It will be treated as String."
Type::Value.new.tap do |cast_type| Type.default_value.tap do |cast_type|
type_map.register_type(oid, cast_type) type_map.register_type(oid, cast_type)
end end
} }

View file

@ -268,7 +268,7 @@ module ActiveRecord
def attribute_types # :nodoc: def attribute_types # :nodoc:
load_schema load_schema
@attribute_types ||= Hash.new(Type::Value.new) @attribute_types ||= Hash.new(Type.default_value)
end end
def yaml_encoder # :nodoc: def yaml_encoder # :nodoc:

View file

@ -312,7 +312,7 @@ module ActiveRecord
key = group_columns.map { |aliaz, col_name| key = group_columns.map { |aliaz, col_name|
column = type_for(col_name) do column = type_for(col_name) do
calculated_data.column_types.fetch(aliaz) do calculated_data.column_types.fetch(aliaz) do
Type::Value.new Type.default_value
end end
end end
type_cast_calculated_value(row[aliaz], column) type_cast_calculated_value(row[aliaz], column)

View file

@ -80,14 +80,14 @@ module ActiveRecord
limit_bind = Attribute.with_cast_value( limit_bind = Attribute.with_cast_value(
"LIMIT".freeze, "LIMIT".freeze,
connection.sanitize_limit(limit_value), connection.sanitize_limit(limit_value),
Type::Value.new, Type.default_value,
) )
end end
if offset_value if offset_value
offset_bind = Attribute.with_cast_value( offset_bind = Attribute.with_cast_value(
"OFFSET".freeze, "OFFSET".freeze,
offset_value.to_i, offset_value.to_i,
Type::Value.new, Type.default_value,
) )
end end
connection.combine_bind_parameters( connection.combine_bind_parameters(

View file

@ -32,8 +32,6 @@ module ActiveRecord
class Result class Result
include Enumerable include Enumerable
IDENTITY_TYPE = Type::Value.new # :nodoc:
attr_reader :columns, :rows, :column_types attr_reader :columns, :rows, :column_types
def initialize(columns, rows, column_types = {}) def initialize(columns, rows, column_types = {})
@ -105,7 +103,7 @@ module ActiveRecord
def column_type(name, type_overrides = {}) def column_type(name, type_overrides = {})
type_overrides.fetch(name) do type_overrides.fetch(name) do
column_types.fetch(name, IDENTITY_TYPE) column_types.fetch(name, Type.default_value)
end end
end end

View file

@ -31,7 +31,7 @@ module ActiveRecord
if klass if klass
klass.type_for_attribute(column_name.to_s) klass.type_for_attribute(column_name.to_s)
else else
Type::Value.new Type.default_value
end end
end end

View file

@ -37,6 +37,10 @@ module ActiveRecord
registry.lookup(*args, adapter: adapter, **kwargs) registry.lookup(*args, adapter: adapter, **kwargs)
end end
def default_value # :nodoc:
@default_value ||= Value.new
end
private private
def current_adapter_name def current_adapter_name

View file

@ -11,7 +11,7 @@ module ActiveRecord
end end
def lookup(lookup_key, *args) def lookup(lookup_key, *args)
fetch(lookup_key, *args) { default_value } fetch(lookup_key, *args) { Type.default_value }
end end
def fetch(lookup_key, *args, &block) def fetch(lookup_key, *args, &block)
@ -55,10 +55,6 @@ module ActiveRecord
yield lookup_key, *args yield lookup_key, *args
end end
end end
def default_value
@default_value ||= ActiveModel::Type::Value.new
end
end end
end end
end end