Class: Orthoses::ActiveRecord::HasMany

Inherits:
Object
  • Object
show all
Defined in:
lib/orthoses/active_record/has_many.rb

Instance Method Summary collapse

Constructor Details

#initialize(loader) ⇒ HasMany

Returns a new instance of HasMany.



6
7
8
# File 'lib/orthoses/active_record/has_many.rb', line 6

def initialize(loader)
  @loader = loader
end

Instance Method Details

#callObject



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
# File 'lib/orthoses/active_record/has_many.rb', line 10

def call
  @loader.call.tap do |store|
    ::ActiveRecord::Base.descendants.each do |base|
      next if base.abstract_class?
      base_name = Utils.module_name(base) || next

      lines = base.reflect_on_all_associations(:has_many).flat_map do |ref|
        singular_name = ref.name.to_s.singularize
        type = Orthoses::ActiveRecord.reflection_klass_name(ref) or next

        collection_proxy = "::#{type}::ActiveRecord_Associations_CollectionProxy"
        [
          "def #{ref.name}: () -> #{collection_proxy}",
          "def #{ref.name}=: (#{collection_proxy} | Array[#{type}]) -> (#{collection_proxy} | Array[#{type}])",
          "def #{singular_name}_ids: () -> Array[Integer]",
          "def #{singular_name}_ids=: (Array[Integer]) -> Array[Integer]",
        ]
      end

      generated_association_methods = "#{base_name}::GeneratedAssociationMethods"
      store[generated_association_methods].header = "module #{generated_association_methods}"
      store[generated_association_methods].concat(lines)

      sig = "include #{generated_association_methods}"
      store[base_name] << sig if !store[base_name].body.include?(sig)
    end
  end
end