Sha256: 8951ae1e9499646cc31b7e58e9d14ac0d37a98f411aae37bc993106f172e15b0

Contents?: true

Size: 823 Bytes

Versions: 23

Compression:

Stored size: 823 Bytes

Contents

# frozen_string_literal: true

module ActiveRecord
  module TypeCaster
    class Connection # :nodoc:
      def initialize(klass, table_name)
        @klass = klass
        @table_name = table_name
      end

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

      def type_for_attribute(attr_name)
        schema_cache = @klass.schema_cache

        if schema_cache.data_source_exists?(table_name)
          column = schema_cache.columns_hash(table_name)[attr_name.to_s]
          if column
            type = @klass.with_connection { |connection| connection.lookup_cast_type_from_column(column) }
          end
        end

        type || Type.default_value
      end

      private
        attr_reader :table_name
    end
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
activerecord-8.0.1 lib/active_record/type_caster/connection.rb
activerecord-8.0.0.1 lib/active_record/type_caster/connection.rb
activerecord-7.2.2.1 lib/active_record/type_caster/connection.rb
activerecord-8.0.0 lib/active_record/type_caster/connection.rb
activerecord-7.2.2 lib/active_record/type_caster/connection.rb
activerecord-8.0.0.rc2 lib/active_record/type_caster/connection.rb
activerecord-7.2.1.2 lib/active_record/type_caster/connection.rb
activerecord-8.0.0.rc1 lib/active_record/type_caster/connection.rb
activerecord-7.2.1.1 lib/active_record/type_caster/connection.rb
activerecord-8.0.0.beta1 lib/active_record/type_caster/connection.rb
omg-activerecord-8.0.0.alpha9 lib/active_record/type_caster/connection.rb
omg-activerecord-8.0.0.alpha8 lib/active_record/type_caster/connection.rb
omg-activerecord-8.0.0.alpha7 lib/active_record/type_caster/connection.rb
omg-activerecord-8.0.0.alpha4 lib/active_record/type_caster/connection.rb
omg-activerecord-8.0.0.alpha3 lib/active_record/type_caster/connection.rb
omg-activerecord-8.0.0.alpha2 lib/active_record/type_caster/connection.rb
omg-activerecord-8.0.0.alpha1 lib/active_record/type_caster/connection.rb
activerecord-7.2.1 lib/active_record/type_caster/connection.rb
activerecord-7.2.0 lib/active_record/type_caster/connection.rb
activerecord-7.2.0.rc1 lib/active_record/type_caster/connection.rb