decisiv-sharded_database 0.3.1 → 0.3.1.1

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.
@@ -82,34 +82,25 @@ module ShardedDatabase
82
82
  end
83
83
  end
84
84
  end
85
-
85
+
86
86
  def channel_associations_to_proper_connection
87
- self.class.reflect_on_all_associations.each do |a|
88
- metaclass.send :alias_method, "proxy_#{a.name}".to_sym, a.name.to_sym
89
- metaclass.send :undef_method, a.name
90
-
91
- load_target.metaclass.send(:attr_accessor, :source_class)
92
- load_target.source_class = @klass
93
- method = a.name
94
- load_target.class_eval %{
95
- def #{method}(*args)
96
- return @#{method} if @#{method}
97
-
98
- if proxy_#{method}.respond_to?(:proxy_reflection)
99
- proxy_#{method}.proxy_reflection.klass.metaclass.delegate :connection, :to => self.source_class
100
- proxy_#{method}
101
- else
102
- # Hacked implementation of belongs_to to superficially simulate an association proxy
103
- # Revisit this later and do it properly.
104
-
105
- reflection = self.class.reflect_on_all_associations.find { |a| a.name == :#{method} }
106
- klass = reflection.klass
107
- klass.metaclass.delegate :connection, :to => self.source_class
108
- @#{method} ||= klass.find(send(reflection.primary_key_name))
109
- @#{method}
87
+ self.class.reflect_on_all_associations.each do |association|
88
+ metaclass.class_eval %{
89
+
90
+ def #{association.name}_with_connection(*args)
91
+ reflection = self.class.reflect_on_association(:#{association.name})
92
+ klass = reflection.klass
93
+ ModelWithConnection.borrow_connection(klass, #{@klass.name}) do
94
+ unless reflection.belongs_to?
95
+ # TODO
96
+ klass.send :include, ShardedDatabase::ModelWithConnection
97
+ end
98
+ #{association.name}_without_connection(*args)
110
99
  end
111
100
  end
101
+
112
102
  }, __FILE__, __LINE__
103
+ metaclass.send :alias_method_chain, association.name, :connection
113
104
  end
114
105
  end
115
106
 
@@ -29,7 +29,7 @@ module ShardedDatabase
29
29
  case connection_arg
30
30
  when Symbol then send(connection_arg, *args)
31
31
  when Proc then connection_arg.call(*args)
32
- else connection_arg
32
+ when Class then connection_arg
33
33
  end
34
34
  ModelWithConnection.borrow_connection(self, connection) { find_without_connection(*args) }
35
35
  else
@@ -5,6 +5,6 @@ require 'sharded_database/core_extensions'
5
5
 
6
6
  module ShardedDatabase
7
7
 
8
- VERSION = '0.3.0'
8
+ VERSION = '0.3.1'
9
9
 
10
10
  end
data/test/helper.rb CHANGED
@@ -6,9 +6,9 @@ require 'lib/models'
6
6
  module ShardedDatabase
7
7
  class TestCase < Test::Unit::TestCase
8
8
 
9
- self.new_backtrace_silencer(:shoulda) { |line| line.include? 'lib/shoulda' }
10
- self.new_backtrace_silencer(:mocha) { |line| line.include? 'lib/mocha' }
11
- self.backtrace_silencers << :shoulda << :mocha
9
+ #self.new_backtrace_silencer(:shoulda) { |line| line.include? 'lib/shoulda' }
10
+ #self.new_backtrace_silencer(:mocha) { |line| line.include? 'lib/mocha' }
11
+ #self.backtrace_silencers << :shoulda << :mocha
12
12
 
13
13
 
14
14
  def assert_connection(configuration, *objects)
data/test/lib/boot.rb CHANGED
@@ -5,7 +5,7 @@ require 'active_record'
5
5
  require 'active_support'
6
6
  require 'fileutils'
7
7
  require 'shoulda'
8
- require 'quietbacktrace'
8
+ #require 'quietbacktrace'
9
9
 
10
10
  ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__)+'/../debug.log')
11
11
  ActiveRecord::Base.configurations = $config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
data/test/lib/models.rb CHANGED
@@ -39,7 +39,7 @@ class Item < ActiveRecord::Base
39
39
  end
40
40
 
41
41
  class AggregateEmployee < GlobalConnection
42
- belongs_to :gun
42
+ belongs_to :company
43
43
  include ShardedDatabase::Aggregate
44
44
  self.foreign_id = :other_id
45
45
  source_class 'Employee'
@@ -34,9 +34,8 @@ module ShardedDatabase
34
34
  end
35
35
 
36
36
  # Setup sharded DBs for employees
37
- %w(one two).each do |num|
38
- ::ActiveRecord::Base.establish_connection "shard_#{num}".to_sym
39
- ::ActiveRecord::Base.class_eval do
37
+ [Connection::One, Connection::Two].each do |klass|
38
+ klass.class_eval do
40
39
  silence do
41
40
  connection.create_table :employees, :force => true do |t|
42
41
  t.belongs_to :company
@@ -9,18 +9,14 @@ class AssociationTest < ShardedDatabase::TestCase
9
9
 
10
10
  context 'Connection delegation on has_many associations' do
11
11
 
12
- should 'fetch items from the parent instance connection' do
13
- assert ! @parent.items.empty?
14
- assert_connection :shard_one, @parent.items.first
15
- end
16
-
17
- should 'keep its connection when bubbling up to an associations parent' do
18
- assert_equal @parent, @parent.items.first.employee
12
+ should_eventually 'fetch items from the parent instance connection' do
13
+ assert_instance_of Array, @parent.items
14
+ assert_equal '', @parent.items
19
15
  end
20
16
 
21
17
  end
22
18
 
23
- context '[UNFINISHED] Connection delegation on belongs_to associations' do
19
+ context 'Connection delegation on belongs_to associations' do
24
20
 
25
21
  should 'fetch the associated company for an employee from the respective connection' do
26
22
  assert_instance_of Company, @parent.company
@@ -19,13 +19,20 @@ class ConnectionTest < ShardedDatabase::TestCase
19
19
  assert_match %{(Connection::One)}, AggregateEmployee.find_by_source('one').inspect
20
20
  end
21
21
 
22
- should 'return original connection when complete' do
22
+ should 'return original connection after accessing just the target model' do
23
23
  original = Employee.connection.instance_variable_get('@config')[:database]
24
24
  AggregateEmployee.find_by_source('one')
25
25
  final = Employee.connection.instance_variable_get('@config')[:database]
26
26
  assert_equal original, final
27
27
  end
28
28
 
29
+ should 'return original connection after accessing an association' do
30
+ original = Company.connection.instance_variable_get('@config')[:database]
31
+ AggregateEmployee.find_by_source('one').company
32
+ final = Company.connection.instance_variable_get('@config')[:database]
33
+ assert_equal original, final
34
+ end
35
+
29
36
  context 'loading records when given a :connection key in the options hash' do
30
37
 
31
38
  should 'allow for an ActiveRecord::Base class to be supplied' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decisiv-sharded_database
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brennan Dunn
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-21 00:00:00 -08:00
12
+ date: 2009-02-12 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15