read_from_slave 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -2,17 +2,17 @@ Read_from_slave
2
2
 
3
3
  Read_from_slave for Rails enables database reads from a slave database, while writes continue to go to the master
4
4
 
5
- Read_from_slave will work with Rails 2.2 and above.
5
+ Read_from_slave will work with Rails 2.2 and above, including Rails 3 versions.
6
6
 
7
7
  Installation
8
8
 
9
- sudo gem install sdsykes-read_from_slave
9
+ gem install read_from_slave
10
10
 
11
11
  Configuration
12
12
 
13
13
  In config/environments/production.rb (for instance)
14
14
 
15
- config.gem "sdsykes-read_from_slave", :lib=>"read_from_slave"
15
+ config.gem "read_from_slave"
16
16
 
17
17
  In config/database.yml
18
18
 
data/README.textile CHANGED
@@ -2,13 +2,13 @@ h1. Read_from_slave
2
2
 
3
3
  h4. Read_from_slave for Rails enables database reads from a slave database, while writes continue to go to the master
4
4
 
5
- Read_from_slave will work with Rails 2.2 and above.
5
+ Read_from_slave will work with Rails 2.2 and above, including Rails 3 versions.
6
6
 
7
7
  h2. Installation
8
8
 
9
9
  <pre>
10
10
  <code>
11
- sudo gem install sdsykes-read_from_slave
11
+ gem install read_from_slave
12
12
  </code>
13
13
  </pre>
14
14
 
@@ -18,7 +18,7 @@ In config/environments/production.rb (for instance)
18
18
 
19
19
  <pre>
20
20
  <code>
21
- config.gem "sdsykes-read_from_slave", :lib=>"read_from_slave"
21
+ config.gem "read_from_slave"
22
22
  </code>
23
23
  </pre>
24
24
 
data/VERSION.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
- :minor: 3
3
- :patch: 0
2
+ :minor: 4
3
+ :build:
4
4
  :major: 0
5
+ :patch: 0
@@ -3,10 +3,14 @@
3
3
  # To use read_from_slave you must install the gem, configure the gem in your environment file,
4
4
  # and setup your database.yml file with an entry for your slave database.
5
5
  #
6
+ # gem install read_from_slave
7
+ #
8
+ # Read_from_slave is compatible with Rails 2.2.x and Rails 3
9
+ #
6
10
  # === Configuration
7
11
  # In config/environments/production.rb (for instance)
8
12
  #
9
- # config.gem "sdsykes-read_from_slave", :lib=>"read_from_slave"
13
+ # config.gem "read_from_slave"
10
14
  #
11
15
  # In config/database.yml
12
16
  #
@@ -47,13 +51,26 @@ module ReadFromSlave
47
51
  base = ActiveRecord::Base
48
52
  base.send(:include, InstanceMethods)
49
53
  base.alias_method_chain :reload, :read_from_slave
50
- base.extend(SingletonMethods)
54
+ base.extend(ClassMethods)
51
55
  base.class_eval do
52
56
  class << self
53
57
  alias_method_chain :find_by_sql, :read_from_slave
54
58
  alias_method_chain :connection, :read_from_slave
55
59
  end
56
60
  end
61
+
62
+ begin
63
+ calculation_base = ActiveRecord::Relation # rails 3
64
+ calculation_base.send(:include, CalculationMethod)
65
+ calculation_base.alias_method_chain :calculate, :read_from_slave
66
+ rescue NameError # rails 2
67
+ base.extend(CalculationMethod)
68
+ base.class_eval do
69
+ class << self
70
+ alias_method_chain :calculate, :read_from_slave
71
+ end
72
+ end
73
+ end
57
74
  end
58
75
  end
59
76
 
@@ -64,7 +81,7 @@ module ReadFromSlave
64
81
  end
65
82
  end
66
83
 
67
- module SingletonMethods
84
+ module ClassMethods
68
85
 
69
86
  @@slave_models = {}
70
87
 
@@ -146,6 +163,15 @@ module ReadFromSlave
146
163
  end
147
164
  end
148
165
  end
166
+
167
+ module CalculationMethod
168
+ def calculate_with_read_from_slave(*args)
169
+ Thread.current[:read_from_slave] = true
170
+ calculate_without_read_from_slave(*args)
171
+ ensure
172
+ Thread.current[:read_from_slave] = false
173
+ end
174
+ end
149
175
  end
150
176
 
151
177
  ReadFromSlave.install!
@@ -42,4 +42,10 @@ class ReadFromSlaveTest < ActiveSupport::TestCase
42
42
  ActiveRecord::Base.establish_slave_connections
43
43
  assert_equal conn, Course.connection_without_read_from_slave
44
44
  end
45
+
46
+ test "count should use the slave" do
47
+ Course.create(:name=>"Saw playing")
48
+ assert_equal 1, Course.count
49
+ assert_equal :slave, Thread.current[:read_from_slave_uses]
50
+ end
45
51
  end
data/test/setup.rb CHANGED
@@ -1,3 +1,7 @@
1
1
  require 'rubygems'
2
- require 'activerecord'
2
+
3
+ # specify rails version to test with here
4
+ #gem 'activerecord', '= 2.3.8'
5
+
6
+ require 'active_record'
3
7
  require File.join(File.dirname(__FILE__), '..', 'lib', 'read_from_slave')
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: read_from_slave
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Stephen Sykes
@@ -9,7 +15,7 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-10-10 00:00:00 +03:00
18
+ date: 2010-08-14 00:00:00 +03:00
13
19
  default_executable:
14
20
  dependencies: []
15
21
 
@@ -45,21 +51,27 @@ rdoc_options:
45
51
  require_paths:
46
52
  - lib
47
53
  required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
48
55
  requirements:
49
56
  - - ">="
50
57
  - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
51
61
  version: "0"
52
- version:
53
62
  required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
54
64
  requirements:
55
65
  - - ">="
56
66
  - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
57
70
  version: "0"
58
- version:
59
71
  requirements: []
60
72
 
61
73
  rubyforge_project:
62
- rubygems_version: 1.3.5
74
+ rubygems_version: 1.3.7
63
75
  signing_key:
64
76
  specification_version: 3
65
77
  summary: Read_from_slave - Utilise your slave databases with rails