Skip to content

Commit

Permalink
add masochistic? predicate method for ActiveRecord::Base.connection
Browse files Browse the repository at this point in the history
load masochism classes during application initialization
  • Loading branch information
mislav committed Apr 13, 2009
1 parent 7fb1261 commit a53ecbc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if defined? ActiveRecord
require 'active_reload/connection_proxy'
end
12 changes: 11 additions & 1 deletion lib/active_reload/connection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def respond_to?(method)
def methods
super | current.methods
end

def masochistic?
true
end
end

module ActiveRecordConnectionMethods
Expand Down Expand Up @@ -160,7 +164,7 @@ def self.included(base)

# Send observed_method(object) if the method exists.
def update_with_master(observed_method, object)
if object.respond_to?(:connection) && object.connection.respond_to?(:with_master)
if object.respond_to?(:connection) && object.connection.masochistic?
object.connection.with_master do
update_without_master(observed_method, object)
end
Expand All @@ -170,3 +174,9 @@ def update_with_master(observed_method, object)
end
end
end

ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
def masochistic?
false
end
end
4 changes: 4 additions & 0 deletions test/connection_proxy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ def test_connection_is_a_proxy
assert_equal 'ActiveReload::ConnectionProxy', connection.class.name
end

def test_masochistic
assert connection.masochistic?
end

def test_reads_go_to_slave
@slave.expects(:select_rows).with('SELECT').returns(['bar'])
assert_equal ['bar'], connection.select_rows('SELECT')
Expand Down

0 comments on commit a53ecbc

Please sign in to comment.