Class: Orthoses::ActiveRecord::Relation

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

Instance Method Summary collapse

Constructor Details

#initialize(loader) ⇒ Relation

Returns a new instance of Relation.



6
7
8
# File 'lib/orthoses/active_record/relation.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/orthoses/active_record/relation.rb', line 10

def call
  @loader.call.tap do |store|
    ::ActiveRecord::Base.descendants.each do |klass|
      next if klass.abstract_class?

      primary_key = fetch_primary_key(klass)

      model_name = Utils.module_name(klass) or next
      class_specific_relation = "#{model_name}::ActiveRecord_Relation"
      class_specific_proxy = "#{model_name}::ActiveRecord_Associations_CollectionProxy"
      class_specific_generated_relation_methods = "#{model_name}::GeneratedRelationMethods"

      # Expressing delegation.
      store[class_specific_generated_relation_methods].tap do |c|
        klass.singleton_methods(false).each do |singleton_method|
          c << "def #{singleton_method}: (*untyped, **untyped) -> untyped"
        end
        (klass.singleton_class.included_modules - ::ActiveRecord::Relation.included_modules).each do |mod|
          mname = Utils.module_name(mod) or next
          store[mname].header = "module #{mname}"
          c << "include #{mname}"
        end
      end

      store[class_specific_relation].tap do |c|
        c.header = "class #{class_specific_relation} < ::ActiveRecord::Relation"
        c << "include #{class_specific_generated_relation_methods}"
        c << "include _ActiveRecord_Relation[#{model_name}, #{primary_key}]"
        c << "include Enumerable[#{model_name}]"
      end

      store[class_specific_proxy].tap do |c|
        c.header = "class #{class_specific_proxy} < ::ActiveRecord::Associations::CollectionProxy"
        c << "include _ActiveRecord_Relation[#{model_name}, #{primary_key}]"
      end

      store[model_name].tap do |c|
        c << "extend _ActiveRecord_Relation_ClassMethods[#{model_name}, #{class_specific_relation}, #{primary_key}]"
      end
    end
  end
end