Class: CommandTower::ApplicationRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/command_tower/application_record.rb

Direct Known Subclasses

User

Class Method Summary collapse

Class Method Details

.attribute_to_type_mappingObject



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
39
40
41
42
43
# File 'app/models/command_tower/application_record.rb', line 7

def self.attribute_to_type_mapping
  @attribute_to_type_mapping ||= begin
    mapping = ActiveSupport::HashWithIndifferentAccess.new
    columns_hash.each do |attribute_name, |
      base = nil
      ruby_type = nil
      allowed_types = nil
      serialized_type = nil
      case .type
      when :string, :text
        base = ruby_type = String
      when :integer, :bigint
        base = ruby_type = Integer
      when :datetime, :time, :date
        base = String
        ruby_type = [DateTime, Time]
      when :float, :decimal
        base = ruby_type = Float
      when :boolean
        base = "Boolean"
        ruby_type = [TrueClass, FalseClass]
        allowed_types = [true, false]
      else
        # All else fails convert to String and continue
        base = ruby_type = String
      end

      attribute_type = attribute_types[attribute_name]
      if attribute_type.is_a?(ActiveRecord::Type::Serialized)
        serialized_type = attribute_type.coder.object_class
      end
      mapping[attribute_name] = { serialized_type:, base:, ruby_type:, allowed_types: }.compact
    end

    mapping
  end
end