123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::TypeCaster::Connection

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activerecord/lib/active_record/type_caster/connection.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(klass, table_name) ⇒ Connection

[ GitHub ]

  
# File 'activerecord/lib/active_record/type_caster/connection.rb', line 6

def initialize(klass, table_name)
  @klass = klass
  @table_name = table_name
end

Instance Attribute Details

#connection (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/type_caster/connection.rb', line 27

delegate :connection, to: :@klass, private: true

#table_name (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/type_caster/connection.rb', line 30

attr_reader :table_name

Instance Method Details

#type_cast_for_database(attr_name, value)

[ GitHub ]

  
# File 'activerecord/lib/active_record/type_caster/connection.rb', line 11

def type_cast_for_database(attr_name, value)
  type = type_for_attribute(attr_name)
  type.serialize(value)
end

#type_for_attribute(attr_name)

[ GitHub ]

  
# File 'activerecord/lib/active_record/type_caster/connection.rb', line 16

def type_for_attribute(attr_name)
  schema_cache = connection.schema_cache

  if schema_cache.data_source_exists?(table_name)
    column = schema_cache.columns_hash(table_name)[attr_name.to_s]
    type = connection.lookup_cast_type_from_column(column) if column
  end

  type || Type.default_value
end