Class: SorbetRails::ModelPlugins::ActiveRecordAttribute

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

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, #serialization_coder_for_column

Methods included from SorbetRails::ModelUtils

#add_relation_query_method, #exists_class_method?, #exists_instance_method?, #habtm_class?, #model_assoc_proxy_class_name, #model_assoc_relation_class_name, #model_class_name, #model_module_name, #model_query_methods_returning_assoc_relation_module_name, #model_query_methods_returning_relation_module_name, #model_relation_class_name, #model_relation_type_alias, #model_relation_type_class_name

Methods included from SorbetRails::ModelColumnUtils

#active_record_type_to_sorbet_type, #attribute_has_unconditional_presence_validation?, #model_class, #nilable_column?, #time_zone_aware_column?, #type_for_column_def

Constructor Details

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

Instance Method Details

#generate(root) ⇒ Object



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
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sorbet-rails/model_plugins/active_record_attribute.rb', line 6

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)

  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,
      )
    elsif serialization_coder_for_column(column_name)
      next # handled by the ActiveRecordSerializedAttribute plugin
    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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
# File 'lib/sorbet-rails/model_plugins/active_record_attribute.rb', line 61

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

  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

      root.create_class(model_class_name) do |model_class|
        # 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)
        model_class.create_enum_class(
          config.class_name,
          enums: t_enum_values.map { |k, v| [v, "%q{#{k}}"] },
        )
      end

      # define t_enum setter/getter
      assignable_type = "#{model_class_name}::#{config.class_name}"
      assignable_type = "T.nilable(#{assignable_type})" if nilable_column
      # 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 nilable_column
    return_type = "String"
    return_type = "T.nilable(#{return_type})" if nilable_column

    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

#value_type_for_attr_writer(column_type) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/sorbet-rails/model_plugins/active_record_attribute.rb', line 129

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