Class: SorbetRails::ModelPlugins::ActiveRecordAssoc

Inherits:
Base
  • Object
show all
Defined in:
lib/sorbet-rails/model_plugins/active_record_assoc.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

#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

#initialize(model_class, available_classes) ⇒ ActiveRecordAssoc

Returns a new instance of ActiveRecordAssoc.


5
6
7
8
# File 'lib/sorbet-rails/model_plugins/active_record_assoc.rb', line 5

def initialize(model_class, available_classes)
  super
  @columns_hash = @model_class.table_exists? ? @model_class.columns_hash : {}
end

Instance Method Details

#assoc_should_be_untyped?(reflection) ⇒ Boolean

Returns:

  • (Boolean)

171
172
173
174
175
176
177
178
179
180
181
# File 'lib/sorbet-rails/model_plugins/active_record_assoc.rb', line 171

def assoc_should_be_untyped?(reflection)
  # For some polymorphic associations (e.g. a has-many-through where the `source`
  # is polymorphic) we can figure out the type from the class_name or source_type.
  polymorpic_with_unknowable_klass = (
    polymorphic_assoc?(reflection) &&
    !reflection.options.key?(:class_name) &&
    !reflection.options.key?(:source_type)
  )

  polymorpic_with_unknowable_klass || !@available_classes.include?(reflection.klass.name)
end

#generate(root) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sorbet-rails/model_plugins/active_record_assoc.rb', line 11

def generate(root)
  return unless @model_class.reflections.length > 0

  assoc_module_name = self.model_module_name("GeneratedAssociationMethods")
  assoc_module_rbi = root.create_module(assoc_module_name)

  model_class_rbi = root.create_class(self.model_class_name)
  model_class_rbi.create_include(assoc_module_name)

  @model_class.reflections.sort.each do |assoc_name, reflection|
    reflection.collection? ?
      populate_collection_assoc_getter_setter(assoc_module_rbi, assoc_name, reflection) :
      populate_single_assoc_getter_setter(assoc_module_rbi, assoc_name, reflection)
  end
end

#polymorphic_assoc?(reflection) ⇒ Boolean

Returns:

  • (Boolean)

190
191
192
193
194
# File 'lib/sorbet-rails/model_plugins/active_record_assoc.rb', line 190

def polymorphic_assoc?(reflection)
  reflection.through_reflection ?
    polymorphic_assoc?(reflection.source_reflection) :
    reflection.polymorphic?
end

#populate_collection_assoc_getter_setter(assoc_module_rbi, assoc_name, reflection) ⇒ Object


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/sorbet-rails/model_plugins/active_record_assoc.rb', line 129

def populate_collection_assoc_getter_setter(assoc_module_rbi, assoc_name, reflection)
  # TODO allow people to specify the possible values of polymorphic associations
  assoc_class = assoc_should_be_untyped?(reflection) ? "T.untyped" : "::#{reflection.klass.name}"
  relation_class = relation_should_be_untyped?(reflection) ?
    "ActiveRecord::Associations::CollectionProxy" :
    "#{assoc_class}::ActiveRecord_Associations_CollectionProxy"

  assoc_module_rbi.create_method(
    assoc_name.to_s,
    return_type: relation_class,
  )
  unless assoc_should_be_untyped?(reflection)
    id_type = "T.untyped"

    if reflection.klass.table_exists?
      # For DB views, the PK column would not exist.
      id_column = reflection.klass.primary_key

      if id_column
        id_column_def = reflection.klass.columns_hash[id_column]

        # Normally the id_type is an Integer, but it could be a String if using
        # UUIDs.
        id_type = type_for_column_def(id_column_def).to_s if id_column_def
      end
    end

    assoc_module_rbi.create_method(
      "#{assoc_name.singularize}_ids",
      return_type: "T::Array[#{id_type}]",
    )
  end
  assoc_module_rbi.create_method(
    "#{assoc_name}=",
    parameters: [
      Parameter.new("value", type: "T::Enumerable[#{assoc_class}]")
    ],
    return_type: nil,
  )
end

#populate_single_assoc_getter_setter(assoc_module_rbi, assoc_name, reflection) ⇒ Object


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
71
72
73
74
# File 'lib/sorbet-rails/model_plugins/active_record_assoc.rb', line 34

def populate_single_assoc_getter_setter(assoc_module_rbi, assoc_name, reflection)
  # TODO allow people to specify the possible values of polymorphic associations
  assoc_class = assoc_should_be_untyped?(reflection) ? "T.untyped" : "::#{reflection.klass.name}"
  assoc_type = (belongs_to_and_required?(reflection) || has_one_and_required?(reflection)) ? assoc_class : "T.nilable(#{assoc_class})"

  params = [
    Parameter.new("*args", type: "T.untyped"),
    Parameter.new("&block", type: "T.nilable(T.proc.params(object: #{assoc_class}).void)")
  ]

  assoc_module_rbi.create_method(
    assoc_name.to_s,
    return_type: assoc_type,
  )
  assoc_module_rbi.create_method(
    "build_#{assoc_name}",
    parameters: params,
    return_type: assoc_class,
  )
  assoc_module_rbi.create_method(
    "create_#{assoc_name}",
    parameters: params,
    return_type: assoc_class,
  )
  assoc_module_rbi.create_method(
    "create_#{assoc_name}!",
    parameters: params,
    return_type: assoc_class,
  )
  assoc_module_rbi.create_method(
    "#{assoc_name}=",
    parameters: [
      Parameter.new("value", type: assoc_type)
    ],
    return_type: nil,
  )
  assoc_module_rbi.create_method(
    "reload_#{assoc_name}",
    return_type: assoc_type,
  )
end

#relation_should_be_untyped?(reflection) ⇒ Boolean

Returns:

  • (Boolean)

184
185
186
187
# File 'lib/sorbet-rails/model_plugins/active_record_assoc.rb', line 184

def relation_should_be_untyped?(reflection)
  # only type the relation we'll generate
  assoc_should_be_untyped?(reflection) || !@available_classes.include?(reflection.klass.name)
end