Skip to content

Commit de6bf3c

Browse files
committed
Stop overriding #class
ARHP has been overriding `#class` so that `connection.class.class_eval` can work. That makes it _very_ hard to debug ARHP issues because you're never quite sure what object you're working with. I've changed it so that we now raise an exception if you try to call `.class_eval` on the `ConnectionProxy` class but implements a `._class_eval` method that will allow the typical `class_eval` behavior if someone _really_ needs to.
1 parent e901b25 commit de6bf3c

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/active_record_host_pool/connection_proxy.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
# for each call to the connection. upon executing a statement, the connection will switch to that database.
77
module ActiveRecordHostPool
88
class ConnectionProxy < Delegator
9+
class << self
10+
def class_eval
11+
raise "You probably want to call .class_eval on the ActiveRecord connection adapter and not on ActiveRecordHostPool's connection proxy. Use ._class_eval if you _really_ know what you're doing."
12+
end
13+
14+
def _class_eval(...)
15+
method(:class_eval).super_method.call(...)
16+
end
17+
end
18+
919
attr_reader :database
1020
def initialize(cx, database)
1121
super(cx)
@@ -26,11 +36,6 @@ def unproxied
2636
@cx
2737
end
2838

29-
# this is bad. I know. but it allows folks who class_eval on connection.class to do so
30-
def class
31-
@cx.class
32-
end
33-
3439
def expects(*args)
3540
@cx.send(:expects, *args)
3641
end

test/test_arhp.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ def test_connection_returns_a_proxy
116116
end
117117

118118
def test_connection_proxy_handles_private_methods
119-
# Relies on connection.class returning the real class
120-
Pool1DbA.connection.class.class_eval do
119+
Pool1DbA.connection.unproxied.class.class_eval do
121120
private
122121

123122
def test_private_method

0 commit comments

Comments
 (0)