Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make core extensions cleaner with refinements #709

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions lib/bullet/detector/unused_eager_loading.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

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

module Bullet
module Detector
class UnusedEagerLoading < Association
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
12 changes: 9 additions & 3 deletions lib/bullet/ext/string.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions lib/bullet/registry/object.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

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

module Bullet
module Registry
class Object < Base
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/ext/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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