ar-octopus 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. data/.gitignore +11 -0
  2. data/.travis.yml +22 -0
  3. data/Appraisals +18 -0
  4. data/Gemfile +3 -12
  5. data/README.mkdn +63 -24
  6. data/Rakefile +70 -92
  7. data/ar-octopus.gemspec +25 -198
  8. data/lib/ar-octopus.rb +1 -0
  9. data/lib/octopus.rb +73 -25
  10. data/lib/octopus/association.rb +6 -5
  11. data/lib/octopus/association_collection.rb +58 -4
  12. data/lib/octopus/has_and_belongs_to_many_association.rb +4 -4
  13. data/lib/octopus/logger.rb +9 -4
  14. data/lib/octopus/migration.rb +155 -50
  15. data/lib/octopus/model.rb +98 -34
  16. data/lib/octopus/proxy.rb +124 -53
  17. data/lib/octopus/rails2/association.rb +46 -93
  18. data/lib/octopus/rails2/persistence.rb +1 -1
  19. data/lib/octopus/rails2/scope.rb +17 -0
  20. data/lib/octopus/rails3.1/singular_association.rb +34 -0
  21. data/lib/octopus/rails3.2/persistence.rb +12 -0
  22. data/lib/octopus/rails3/abstract_adapter.rb +39 -0
  23. data/lib/octopus/rails3/arel.rb +5 -5
  24. data/lib/octopus/rails3/log_subscriber.rb +22 -0
  25. data/lib/octopus/rails3/persistence.rb +10 -5
  26. data/lib/octopus/railtie.rb +13 -0
  27. data/lib/octopus/scope_proxy.rb +22 -16
  28. data/lib/octopus/version.rb +3 -0
  29. data/lib/tasks/octopus.rake +20 -0
  30. data/sample_app/Gemfile +2 -2
  31. data/sample_app/config/initializers/inflections.rb +1 -1
  32. data/sample_app/config/initializers/secret_token.rb +1 -1
  33. data/sample_app/db/migrate/20100720172730_create_items.rb +1 -1
  34. data/sample_app/db/migrate/20100720210335_create_sample_users.rb +1 -1
  35. data/sample_app/db/seeds.rb +1 -1
  36. data/sample_app/features/migrate.feature +12 -12
  37. data/sample_app/features/seed.feature +3 -3
  38. data/sample_app/features/step_definitions/web_steps.rb +5 -5
  39. data/sample_app/features/support/env.rb +8 -8
  40. data/sample_app/lib/tasks/cucumber.rake +2 -2
  41. data/sample_app/public/javascripts/effects.js +1 -1
  42. data/spec/config/shards.yml +38 -28
  43. data/spec/migrations/11_add_field_in_all_slaves.rb +1 -1
  44. data/spec/migrations/12_create_users_using_block.rb +2 -2
  45. data/spec/migrations/13_create_users_using_block_and_using.rb +2 -2
  46. data/spec/migrations/14_create_users_on_shards_of_a_group_with_versions.rb +11 -0
  47. data/spec/migrations/1_create_users_on_master.rb +1 -1
  48. data/spec/migrations/2_create_users_on_canada.rb +1 -1
  49. data/spec/migrations/3_create_users_on_both_shards.rb +1 -1
  50. data/spec/migrations/4_create_users_on_shards_of_a_group.rb +1 -1
  51. data/spec/migrations/5_create_users_on_multiples_groups.rb +1 -1
  52. data/spec/migrations/6_raise_exception_with_invalid_shard_name.rb +1 -1
  53. data/spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb +1 -1
  54. data/spec/migrations/8_raise_exception_with_invalid_group_name.rb +1 -1
  55. data/spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb +1 -1
  56. data/spec/octopus/association_spec.rb +88 -70
  57. data/spec/octopus/log_subscriber_spec.rb +22 -0
  58. data/spec/octopus/logger_spec.rb +28 -15
  59. data/spec/octopus/migration_spec.rb +47 -43
  60. data/spec/octopus/model_spec.rb +179 -13
  61. data/spec/octopus/octopus_spec.rb +26 -4
  62. data/spec/octopus/proxy_spec.rb +61 -23
  63. data/spec/octopus/{replication_specs.rb → replication_spec.rb} +33 -26
  64. data/spec/octopus/scope_proxy_spec.rb +3 -3
  65. data/spec/octopus/sharded_spec.rb +9 -9
  66. data/spec/spec_helper.rb +10 -12
  67. data/spec/support/active_record/connection_adapters/modify_config_adapter.rb +17 -0
  68. data/spec/support/database_connection.rb +2 -0
  69. data/spec/{database_models.rb → support/database_models.rb} +27 -2
  70. data/spec/support/octopus_helper.rb +50 -0
  71. data/spec/tasks/octopus.rake_spec.rb +36 -0
  72. metadata +188 -169
  73. data/Gemfile.lock +0 -68
  74. data/lib/octopus/rails3/association.rb +0 -112
  75. data/spec/database_connection.rb +0 -4
  76. data/spec/octopus/controller_spec.rb +0 -34
  77. data/spec/octopus_helper.rb +0 -37
@@ -1,206 +1,33 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "octopus/version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
- s.name = %q{ar-octopus}
8
- s.version = "0.4.0"
6
+ s.name = "ar-octopus"
7
+ s.version = Octopus::VERSION
8
+ s.authors = ["Thiago Pradi", "Mike Perham", "Gabriel Sobrinho"]
9
+ s.email = ["tchandy@gmail.com", "mperham@gmail.com", "gabriel.sobrinho@gmail.com"]
10
+ s.homepage = "https://github.com/tchandy/octopus"
11
+ s.summary = %q{Easy Database Sharding for ActiveRecord}
12
+ s.description = %q{This gem allows you to use sharded databases with ActiveRecord. This also provides a interface for replication and for running migrations with multiples shards.}
9
13
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Thiago Pradi", "Mike Perham"]
12
- s.date = %q{2011-07-03}
13
- s.description = %q{This gem allows you to use sharded databases with ActiveRecord. this also provides a interface for replication and for running migrations with multiples shards.}
14
- s.email = %q{tchandy@gmail.com}
15
- s.extra_rdoc_files = [
16
- "README.mkdn"
17
- ]
18
- s.files = [
19
- ".rspec",
20
- "Gemfile",
21
- "Gemfile.lock",
22
- "README.mkdn",
23
- "Rakefile",
24
- "TODO.txt",
25
- "ar-octopus.gemspec",
26
- "init.rb",
27
- "lib/octopus.rb",
28
- "lib/octopus/association.rb",
29
- "lib/octopus/association_collection.rb",
30
- "lib/octopus/has_and_belongs_to_many_association.rb",
31
- "lib/octopus/logger.rb",
32
- "lib/octopus/migration.rb",
33
- "lib/octopus/model.rb",
34
- "lib/octopus/proxy.rb",
35
- "lib/octopus/rails2/association.rb",
36
- "lib/octopus/rails2/persistence.rb",
37
- "lib/octopus/rails3/arel.rb",
38
- "lib/octopus/rails3/association.rb",
39
- "lib/octopus/rails3/persistence.rb",
40
- "lib/octopus/scope_proxy.rb",
41
- "rails/init.rb",
42
- "sample_app/.gitignore",
43
- "sample_app/.rspec",
44
- "sample_app/Gemfile",
45
- "sample_app/Gemfile.lock",
46
- "sample_app/README",
47
- "sample_app/Rakefile",
48
- "sample_app/app/controllers/application_controller.rb",
49
- "sample_app/app/helpers/application_helper.rb",
50
- "sample_app/app/models/item.rb",
51
- "sample_app/app/models/user.rb",
52
- "sample_app/app/views/layouts/application.html.erb",
53
- "sample_app/autotest/discover.rb",
54
- "sample_app/config.ru",
55
- "sample_app/config/application.rb",
56
- "sample_app/config/boot.rb",
57
- "sample_app/config/cucumber.yml",
58
- "sample_app/config/database.yml",
59
- "sample_app/config/environment.rb",
60
- "sample_app/config/environments/development.rb",
61
- "sample_app/config/environments/production.rb",
62
- "sample_app/config/environments/test.rb",
63
- "sample_app/config/initializers/backtrace_silencers.rb",
64
- "sample_app/config/initializers/inflections.rb",
65
- "sample_app/config/initializers/mime_types.rb",
66
- "sample_app/config/initializers/secret_token.rb",
67
- "sample_app/config/initializers/session_store.rb",
68
- "sample_app/config/locales/en.yml",
69
- "sample_app/config/routes.rb",
70
- "sample_app/config/shards.yml",
71
- "sample_app/db/migrate/20100720172715_create_users.rb",
72
- "sample_app/db/migrate/20100720172730_create_items.rb",
73
- "sample_app/db/migrate/20100720210335_create_sample_users.rb",
74
- "sample_app/db/schema.rb",
75
- "sample_app/db/seeds.rb",
76
- "sample_app/doc/README_FOR_APP",
77
- "sample_app/features/migrate.feature",
78
- "sample_app/features/seed.feature",
79
- "sample_app/features/step_definitions/seeds_steps.rb",
80
- "sample_app/features/step_definitions/web_steps.rb",
81
- "sample_app/features/support/env.rb",
82
- "sample_app/features/support/paths.rb",
83
- "sample_app/lib/tasks/.gitkeep",
84
- "sample_app/lib/tasks/cucumber.rake",
85
- "sample_app/public/404.html",
86
- "sample_app/public/422.html",
87
- "sample_app/public/500.html",
88
- "sample_app/public/favicon.ico",
89
- "sample_app/public/images/rails.png",
90
- "sample_app/public/index.html",
91
- "sample_app/public/javascripts/application.js",
92
- "sample_app/public/javascripts/controls.js",
93
- "sample_app/public/javascripts/dragdrop.js",
94
- "sample_app/public/javascripts/effects.js",
95
- "sample_app/public/javascripts/prototype.js",
96
- "sample_app/public/javascripts/rails.js",
97
- "sample_app/public/robots.txt",
98
- "sample_app/public/stylesheets/.gitkeep",
99
- "sample_app/script/cucumber",
100
- "sample_app/script/rails",
101
- "sample_app/spec/models/item_spec.rb",
102
- "sample_app/spec/models/user_spec.rb",
103
- "sample_app/spec/spec_helper.rb",
104
- "sample_app/test/performance/browsing_test.rb",
105
- "sample_app/test/test_helper.rb",
106
- "sample_app/vendor/plugins/.gitkeep",
107
- "spec/config/shards.yml",
108
- "spec/database_connection.rb",
109
- "spec/database_models.rb",
110
- "spec/migrations/10_create_users_using_replication.rb",
111
- "spec/migrations/11_add_field_in_all_slaves.rb",
112
- "spec/migrations/12_create_users_using_block.rb",
113
- "spec/migrations/13_create_users_using_block_and_using.rb",
114
- "spec/migrations/1_create_users_on_master.rb",
115
- "spec/migrations/2_create_users_on_canada.rb",
116
- "spec/migrations/3_create_users_on_both_shards.rb",
117
- "spec/migrations/4_create_users_on_shards_of_a_group.rb",
118
- "spec/migrations/5_create_users_on_multiples_groups.rb",
119
- "spec/migrations/6_raise_exception_with_invalid_shard_name.rb",
120
- "spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb",
121
- "spec/migrations/8_raise_exception_with_invalid_group_name.rb",
122
- "spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb",
123
- "spec/octopus/association_spec.rb",
124
- "spec/octopus/controller_spec.rb",
125
- "spec/octopus/logger_spec.rb",
126
- "spec/octopus/migration_spec.rb",
127
- "spec/octopus/model_spec.rb",
128
- "spec/octopus/octopus_spec.rb",
129
- "spec/octopus/proxy_spec.rb",
130
- "spec/octopus/replication_specs.rb",
131
- "spec/octopus/scope_proxy_spec.rb",
132
- "spec/octopus/sharded_spec.rb",
133
- "spec/octopus_helper.rb",
134
- "spec/spec_helper.rb"
135
- ]
136
- s.homepage = %q{http://github.com/tchandy/octopus}
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
137
17
  s.require_paths = ["lib"]
138
- s.rubygems_version = %q{1.4.2}
139
- s.summary = %q{Easy Database Sharding for ActiveRecord}
140
- s.test_files = [
141
- "spec/database_connection.rb",
142
- "spec/database_models.rb",
143
- "spec/migrations/10_create_users_using_replication.rb",
144
- "spec/migrations/11_add_field_in_all_slaves.rb",
145
- "spec/migrations/12_create_users_using_block.rb",
146
- "spec/migrations/13_create_users_using_block_and_using.rb",
147
- "spec/migrations/1_create_users_on_master.rb",
148
- "spec/migrations/2_create_users_on_canada.rb",
149
- "spec/migrations/3_create_users_on_both_shards.rb",
150
- "spec/migrations/4_create_users_on_shards_of_a_group.rb",
151
- "spec/migrations/5_create_users_on_multiples_groups.rb",
152
- "spec/migrations/6_raise_exception_with_invalid_shard_name.rb",
153
- "spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb",
154
- "spec/migrations/8_raise_exception_with_invalid_group_name.rb",
155
- "spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb",
156
- "spec/octopus/association_spec.rb",
157
- "spec/octopus/controller_spec.rb",
158
- "spec/octopus/logger_spec.rb",
159
- "spec/octopus/migration_spec.rb",
160
- "spec/octopus/model_spec.rb",
161
- "spec/octopus/octopus_spec.rb",
162
- "spec/octopus/proxy_spec.rb",
163
- "spec/octopus/replication_specs.rb",
164
- "spec/octopus/scope_proxy_spec.rb",
165
- "spec/octopus/sharded_spec.rb",
166
- "spec/octopus_helper.rb",
167
- "spec/spec_helper.rb"
168
- ]
169
18
 
170
- if s.respond_to? :specification_version then
171
- s.specification_version = 3
19
+ s.post_install_message = "Important: If you are upgrading from < Octopus 0.5.0 you need to run:\n" \
20
+ "$ rake octopus:copy_schema_versions\n\n" \
21
+ "Octopus now stores schema version information in each shard and migrations will not " \
22
+ "work properly unless this task is invoked."
172
23
 
173
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
174
- s.add_runtime_dependency(%q<activerecord>, ["= 3.0.6"])
175
- s.add_runtime_dependency(%q<actionpack>, ["= 3.0.6"])
176
- s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
177
- s.add_development_dependency(%q<mysql2>, [">= 0"])
178
- s.add_development_dependency(%q<pg>, [">= 0.9.0"])
179
- s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
180
- s.add_development_dependency(%q<jeweler>, [">= 1.4"])
181
- s.add_development_dependency(%q<actionpack>, [">= 2.3"])
182
- s.add_runtime_dependency(%q<activerecord>, [">= 2.3"])
183
- else
184
- s.add_dependency(%q<activerecord>, ["= 3.0.6"])
185
- s.add_dependency(%q<actionpack>, ["= 3.0.6"])
186
- s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
187
- s.add_dependency(%q<mysql2>, [">= 0"])
188
- s.add_dependency(%q<pg>, [">= 0.9.0"])
189
- s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
190
- s.add_dependency(%q<jeweler>, [">= 1.4"])
191
- s.add_dependency(%q<actionpack>, [">= 2.3"])
192
- s.add_dependency(%q<activerecord>, [">= 2.3"])
193
- end
194
- else
195
- s.add_dependency(%q<activerecord>, ["= 3.0.6"])
196
- s.add_dependency(%q<actionpack>, ["= 3.0.6"])
197
- s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
198
- s.add_dependency(%q<mysql2>, [">= 0"])
199
- s.add_dependency(%q<pg>, [">= 0.9.0"])
200
- s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
201
- s.add_dependency(%q<jeweler>, [">= 1.4"])
202
- s.add_dependency(%q<actionpack>, [">= 2.3"])
203
- s.add_dependency(%q<activerecord>, [">= 2.3"])
204
- end
24
+ s.add_dependency 'activerecord', '>= 2.3.0'
25
+ s.add_dependency 'activesupport', '>= 2.3.0'
26
+ s.add_development_dependency 'rake', '>= 0.8.7'
27
+ s.add_development_dependency 'rspec', '>= 2.0.0'
28
+ s.add_development_dependency 'mysql', '2.8.1'
29
+ s.add_development_dependency 'pg', '>= 0.11.0'
30
+ s.add_development_dependency 'sqlite3', '>= 1.3.4'
31
+ s.add_development_dependency 'pry'
32
+ s.add_development_dependency 'appraisal', '>= 0.3.8'
205
33
  end
206
-
@@ -0,0 +1 @@
1
+ require "octopus"
@@ -1,3 +1,7 @@
1
+ require 'active_record'
2
+ require 'active_support/version'
3
+ require 'active_support/core_ext/class'
4
+
1
5
  require "yaml"
2
6
  require "erb"
3
7
 
@@ -10,26 +14,39 @@ module Octopus
10
14
  @rails_env ||= self.rails? ? Rails.env.to_s : 'shards'
11
15
  end
12
16
 
13
- def self.config()
14
- file_name = Octopus.directory() + "/config/shards.yml"
15
-
16
- if File.exists? file_name
17
- @config ||= HashWithIndifferentAccess.new(YAML.load(ERB.new(File.open(file_name).read()).result))[Octopus.env()]
17
+ def self.config
18
+ @config ||= begin
19
+ file_name = Octopus.directory() + "/config/shards.yml"
18
20
 
19
- if @config && @config['environments']
20
- self.environments = @config['environments']
21
+ if File.exists?(file_name)
22
+ config ||= HashWithIndifferentAccess.new(YAML.load(ERB.new(File.read(file_name)).result))[Octopus.env()]
23
+ else
24
+ config ||= HashWithIndifferentAccess.new
21
25
  end
22
- else
23
- @config ||= HashWithIndifferentAccess.new
26
+
27
+ config
24
28
  end
29
+ end
25
30
 
26
- @config
31
+ # Public: Whether or not Octopus is configured and should hook into the
32
+ # current environment. Checks the environments config option for the Rails
33
+ # environment by default.
34
+ #
35
+ # Returns a boolean
36
+ def self.enabled?
37
+ if defined?(::Rails)
38
+ Octopus.environments.include?(Rails.env.to_s)
39
+ else
40
+ # TODO: This doens't feel right but !Octopus.config.blank? is breaking a
41
+ # test. Also, Octopus.config is always returning a hash.
42
+ Octopus.config
43
+ end
27
44
  end
28
45
 
29
46
  # Returns the Rails.root_to_s when you are using rails
30
47
  # Running the current directory in a generic Ruby process
31
48
  def self.directory()
32
- @directory ||= defined?(Rails) ? Rails.root.to_s : Dir.pwd
49
+ @directory ||= defined?(Rails) ? Rails.root.to_s : Dir.pwd
33
50
  end
34
51
 
35
52
  # This is the default way to do Octopus Setup
@@ -44,33 +61,47 @@ module Octopus
44
61
  end
45
62
 
46
63
  def self.environments
47
- @environments || ['production']
64
+ @environments ||= config['environments'] || ['production']
65
+ end
66
+
67
+ def self.rails2?
68
+ ActiveRecord::VERSION::MAJOR == 2
48
69
  end
49
70
 
50
71
  def self.rails3?
51
72
  ActiveRecord::VERSION::MAJOR == 3
52
73
  end
53
74
 
75
+ def self.rails30?
76
+ ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 0
77
+ end
78
+
79
+ def self.rails31?
80
+ ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 1
81
+ end
82
+
83
+ def self.rails32?
84
+ ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 2
85
+ end
86
+
54
87
  def self.rails?
55
- defined?(Rails)
88
+ defined?(Rails)
56
89
  end
57
-
90
+
58
91
  def self.shards=(shards)
59
- @config ||= HashWithIndifferentAccess.new
60
- @config[rails_env()] = HashWithIndifferentAccess.new(shards)
92
+ config[rails_env()] = HashWithIndifferentAccess.new(shards)
61
93
  ActiveRecord::Base.connection.initialize_shards(@config)
62
94
  end
63
-
95
+
64
96
  def self.using(shard, &block)
65
- ActiveRecord::Base.hijack_initializer()
66
97
  conn = ActiveRecord::Base.connection
67
-
98
+
68
99
  if conn.is_a?(Octopus::Proxy)
69
100
  conn.run_queries_on_shard(shard, &block)
70
101
  else
71
102
  yield
72
103
  end
73
- end
104
+ end
74
105
  end
75
106
 
76
107
 
@@ -80,15 +111,32 @@ require "octopus/association_collection"
80
111
  require "octopus/has_and_belongs_to_many_association"
81
112
  require "octopus/association"
82
113
 
114
+ if Octopus.rails2?
115
+ require "octopus/rails2/association"
116
+ require "octopus/rails2/persistence"
117
+ require "octopus/rails2/scope"
118
+ end
119
+
83
120
  if Octopus.rails3?
84
- require "octopus/rails3/association"
85
121
  require "octopus/rails3/persistence"
86
- require "octopus/rails3/arel"
87
- else
122
+ require "octopus/rails3/log_subscriber"
123
+ require "octopus/rails3/abstract_adapter"
124
+ require "octopus/railtie"
125
+ end
126
+
127
+ if Octopus.rails30?
88
128
  require "octopus/rails2/association"
89
- require "octopus/rails2/persistence"
129
+ require "octopus/rails3/arel"
130
+ end
131
+
132
+ if Octopus.rails31? || Octopus.rails32?
133
+ require "octopus/rails3.1/singular_association"
134
+ end
135
+
136
+ if Octopus.rails32?
137
+ require "octopus/rails3.2/persistence"
90
138
  end
91
139
 
92
140
  require "octopus/proxy"
93
141
  require "octopus/scope_proxy"
94
- require "octopus/logger"
142
+ require "octopus/logger"
@@ -5,11 +5,12 @@ module Octopus::Association
5
5
 
6
6
  module InstanceMethods
7
7
  def set_connection_on_association(record)
8
+ return unless ::Octopus.enabled?
8
9
  return if !self.connection.respond_to?(:current_shard) || !self.respond_to?(:current_shard)
9
10
  if !record.current_shard.nil? && !self.current_shard.nil? && record.current_shard != self.current_shard
10
- raise "Association Error: Records are from different shards"
11
+ raise "Association Error: Records are from different shards"
11
12
  end
12
-
13
+
13
14
  record.current_shard = self.connection.current_shard = self.current_shard if should_set_current_shard?
14
15
  end
15
16
  end
@@ -28,7 +29,7 @@ module Octopus::Association
28
29
  if options[:before_add].is_a?(Array)
29
30
  options[:before_add] << :set_connection_on_association
30
31
  elsif options[:before_add].is_a?(Symbol)
31
- options[:before_add] = [:set_connection_on_association, options[:before_add]]
32
+ options[:before_add] = [:set_connection_on_association, options[:before_add]]
32
33
  else
33
34
  options[:before_add] = :set_connection_on_association
34
35
  end
@@ -36,11 +37,11 @@ module Octopus::Association
36
37
  if options[:before_remove].is_a?(Array)
37
38
  options[:before_remove] << :set_connection_on_association
38
39
  elsif options[:before_remove].is_a?(Symbol)
39
- options[:before_remove] = [:set_connection_on_association, options[:before_remove]]
40
+ options[:before_remove] = [:set_connection_on_association, options[:before_remove]]
40
41
  else
41
42
  options[:before_remove] = :set_connection_on_association
42
43
  end
43
44
  end
44
45
  end
45
46
 
46
- ActiveRecord::Base.extend(Octopus::Association)
47
+ ActiveRecord::Base.extend(Octopus::Association)
@@ -1,15 +1,69 @@
1
- module Octopus::AssociationCollection
1
+ module Octopus::AssociationCollection
2
+
3
+ def self.included(base)
4
+ if Octopus.rails31? || Octopus.rails32?
5
+ base.instance_eval do
6
+ alias_method_chain :reader, :octopus
7
+ alias_method_chain :writer, :octopus
8
+ alias_method_chain :ids_reader, :octopus
9
+ alias_method_chain :ids_writer, :octopus
10
+ alias_method_chain :create, :octopus
11
+ alias_method_chain :create!, :octopus
12
+ alias_method_chain :build, :octopus
13
+ end
14
+ end
15
+ end
16
+
17
+ def build_with_octopus(*args, &block)
18
+ owner.reload_connection
19
+ build_without_octopus(*args, &block)
20
+ end
21
+
22
+ def reader_with_octopus(*args)
23
+ owner.reload_connection
24
+ reader_without_octopus(*args)
25
+ end
26
+
27
+ def writer_with_octopus(*args)
28
+ owner.reload_connection
29
+ writer_without_octopus(*args)
30
+ end
31
+
32
+ def ids_reader_with_octopus(*args)
33
+ owner.reload_connection
34
+ ids_reader_without_octopus(*args)
35
+ end
36
+
37
+ def ids_writer_with_octopus(*args)
38
+ owner.reload_connection
39
+ ids_writer_without_octopus(*args)
40
+ end
41
+
42
+ def create_with_octopus(*args, &block)
43
+ owner.reload_connection
44
+ create_without_octopus(*args, &block)
45
+ end
46
+
47
+ def create_with_octopus!(*args, &block)
48
+ owner.reload_connection
49
+ create_without_octopus!(*args, &block)
50
+ end
51
+
2
52
  def should_wrap_the_connection?
3
53
  @owner.respond_to?(:current_shard) && @owner.current_shard != nil
4
54
  end
5
55
 
6
56
  def count(*args)
7
57
  if should_wrap_the_connection?
8
- Octopus.using(@owner.current_shard) { super }
9
- else
58
+ Octopus.using(@owner.current_shard) { super }
59
+ else
10
60
  super
11
61
  end
12
62
  end
13
63
  end
14
64
 
15
- ActiveRecord::Associations::AssociationCollection.send(:include, Octopus::AssociationCollection)
65
+ if Octopus.rails31? || Octopus.rails32?
66
+ ActiveRecord::Associations::CollectionAssociation.send(:include, Octopus::AssociationCollection)
67
+ else
68
+ ActiveRecord::Associations::AssociationCollection.send(:include, Octopus::AssociationCollection)
69
+ end