Class: SorbetRails::ModelPlugins::ActiveRecordAttribute

Inherits:
Base
  • Object
show all
Defined in:
lib/sorbet-rails/model_plugins/active_record_attribute.rb

Defined Under Namespace

Classes: ColumnType

Constant Summary

Constants inherited from Base

Base::Parameter

Instance Attribute Summary

Attributes inherited from Base

#available_classes, #model_class

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from SorbetRails::ModelUtils

#add_relation_query_method, #exists_class_method?, #exists_instance_method?, #model_assoc_proxy_class_name, #model_assoc_relation_class_name, #model_class, #model_class_name, #model_module_name, #model_relation_class_name, #model_relation_type_alias, #model_relation_type_class_name

Constructor Details

This class inherits a constructor from SorbetRails::ModelPlugins::Base

Instance Method Details

#active_record_type_to_sorbet_type(klass, time_zone_aware: false) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/sorbet-rails/model_plugins/active_record_attribute.rb', line 167

def active_record_type_to_sorbet_type(klass, time_zone_aware: false)
  case klass
  when ActiveRecord::Type::Boolean
    "T::Boolean"
  when ActiveRecord::Type::DateTime, ActiveRecord::Type::Time
    time_zone_aware ? ActiveSupport::TimeWithZone : Time
  when ActiveRecord::Type::Date
    Date
  when ActiveRecord::Type::Decimal
    BigDecimal
  when ActiveRecord::Type::Float
    Float
  when ActiveRecord::Type::BigInteger, ActiveRecord::Type::Integer, ActiveRecord::Type::DecimalWithoutScale, ActiveRecord::Type::UnsignedInteger
    Integer
  when ActiveRecord::Type::Binary, ActiveRecord::Type::String, ActiveRecord::Type::Text
    String
  else
    # Json type is only supported in Rails 5.2 and above
    case
    when Object.const_defined?('ActiveRecord::Type::Json') && klass.is_a?(ActiveRecord::Type::Json)
      "T.any(T::Array[T.untyped], T::Boolean, Float, T::Hash[T.untyped, T.untyped], Integer, String)"
    when Object.const_defined?('ActiveRecord::Enum::EnumType') && klass.is_a?(ActiveRecord::Enum::EnumType)
      String
    else
      "T.untyped"
    end
  end
end

#generate(root) ⇒ Object



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
# File 'lib/sorbet-rails/model_plugins/active_record_attribute.rb', line 22

def generate(root)
  columns_hash = @model_class.table_exists? ? @model_class.columns_hash : {}
  return unless columns_hash.size > 0

  attribute_module_name = self.model_module_name("GeneratedAttributeMethods")
  attribute_module_rbi = root.create_module(attribute_module_name)
  attribute_module_rbi.create_extend("T::Sig")

  model_class_rbi = root.create_class(self.model_class_name)
  model_class_rbi.create_include(attribute_module_name)
  model_defined_enums = @model_class.defined_enums

  columns_hash.sort.each do |column_name, column_def|
    if model_defined_enums.has_key?(column_name)
      generate_enum_methods(
        root,
        model_class_rbi,
        attribute_module_rbi,
        model_defined_enums,
        column_name,
        column_def,
      )
    else
      column_type = type_for_column_def(column_def)
      attribute_module_rbi.create_method(
        column_name.to_s,
        return_type: column_type.to_s,
      )
      attribute_module_rbi.create_method(
        "#{column_name}=",
        parameters: [
          Parameter.new("value", type: value_type_for_attr_writer(column_type))
        ],
        return_type: nil,
      )
    end

    attribute_module_rbi.create_method(
      "#{column_name}?",
      return_type: "T::Boolean",
    )
  end
end

#generate_enum_methods(root, model_class_rbi, attribute_module_rbi, model_defined_enums, column_name, column_def) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sorbet-rails/model_plugins/active_record_attribute.rb', line 76

def generate_enum_methods(
  root,
  model_class_rbi,
  attribute_module_rbi,
  model_defined_enums,
  column_name,
  column_def
)
  should_skip_setter_getter = false

  if @model_class.is_a?(::ActiveRecord::Enum)
    config = @model_class.typed_enum_reflections[column_name]
    if config.present?
      # do not generate the methods for enums in strict_mode
      should_skip_setter_getter = config.strict_mode

      t_enum_type = "#{@model_class.name}::#{config.class_name}"

      # define T::Enum class & values
      enum_values = T.must(model_defined_enums[column_name])
      t_enum_values = @model_class.gen_typed_enum_values(enum_values.keys)
      root.create_enum_class(
        t_enum_type,
        enums: t_enum_values.map { |k, v| [v, "'#{k}'"] },
      )

      # define t_enum setter/getter
      assignable_type = t_enum_type
      assignable_type = "T.nilable(#{assignable_type})" if column_def.null
      # add directly to model_class_rbi because they are included
      # by sorbet's hidden.rbi
      model_class_rbi.create_method(
        "typed_#{column_name}",
        return_type: assignable_type,
      )
      model_class_rbi.create_method(
        "typed_#{column_name}=",
        parameters: [
          Parameter.new("value", type: assignable_type)
        ],
        return_type: nil,
      )
    end
  end

  if !should_skip_setter_getter
    # enum attribute is treated differently
    assignable_type = "T.any(Integer, String, Symbol)"
    assignable_type = "T.nilable(#{assignable_type})" if column_def.null
    return_type = "String"
    return_type = "T.nilable(#{return_type})" if column_def.null

    attribute_module_rbi.create_method(
      column_name.to_s,
      return_type: return_type,
    )
    attribute_module_rbi.create_method(
      "#{column_name}=",
      parameters: [
        Parameter.new("value", type: assignable_type)
      ],
      return_type: nil,
    )
  end
end

#time_zone_aware_column?(column_def, cast_type) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/sorbet-rails/model_plugins/active_record_attribute.rb', line 202

def time_zone_aware_column?(column_def, cast_type)
  # this private class method returns true if the attribute should be "time
  # zone aware"; it takes into account various global and model-specific
  # configuration options as described here:
  # https://api.rubyonrails.org/classes/ActiveRecord/Timestamp.html
  #
  # although it's private, it's better this than trying to reimplement the "is
  # this attribute tz aware?" logic ourselves
  @model_class.send(
    :create_time_zone_conversion_attribute?,
    column_def.name,
    cast_type
  )
end

#type_for_column_def(column_def) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/sorbet-rails/model_plugins/active_record_attribute.rb', line 143

def type_for_column_def(column_def)
  cast_type = ActiveRecord::Base.connection.respond_to?(:lookup_cast_type_from_column) ?
    ActiveRecord::Base.connection.lookup_cast_type_from_column(column_def) :
    column_def.cast_type

  strict_type =
    active_record_type_to_sorbet_type(
      cast_type,
      time_zone_aware: time_zone_aware_column?(column_def, cast_type),
    )

  ColumnType.new(
    base_type: strict_type,
    nilable: column_def.null,
    array_type: column_def.try(:array?),
  )
end

#value_type_for_attr_writer(column_type) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/sorbet-rails/model_plugins/active_record_attribute.rb', line 218

def value_type_for_attr_writer(column_type)
  # it's safe - and convenient - to assign any "time like" object to a time zone
  # aware attribute because Rails will cast it to a `ActiveSupport::TimeWithZone`
  # (so rereading the attribute will always return the `TimeWithZone` type)
  assignable_time_supertypes = [Date, Time, ActiveSupport::TimeWithZone].map(&:to_s)

  type = column_type.base_type
  if type.is_a?(Class)
    if type == ActiveSupport::TimeWithZone
      type = "T.any(#{assignable_time_supertypes.join(', ')})"
    elsif type < Numeric || type == ActiveSupport::Duration
      type = "T.any(Numeric, ActiveSupport::Duration)"
    elsif type == String
      type = "T.any(String, Symbol)"
    end
  end
  ColumnType.new(base_type: type, nilable: column_type.nilable, array_type: column_type.array_type).to_s
end