diff --git a/lib/bullet/detector/association.rb b/lib/bullet/detector/association.rb index 549ec039..ab1b2cfd 100644 --- a/lib/bullet/detector/association.rb +++ b/lib/bullet/detector/association.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +using Bullet::Ext::Object + module Bullet module Detector class Association < Base diff --git a/lib/bullet/detector/counter_cache.rb b/lib/bullet/detector/counter_cache.rb index 62744dd9..c9f27130 100644 --- a/lib/bullet/detector/counter_cache.rb +++ b/lib/bullet/detector/counter_cache.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +using Bullet::Ext::Object + module Bullet module Detector class CounterCache < Base diff --git a/lib/bullet/detector/n_plus_one_query.rb b/lib/bullet/detector/n_plus_one_query.rb index 8effc793..7a6600e1 100644 --- a/lib/bullet/detector/n_plus_one_query.rb +++ b/lib/bullet/detector/n_plus_one_query.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +using Bullet::Ext::Object + module Bullet module Detector class NPlusOneQuery < Association diff --git a/lib/bullet/detector/unused_eager_loading.rb b/lib/bullet/detector/unused_eager_loading.rb index d9bc2af7..c87c564c 100644 --- a/lib/bullet/detector/unused_eager_loading.rb +++ b/lib/bullet/detector/unused_eager_loading.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +using Bullet::Ext::Object +using Bullet::Ext::String + module Bullet module Detector class UnusedEagerLoading < Association diff --git a/lib/bullet/ext/object.rb b/lib/bullet/ext/object.rb index 64d1827a..68dfbc87 100644 --- a/lib/bullet/ext/object.rb +++ b/lib/bullet/ext/object.rb @@ -1,30 +1,36 @@ # frozen_string_literal: true -class Object - def bullet_key - "#{self.class}:#{bullet_primary_key_value}" - end +module Bullet + module Ext + module Object + refine ::Object do + def bullet_key + "#{self.class}:#{bullet_primary_key_value}" + end - def bullet_primary_key_value - return if respond_to?(:persisted?) && !persisted? + def bullet_primary_key_value + return if respond_to?(:persisted?) && !persisted? - if self.class.respond_to?(:primary_keys) && self.class.primary_keys - primary_key = self.class.primary_keys - elsif self.class.respond_to?(:primary_key) && self.class.primary_key - primary_key = self.class.primary_key - else - primary_key = :id - end + if self.class.respond_to?(:primary_keys) && self.class.primary_keys + primary_key = self.class.primary_keys + elsif self.class.respond_to?(:primary_key) && self.class.primary_key + primary_key = self.class.primary_key + else + primary_key = :id + end - bullet_join_potential_composite_primary_key(primary_key) - end + bullet_join_potential_composite_primary_key(primary_key) + end - private + private - def bullet_join_potential_composite_primary_key(primary_keys) - return send(primary_keys) unless primary_keys.is_a?(Enumerable) + def bullet_join_potential_composite_primary_key(primary_keys) + return send(primary_keys) unless primary_keys.is_a?(Enumerable) - primary_keys.map { |primary_key| send primary_key } - .join(',') + primary_keys.map { |primary_key| send primary_key } + .join(',') + end + end + end end end diff --git a/lib/bullet/ext/string.rb b/lib/bullet/ext/string.rb index 96f02c66..1b9e9413 100644 --- a/lib/bullet/ext/string.rb +++ b/lib/bullet/ext/string.rb @@ -1,7 +1,13 @@ # frozen_string_literal: true -class String - def bullet_class_name - sub(/:[^:]*?$/, '') +module Bullet + module Ext + module String + refine ::String do + def bullet_class_name + sub(/:[^:]*?$/, '') + end + end + end end end diff --git a/lib/bullet/registry/object.rb b/lib/bullet/registry/object.rb index 1eda349e..3fc232c0 100644 --- a/lib/bullet/registry/object.rb +++ b/lib/bullet/registry/object.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +using Bullet::Ext::Object +using Bullet::Ext::String + module Bullet module Registry class Object < Base diff --git a/lib/bullet/stack_trace_filter.rb b/lib/bullet/stack_trace_filter.rb index bea7fc94..f46167eb 100644 --- a/lib/bullet/stack_trace_filter.rb +++ b/lib/bullet/stack_trace_filter.rb @@ -2,6 +2,8 @@ require "bundler" +using Bullet::Ext::Object + module Bullet module StackTraceFilter VENDOR_PATH = '/vendor' diff --git a/spec/bullet/detector/association_spec.rb b/spec/bullet/detector/association_spec.rb index 67ba29d6..2a349fcd 100644 --- a/spec/bullet/detector/association_spec.rb +++ b/spec/bullet/detector/association_spec.rb @@ -2,6 +2,8 @@ require 'spec_helper' +using Bullet::Ext::Object + module Bullet module Detector describe Association do diff --git a/spec/bullet/detector/counter_cache_spec.rb b/spec/bullet/detector/counter_cache_spec.rb index 138dc074..53685900 100644 --- a/spec/bullet/detector/counter_cache_spec.rb +++ b/spec/bullet/detector/counter_cache_spec.rb @@ -2,6 +2,8 @@ require 'spec_helper' +using Bullet::Ext::Object + module Bullet module Detector describe CounterCache do diff --git a/spec/bullet/detector/n_plus_one_query_spec.rb b/spec/bullet/detector/n_plus_one_query_spec.rb index 1f25f180..7b981bf4 100644 --- a/spec/bullet/detector/n_plus_one_query_spec.rb +++ b/spec/bullet/detector/n_plus_one_query_spec.rb @@ -2,6 +2,8 @@ require 'spec_helper' +using Bullet::Ext::Object + module Bullet module Detector describe NPlusOneQuery do diff --git a/spec/bullet/detector/unused_eager_loading_spec.rb b/spec/bullet/detector/unused_eager_loading_spec.rb index 1582d5df..c6a0a3df 100644 --- a/spec/bullet/detector/unused_eager_loading_spec.rb +++ b/spec/bullet/detector/unused_eager_loading_spec.rb @@ -2,6 +2,8 @@ require 'spec_helper' +using Bullet::Ext::Object + module Bullet module Detector describe UnusedEagerLoading do diff --git a/spec/bullet/ext/object_spec.rb b/spec/bullet/ext/object_spec.rb index 433dee72..13846f81 100644 --- a/spec/bullet/ext/object_spec.rb +++ b/spec/bullet/ext/object_spec.rb @@ -2,6 +2,8 @@ require 'spec_helper' +using Bullet::Ext::Object + describe Object do context 'bullet_key' do it 'should return class and id composition' do diff --git a/spec/bullet/ext/string_spec.rb b/spec/bullet/ext/string_spec.rb index d8add1f9..25894ab9 100644 --- a/spec/bullet/ext/string_spec.rb +++ b/spec/bullet/ext/string_spec.rb @@ -2,6 +2,8 @@ require 'spec_helper' +using Bullet::Ext::String + describe String do context 'bullet_class_name' do it 'should only return class name' do diff --git a/spec/bullet/registry/object_spec.rb b/spec/bullet/registry/object_spec.rb index a97f5ff7..d74a59b5 100644 --- a/spec/bullet/registry/object_spec.rb +++ b/spec/bullet/registry/object_spec.rb @@ -2,6 +2,8 @@ require 'spec_helper' +using Bullet::Ext::Object + module Bullet module Registry describe Object do diff --git a/spec/support/bullet_ext.rb b/spec/support/bullet_ext.rb index 69d7c6a7..1a85293c 100644 --- a/spec/support/bullet_ext.rb +++ b/spec/support/bullet_ext.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +using Bullet::Ext::Object + module Bullet def self.collected_notifications_of_class(notification_class) Bullet.notification_collector.collection.select { |notification| notification.is_a? notification_class }