Module: JitPreloader::ActiveRecordRelation

Defined in:
lib/jit_preloader/active_record/relation.rb

Instance Method Summary collapse

Instance Method Details

#calculate(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/jit_preloader/active_record/relation.rb', line 17

def calculate(*args)
  if respond_to?(:proxy_association) && proxy_association.owner && proxy_association.owner.jit_n_plus_one_tracking
    ActiveSupport::Notifications.publish("n_plus_one_query",
                                         source: proxy_association.owner,
                                         association: "#{proxy_association.reflection.name}.#{args.first}")
  end

  super(*args)
end

#exec_queriesObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/jit_preloader/active_record/relation.rb', line 27

def exec_queries
  super.tap do |records|
    if limit_value != 1
      records.each{ |record| record.jit_n_plus_one_tracking = true }
      if jit_preload? || JitPreloader.globally_enabled?
        JitPreloader::Preloader.attach(records)
      end
    end
  end
end

#jit_preload(*args) ⇒ Object



4
5
6
# File 'lib/jit_preloader/active_record/relation.rb', line 4

def jit_preload(*args)
  spawn.jit_preload!(*args)
end

#jit_preload!(*args) ⇒ Object



8
9
10
11
# File 'lib/jit_preloader/active_record/relation.rb', line 8

def jit_preload!(*args)
  @jit_preload = true
  self
end

#jit_preload?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/jit_preloader/active_record/relation.rb', line 13

def jit_preload?
  @jit_preload
end