Class: ActiveRecord::TypeCaster::Connection

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/type_caster/connection.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(klass, table_name) ⇒ Connection

Returns a new instance of Connection.



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

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

Instance Method Details

#type_cast_for_database(attr_name, value) ⇒ Object



11
12
13
14
# 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) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# 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