acts_as_readonlyable 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +75 -0
- data/init.rb +1 -0
- data/lib/acts_as_readonlyable.rb +114 -0
- metadata +50 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 Revolution Health Group LLC. All rights reserved.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
== Introduction
|
2
|
+
|
3
|
+
ActsAsReadonlyable adds support of multiple read-only slave databases to ActiveRecord models. When a model is marked with acts_as_readonlyable, some of AR finders are overridden to run against a slave DB. The supported finders are find, find_by_sql, count_by_sql, find_[all_]by_*, and reload.
|
4
|
+
|
5
|
+
Finders can be forced to fall back to a default DB by passing the :readonly flag set to 'false'.
|
6
|
+
|
7
|
+
== Disclaimer
|
8
|
+
|
9
|
+
As our blog entry http://revolutiononrails.blogspot.com/2007/04/ActsAsReadonlyable-to-support-read-only.html points out, we wrote this plugin in preparation to using slave DBs but we are not going to have those until May 2007. So even though the code is covered with tests (see svn://rubyforge.org/var/svn/acts-as-with-ro/trunk/test/unit/read_write_model_test.rb), it has not been used outside of those. We would have a discovery period in May when the code is likely to be improved so for now you can use is at your own risk. Meanwhile, we would be happy to fix any issue revealed. Drop us a line at rails-trunk [ at ] revolution DOT com.
|
10
|
+
|
11
|
+
Using this plugin should not be your first step in application optimization/scaling or even the second one. Before installing it make sure you understand the implication of leveraging multiple DBs (for example, the potential for cross DB joins).
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
Add 'acts_as_readonlyable db_config_entry' to your models backed up by slave DBs. If you want to apply ActsAsReadonlyable to all models, add this or similar code at the end of config/environment.rb:
|
16
|
+
|
17
|
+
class << ActiveRecord::Base
|
18
|
+
|
19
|
+
def read_only_inherited(child)
|
20
|
+
child.acts_as_readonlyable :read_only
|
21
|
+
ar_inherited(child)
|
22
|
+
end
|
23
|
+
|
24
|
+
alias_method :ar_inherited, :inherited
|
25
|
+
alias_method :inherited, :read_only_inherited
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
== Example
|
30
|
+
|
31
|
+
=== Sample DB Config
|
32
|
+
|
33
|
+
dbs:
|
34
|
+
|
35
|
+
database: master_db
|
36
|
+
host: master-host
|
37
|
+
|
38
|
+
read_only:
|
39
|
+
database: slave_db
|
40
|
+
host: slave-host
|
41
|
+
|
42
|
+
|
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.
|
44
|
+
|
45
|
+
=== Sample Model
|
46
|
+
|
47
|
+
class Fruit < ActiveRecord::Base
|
48
|
+
acts_as_readonlyable :read_only
|
49
|
+
end
|
50
|
+
|
51
|
+
=== Usage
|
52
|
+
|
53
|
+
r = Fruit.find(:first) # executes against the read_only db
|
54
|
+
r.field = 'value'
|
55
|
+
r.save! # executes against the read_write db
|
56
|
+
|
57
|
+
r.reload # executes against the read_only db
|
58
|
+
r.reload(:readonly => false) # executes against the read_write db
|
59
|
+
|
60
|
+
|
61
|
+
== Installation
|
62
|
+
|
63
|
+
As plugin:
|
64
|
+
script/plugin install svn://rubyforge.org/var/svn/acts-as-with-ro/trunk/vendor/plugins/acts_as_readonlyable
|
65
|
+
|
66
|
+
|
67
|
+
== License
|
68
|
+
|
69
|
+
ActsAsReadonlyable released under the MIT license.
|
70
|
+
|
71
|
+
|
72
|
+
== Support
|
73
|
+
|
74
|
+
The plugin RubyForge page is http://rubyforge.org/projects/acts-as-with-ro
|
75
|
+
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'acts_as_readonlyable'
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# Copyright (c) 2007 Revolution Health Group LLC. All rights reserved.
|
2
|
+
|
3
|
+
module ActiveRecord; module Acts; end; end
|
4
|
+
|
5
|
+
module ActiveRecord::Acts::ActsAsReadonlyable
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
|
13
|
+
def acts_as_readonlyable(readonly_db)
|
14
|
+
@readonly_db = readonly_db.to_s
|
15
|
+
@readonly_klass = def_class(@readonly_db)
|
16
|
+
self.extend(FinderClassOverrides)
|
17
|
+
self.send(:include, FinderObjectOverrides)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def def_class(readonly_db)
|
23
|
+
eval <<-CLASS_DEF
|
24
|
+
class ::Generated#{ readonly_db.camelize } < ActiveRecord::Base
|
25
|
+
self.abstract_class = true
|
26
|
+
establish_connection configurations[RAILS_ENV]['#{ readonly_db }']
|
27
|
+
self
|
28
|
+
end
|
29
|
+
CLASS_DEF
|
30
|
+
end
|
31
|
+
|
32
|
+
module FinderClassOverrides
|
33
|
+
|
34
|
+
def find_every(options)
|
35
|
+
run_on_db(options) { super }
|
36
|
+
end
|
37
|
+
|
38
|
+
def find_by_sql(sql, options = nil)
|
39
|
+
|
40
|
+
# Called through construct_finder_sql
|
41
|
+
if sql.is_a?(Hash)
|
42
|
+
options = sql
|
43
|
+
sql = sql[:sql]
|
44
|
+
end
|
45
|
+
|
46
|
+
run_on_db(options) { super(sql) }
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
def count_by_sql(sql, options = nil)
|
51
|
+
run_on_db(options) { super(sql) }
|
52
|
+
end
|
53
|
+
|
54
|
+
def construct_finder_sql(options)
|
55
|
+
options.merge(:sql => super)
|
56
|
+
end
|
57
|
+
|
58
|
+
def set_readonly_option!(options) #:nodoc:
|
59
|
+
# Inherit :readonly from finder scope if set. Otherwise,
|
60
|
+
# if :joins is not blank then :readonly defaults to true.
|
61
|
+
unless options.has_key?(:readonly)
|
62
|
+
if scoped?(:find, :readonly)
|
63
|
+
options[:readonly] = true if scope(:find, :readonly)
|
64
|
+
elsif !options[:joins].blank? && !options[:select]
|
65
|
+
options[:readonly] = true
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def run_on_db(options)
|
73
|
+
if with_readonly?(options)
|
74
|
+
run_on_readonly_db { yield }
|
75
|
+
else
|
76
|
+
yield
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def with_readonly?(options)
|
81
|
+
(! options.is_a?(Hash)) || (! options.key?(:readonly)) || options[:readonly]
|
82
|
+
end
|
83
|
+
|
84
|
+
def run_on_readonly_db
|
85
|
+
klass_conn = connection
|
86
|
+
begin
|
87
|
+
self.connection = @readonly_klass.connection
|
88
|
+
self.clear_active_connection_name
|
89
|
+
yield
|
90
|
+
ensure
|
91
|
+
self.connection = klass_conn
|
92
|
+
self.clear_active_connection_name
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
module FinderObjectOverrides
|
99
|
+
|
100
|
+
# backport from 1.2.3 for 1.1.6
|
101
|
+
def reload(options = nil)
|
102
|
+
clear_aggregation_cache
|
103
|
+
clear_association_cache
|
104
|
+
@attributes.update(self.class.find(self.id, options).instance_variable_get('@attributes'))
|
105
|
+
self
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
ActiveRecord::Base.send(:include, ActiveRecord::Acts::ActsAsReadonlyable)
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.2
|
3
|
+
specification_version: 1
|
4
|
+
name: acts_as_readonlyable
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.3
|
7
|
+
date: 2007-04-21 00:00:00 -04:00
|
8
|
+
summary: acts_as_readonlyable allows to add read-only slaves DBs to models
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: rails-trunk@revolution.com
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: acts_as_readonlyable
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- RHG Team
|
31
|
+
files:
|
32
|
+
- init.rb
|
33
|
+
- lib/acts_as_readonlyable.rb
|
34
|
+
- README
|
35
|
+
- MIT-LICENSE
|
36
|
+
test_files: []
|
37
|
+
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- README
|
42
|
+
- MIT-LICENSE
|
43
|
+
executables: []
|
44
|
+
|
45
|
+
extensions: []
|
46
|
+
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
dependencies: []
|
50
|
+
|