acts_as_readonlyable 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/lib/acts_as_readonlyable.rb +19 -12
- metadata +2 -2
data/README
CHANGED
@@ -40,7 +40,7 @@ dbs:
|
|
40
40
|
host: slave-host
|
41
41
|
|
42
42
|
|
43
|
-
Note:
|
43
|
+
Note: There is no need for more than one read-only database configuration in your database.yml since you can leverage traditional load balancing solutions for slaves. If you still want to use database.yml to spread the load, define multiple entries there and use acts_as_readonlyable [:first_read_only, :second_read_only]
|
44
44
|
|
45
45
|
=== Sample Model
|
46
46
|
|
data/lib/acts_as_readonlyable.rb
CHANGED
@@ -10,29 +10,31 @@ module ActiveRecord::Acts::ActsAsReadonlyable
|
|
10
10
|
|
11
11
|
module ClassMethods
|
12
12
|
|
13
|
-
def acts_as_readonlyable(
|
14
|
-
@
|
15
|
-
@
|
13
|
+
def acts_as_readonlyable(*readonly_dbs)
|
14
|
+
@readonly_dbs = readonly_dbs.flatten.collect(&:to_s)
|
15
|
+
@readonly_models = readonly_classes(@readonly_dbs)
|
16
16
|
self.extend(FinderClassOverrides)
|
17
17
|
self.send(:include, FinderObjectOverrides)
|
18
18
|
end
|
19
19
|
|
20
20
|
private
|
21
21
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
22
|
+
def readonly_classes(dbs)
|
23
|
+
dbs.collect do |db|
|
24
|
+
define_readonly_class(db) unless ActiveRecord.const_defined?(readonly_class_name(db))
|
25
|
+
ActiveRecord.const_get(readonly_class_name(db))
|
26
|
+
end
|
25
27
|
end
|
26
28
|
|
27
|
-
def readonly_class_name(
|
28
|
-
"Generated#{
|
29
|
+
def readonly_class_name(db)
|
30
|
+
"Generated#{ db.camelize }"
|
29
31
|
end
|
30
32
|
|
31
|
-
def define_readonly_class(
|
33
|
+
def define_readonly_class(db)
|
32
34
|
eval <<-CLASS_DEF
|
33
|
-
class ActiveRecord::#{ readonly_class_name(
|
35
|
+
class ActiveRecord::#{ readonly_class_name(db) } < ActiveRecord::Base
|
34
36
|
self.abstract_class = true
|
35
|
-
establish_connection configurations[RAILS_ENV]['#{
|
37
|
+
establish_connection configurations[RAILS_ENV]['#{ db }']
|
36
38
|
end
|
37
39
|
CLASS_DEF
|
38
40
|
end
|
@@ -92,13 +94,18 @@ module ActiveRecord::Acts::ActsAsReadonlyable
|
|
92
94
|
def run_on_readonly_db
|
93
95
|
klass_conn = connection
|
94
96
|
begin
|
95
|
-
self.connection =
|
97
|
+
self.connection = readonly_model.connection
|
96
98
|
self.clear_active_connection_name
|
97
99
|
yield
|
98
100
|
ensure
|
99
101
|
self.connection = klass_conn
|
100
102
|
self.clear_active_connection_name
|
101
103
|
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
def readonly_model
|
108
|
+
@readonly_models[rand(@readonly_models.size)]
|
102
109
|
end
|
103
110
|
|
104
111
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: acts_as_readonlyable
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2007-04-
|
6
|
+
version: 0.0.5
|
7
|
+
date: 2007-04-28 00:00:00 -04:00
|
8
8
|
summary: acts_as_readonlyable allows to add read-only slaves DBs to models
|
9
9
|
require_paths:
|
10
10
|
- lib
|