Skip to content

Commit

Permalink
Avoid globally polluting ::Object class
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda committed Aug 11, 2024
1 parent 5ec21fc commit 763ad8a
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 20 deletions.
2 changes: 2 additions & 0 deletions lib/bullet/detector/association.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

using Bullet::Ext::Object

module Bullet
module Detector
class Association < Base
Expand Down
2 changes: 2 additions & 0 deletions lib/bullet/detector/counter_cache.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

using Bullet::Ext::Object

module Bullet
module Detector
class CounterCache < Base
Expand Down
2 changes: 2 additions & 0 deletions lib/bullet/detector/n_plus_one_query.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

using Bullet::Ext::Object

module Bullet
module Detector
class NPlusOneQuery < Association
Expand Down
1 change: 1 addition & 0 deletions lib/bullet/detector/unused_eager_loading.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

using Bullet::Ext::Object
using Bullet::Ext::String

module Bullet
Expand Down
46 changes: 26 additions & 20 deletions lib/bullet/ext/object.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/bullet/registry/object.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

using Bullet::Ext::Object
using Bullet::Ext::String

module Bullet
Expand Down
2 changes: 2 additions & 0 deletions lib/bullet/stack_trace_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require "bundler"

using Bullet::Ext::Object

module Bullet
module StackTraceFilter
VENDOR_PATH = '/vendor'
Expand Down
2 changes: 2 additions & 0 deletions spec/bullet/detector/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'spec_helper'

using Bullet::Ext::Object

module Bullet
module Detector
describe Association do
Expand Down
2 changes: 2 additions & 0 deletions spec/bullet/detector/counter_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'spec_helper'

using Bullet::Ext::Object

module Bullet
module Detector
describe CounterCache do
Expand Down
2 changes: 2 additions & 0 deletions spec/bullet/detector/n_plus_one_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'spec_helper'

using Bullet::Ext::Object

module Bullet
module Detector
describe NPlusOneQuery do
Expand Down
2 changes: 2 additions & 0 deletions spec/bullet/detector/unused_eager_loading_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'spec_helper'

using Bullet::Ext::Object

module Bullet
module Detector
describe UnusedEagerLoading do
Expand Down
2 changes: 2 additions & 0 deletions spec/bullet/ext/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions spec/bullet/registry/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'spec_helper'

using Bullet::Ext::Object

module Bullet
module Registry
describe Object do
Expand Down
2 changes: 2 additions & 0 deletions spec/support/bullet_ext.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down

0 comments on commit 763ad8a

Please sign in to comment.