aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/types.rb
blob: 74f569352b0e7add21ed84d254d97e3e3c667bd4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module ActiveRecord
  module Types
    extend ActiveSupport::Concern

    module ClassMethods

      def attribute_types
        attribute_types = {}
        columns.each do |column|
          options = {}
          options[:time_zone_aware] = time_zone_aware?(column.name)
          options[:serialize]       = serialized_attributes[column.name]

          attribute_types[column.name] = to_type(column, options)
        end
        attribute_types
      end

      private

      def to_type(column, options = {})
        type_class = if options[:time_zone_aware]
          Type::TimeWithZone
        elsif options[:serialize]
          Type::Serialize
        elsif [ :integer, :float, :decimal ].include?(column.type)
          Type::Number
        else
          Type::Object
        end

        type_class.new(column, options)
      end

    end

  end
end