public
Description: ActiveRecord connection proxy for master/slave connections
Clone URL: git://github.com/technoweenie/masochism.git
changed observers to only query against the master database, was causing a 
race condition in observers with conditionals
Andy Delcambre (author)
Tue Mar 04 08:56:17 -0800 2008
commit  a6933cae9b2634461006c17f7a5412237ef3e8bb
tree    bda26c84f8cf9847f69a74c0621cb11e793738ad
parent  7d1d83526e9018e163cf643bc6e7d4d515d0e50c
...
20
21
22
 
 
 
 
23
24
25
...
60
61
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
64
...
20
21
22
23
24
25
26
27
28
29
...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
0
@@ -20,6 +20,10 @@ module ActiveReload
0
     def self.setup_for(master, slave = nil)
0
       slave ||= ActiveRecord::Base
0
       slave.send :include, ActiveRecordConnectionMethods
0
+ # extend observer to always use the master database
0
+ # observers only get triggered on writes, so shouldn't be a performance hit
0
+ # removes a race condition if you are using conditionals in the observer
0
+ ActiveRecord::Observer.send :include, ActiveReload::ObserverExtensions
0
       ActiveRecord::Base.active_connections[slave.name] = new(master, slave)
0
     end
0
 
0
@@ -60,4 +64,21 @@ module ActiveReload
0
       connection.with_master { reload_without_master }
0
     end
0
   end
0
+
0
+ module ObserverExtensions
0
+ def self.included(base)
0
+ base.alias_method_chain :update, :masterdb
0
+ end
0
+
0
+ # Send observed_method(object) if the method exists.
0
+ def update_with_masterdb(observed_method, object) #:nodoc:
0
+ if object.class.connection.respond_to?(:with_master)
0
+ object.class.connection.with_master do
0
+ update_without_masterdb(observed_method, object)
0
+ end
0
+ else
0
+ update_without_masterdb(observed_method, object)
0
+ end
0
+ end
0
+ end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.