Class: Tapioca::Dsl::Helpers::ActiveRecordColumnTypeHelper

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
RBIHelper
Defined in:
lib/tapioca/dsl/helpers/active_record_column_type_helper.rb

Constant Summary

Constants included from SorbetHelper

SorbetHelper::FEATURE_REQUIREMENTS, SorbetHelper::SORBET_BIN, SorbetHelper::SORBET_EXE_PATH_ENV_VAR, SorbetHelper::SORBET_GEM_SPEC, SorbetHelper::SORBET_PAYLOAD_URL

Instance Method Summary collapse

Methods included from RBIHelper

#as_nilable_type, #create_block_param, #create_kw_opt_param, #create_kw_param, #create_kw_rest_param, #create_opt_param, #create_param, #create_rest_param, #create_typed_param, #sanitize_signature_types, serialize_type_variable, #valid_method_name?, #valid_parameter_name?

Methods included from SorbetHelper

#sorbet, #sorbet_path, #sorbet_supports?

Constructor Details

#initialize(constant) ⇒ ActiveRecordColumnTypeHelper

Returns a new instance of ActiveRecordColumnTypeHelper.



12
13
14
# File 'lib/tapioca/dsl/helpers/active_record_column_type_helper.rb', line 12

def initialize(constant)
  @constant = constant
end

Instance Method Details

#type_for(column_name) ⇒ Object



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tapioca/dsl/helpers/active_record_column_type_helper.rb', line 17

def type_for(column_name)
  return ["T.untyped", "T.untyped"] if do_not_generate_strong_types?(@constant)

  column_type = @constant.attribute_types[column_name]

  getter_type =
    case column_type
    when defined?(MoneyColumn) && MoneyColumn::ActiveRecordType
      "::Money"
    when ActiveRecord::Type::Integer
      "::Integer"
    when ActiveRecord::Type::String
      "::String"
    when ActiveRecord::Type::Date
      "::Date"
    when ActiveRecord::Type::Decimal
      "::BigDecimal"
    when ActiveRecord::Type::Float
      "::Float"
    when ActiveRecord::Type::Boolean
      "T::Boolean"
    when ActiveRecord::Type::DateTime, ActiveRecord::Type::Time
      "::Time"
    when ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
      "::ActiveSupport::TimeWithZone"
    when ActiveRecord::Enum::EnumType
      "::String"
    when ActiveRecord::Type::Serialized
      serialized_column_type(column_type)
    else
      handle_unknown_type(column_type)
    end

  column = @constant.columns_hash[column_name]
  setter_type =
    case column_type
    when ActiveRecord::Enum::EnumType
      enum_setter_type(column_type)
    else
      getter_type
    end

  if column&.null
    return [as_nilable_type(getter_type), as_nilable_type(setter_type)]
  end

  if column_name == @constant.primary_key ||
      column_name == "created_at" ||
      column_name == "updated_at"
    getter_type = as_nilable_type(getter_type)
  end

  [getter_type, setter_type]
end