Class: Polo::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/polo/collector.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_class, id, dependency_tree = {}) ⇒ Collector

Returns a new instance of Collector.



4
5
6
7
8
9
# File 'lib/polo/collector.rb', line 4

def initialize(base_class, id, dependency_tree={})
  @base_class = base_class
  @id = id
  @dependency_tree = dependency_tree
  @selects = []
end

Instance Method Details

#collectObject

Public: Traverses the dependency tree and collects every SQL query.

This is done by wrapping a top level call to includes(…) with a ActiveSupport::Notifications block and collecting every generate SQL query.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/polo/collector.rb', line 16

def collect
  unprepared_statement do
    ActiveSupport::Notifications.subscribed(collector, 'sql.active_record') do
      base_finder = @base_class.includes(@dependency_tree).where(@base_class.primary_key => @id)
      collect_sql(@base_class, base_finder.to_sql)
      base_finder.to_a
    end
  end

  @selects.compact.uniq
end