Module: Bullet::ActiveRecord

Defined in:
lib/bullet/active_record70.rb,
lib/bullet/active_record71.rb,
lib/bullet/active_record72.rb,
lib/bullet/active_record80.rb,
lib/bullet/active_record81.rb

Class Method Summary collapse

Class Method Details

.enableObject



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/bullet/active_record70.rb', line 15

def self.enable
  require 'active_record'
  ::ActiveRecord::Base.extend(
    Module.new do
      def find_by_sql(sql, binds = [], preparable: nil, &block)
        result = super
        if Bullet.start?
          if result.is_a? Array
            if result.size > 1
              Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
              Bullet::Detector::CounterCache.add_possible_objects(result)
            elsif result.size == 1
              Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
              Bullet::Detector::CounterCache.add_impossible_object(result.first)
            end
          elsif result.is_a? ::ActiveRecord::Base
            Bullet::Detector::NPlusOneQuery.add_impossible_object(result)
            Bullet::Detector::CounterCache.add_impossible_object(result)
          end
        end
        result
      end
    end
  )

  ::ActiveRecord::Base.prepend(SaveWithBulletSupport)

  ::ActiveRecord::Relation.prepend(
    Module.new do
      # if select a collection of objects, then these objects have possible to cause N+1 query.
      # if select only one object, then the only one object has impossible to cause N+1 query.
      def records
        result = super
        if Bullet.start?
          if result.first.class.name !~ /^HABTM_/
            if result.size > 1
              Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
              Bullet::Detector::CounterCache.add_possible_objects(result)
            elsif result.size == 1
              Bullet::Detector::NPlusOneQuery.add_impossible_object(result.first)
              Bullet::Detector::CounterCache.add_impossible_object(result.first)
            end
          end
        end
        result
      end
    end
  )

  ::ActiveRecord::Associations::Preloader::Batch.prepend(
    Module.new do
      def call
        if Bullet.start?
          @preloaders.each do |preloader|
            preloader.records.each { |record|
              Bullet::Detector::Association.add_object_associations(record, preloader.associations)
            }
            Bullet::Detector::UnusedEagerLoading.add_eager_loadings(preloader.records, preloader.associations)
          end
        end
        super
      end
    end
  )

  ::ActiveRecord::Associations::Preloader::Branch.prepend(
    Module.new do
      def preloaders_for_reflection(reflection, reflection_records)
        if Bullet.start?
          reflection_records.compact!
          if reflection_records.first.class.name !~ /^HABTM_/
            reflection_records.each { |record|
              Bullet::Detector::Association.add_object_associations(record, reflection.name)
            }
            Bullet::Detector::UnusedEagerLoading.add_eager_loadings(reflection_records, reflection.name)
          end
        end
        super
      end
    end
  )

  ::ActiveRecord::Associations::Preloader::ThroughAssociation.prepend(
    Module.new do
      def source_preloaders
        if Bullet.start? && !defined?(@source_preloaders)
          preloaders = super
          preloaders.each do |preloader|
            reflection_name = preloader.send(:reflection).name
            preloader.send(:owners).each do |owner|
              Bullet::Detector::NPlusOneQuery.call_association(owner, reflection_name)
            end
          end
        else
          super
        end
      end

      def through_preloaders
        if Bullet.start? && !defined?(@through_preloaders)
          preloaders = super
          preloaders.each do |preloader|
            reflection_name = preloader.send(:reflection).name
            preloader.send(:owners).each do |owner|
              Bullet::Detector::NPlusOneQuery.call_association(owner, reflection_name)
            end
          end
        else
          super
        end
      end
    end
  )

  ::ActiveRecord::Associations::JoinDependency.prepend(
    Module.new do
      def instantiate(result_set, strict_loading_value, &block)
        @bullet_eager_loadings = {}
        records = super

        if Bullet.start?
          @bullet_eager_loadings.each do |_klazz, eager_loadings_hash|
            objects = eager_loadings_hash.keys
            Bullet::Detector::UnusedEagerLoading.add_eager_loadings(
              objects,
              eager_loadings_hash[objects.first].to_a
            )
          end
        end
        records
      end

      def construct(ar_parent, parent, row, seen, model_cache, strict_loading_value)
        if Bullet.start?
          unless ar_parent.nil?
            parent.children.each do |node|
              key = aliases.column_alias(node, node.primary_key)
              id = row[key]
              next unless id.nil?

              associations = [node.reflection.name]
              if node.reflection.through_reflection?
                associations << node.reflection.through_reflection.name
              end
              associations.each do |association|
                Bullet::Detector::Association.add_object_associations(ar_parent, association)
                Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association)
                @bullet_eager_loadings[ar_parent.class] ||= {}
                @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new
                @bullet_eager_loadings[ar_parent.class][ar_parent] << association
              end
            end
          end
        end

        super
      end

      # call join associations
      def construct_model(record, node, row, model_cache, id, strict_loading_value)
        result = super

        if Bullet.start?
          associations = [node.reflection.name]
          if node.reflection.through_reflection?
            associations << node.reflection.through_reflection.name
          end
          associations.each do |association|
            Bullet::Detector::Association.add_object_associations(record, association)
            Bullet::Detector::NPlusOneQuery.call_association(record, association)
            @bullet_eager_loadings[record.class] ||= {}
            @bullet_eager_loadings[record.class][record] ||= Set.new
            @bullet_eager_loadings[record.class][record] << association
          end
        end

        result
      end
    end
  )

  ::ActiveRecord::Associations::Association.prepend(
    Module.new do
      def inversed_from(record)
        if Bullet.start?
          Bullet::Detector::NPlusOneQuery.add_inversed_object(owner, reflection.name)
        end
        super
      end

      def inversed_from_queries(record)
        if Bullet.start? && inversable?(record)
          Bullet::Detector::NPlusOneQuery.add_inversed_object(owner, reflection.name)
        end
        super
      end
    end
  )

  ::ActiveRecord::Associations::CollectionAssociation.prepend(
    Module.new do
      def load_target
        records = super

        if Bullet.start?
          if is_a? ::ActiveRecord::Associations::ThroughAssociation
            association = owner.association(reflection.through_reflection.name)
            if association.loaded?
              Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
              Array.wrap(association.target).each do |through_record|
                Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
              end

              if reflection.through_reflection != through_reflection
                Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
              end
            end
          end
          Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
          if records.first.class.name !~ /^HABTM_/
            if records.size > 1
              Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
              Bullet::Detector::CounterCache.add_possible_objects(records)
            elsif records.size == 1
              Bullet::Detector::NPlusOneQuery.add_impossible_object(records.first)
              Bullet::Detector::CounterCache.add_impossible_object(records.first)
            end
          end
        end
        records
      end

      def empty?
        if Bullet.start? && !reflection.has_cached_counter?
          Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations)
        end
        super
      end

      def include?(object)
        Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations) if Bullet.start?
        super
      end
    end
  )

  ::ActiveRecord::Associations::SingularAssociation.prepend(
    Module.new do
      # call has_one and belongs_to associations
      def reader
        result = super

        if Bullet.start?
          if owner.class.name !~ /^HABTM_/
            if is_a? ::ActiveRecord::Associations::ThroughAssociation
              Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
              association = owner.association(reflection.through_reflection.name)
              Array.wrap(association.target).each do |through_record|
                Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
              end

              if reflection.through_reflection != through_reflection
                Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
              end
            end
            Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)

            if Bullet::Detector::NPlusOneQuery.impossible?(owner)
              Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
            else
              Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
            end
          end
        end
        result
      end
    end
  )

  ::ActiveRecord::Associations::HasManyAssociation.prepend(
    Module.new do
      def empty?
        if Bullet.start? && !reflection.has_cached_counter?
          Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, caller_locations)
        end
        super
      end

      def count_records
        result = reflection.has_cached_counter?
        if Bullet.start? && !result && !is_a?(::ActiveRecord::Associations::ThroughAssociation)
          Bullet::Detector::CounterCache.add_counter_cache(owner, reflection.name)
        end
        super
      end
    end
  )

  ::ActiveRecord::Associations::CollectionProxy.prepend(
    Module.new do
      def count(column_name = nil)
        if Bullet.start? && !proxy_association.is_a?(::ActiveRecord::Associations::ThroughAssociation)
          Bullet::Detector::CounterCache.add_counter_cache(
            proxy_association.owner,
            proxy_association.reflection.name
          )
          Bullet::Detector::NPlusOneQuery.call_association(
            proxy_association.owner,
            proxy_association.reflection.name,
            caller_locations
          )
        end
        super(column_name)
      end
    end
  )
end