decisiv-sharded_database 0.1.3 → 0.1.4

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.
@@ -4,6 +4,6 @@ require 'sharded_database/core_extensions'
4
4
 
5
5
  module ShardedDatabase
6
6
 
7
- VERSION = '0.1.3'
7
+ VERSION = '0.1.4'
8
8
 
9
9
  end
@@ -41,10 +41,25 @@ module ShardedDatabase
41
41
  load_target.source_class = @klass
42
42
  load_target.class_eval %{
43
43
  def #{method}(*args)
44
- proxy_#{method}.proxy_reflection.klass.metaclass.delegate :connection, :to => self.source_class
45
- proxy_#{method}
44
+ if proxy_#{method}.respond_to?(:proxy_reflection)
45
+ proxy_#{method}.proxy_reflection.klass.metaclass.delegate :connection, :to => self.source_class
46
+ proxy_#{method}
47
+ else
48
+ # Hacked implementation of belongs_to to superficially simulate an association proxy
49
+ # Revisit this later and do it properly.
50
+
51
+ reflection = self.class.reflect_on_all_associations.find { |a| a.name == :#{method} }
52
+ klass = reflection.klass
53
+
54
+ @original_connection = klass.connection
55
+ klass.metaclass.delegate :connection, :to => self.source_class
56
+ value = klass.find(send(reflection.primary_key_name))
57
+ klass.metaclass.delegate :connection, :to => @original_connection
58
+
59
+ value
60
+ end
46
61
  end
47
- }
62
+ }, __FILE__, __LINE__
48
63
  end
49
64
 
50
65
  end
data/test/lib/models.rb CHANGED
@@ -18,6 +18,7 @@ class GlobalConnection < ActiveRecord::Base
18
18
  end
19
19
 
20
20
  class AggregateEstimate < GlobalConnection
21
+ belongs_to :gun
21
22
  include ShardedDatabase::Aggregate
22
23
  self.foreign_id = :other_id
23
24
  preserve_attributes :source
@@ -28,7 +29,12 @@ class AggregateEstimate < GlobalConnection
28
29
 
29
30
  end
30
31
 
31
- class Estimate < ActiveRecord::Base
32
+ class Company < ActiveRecord::Base
33
+ has_many :items
34
+ end
35
+
36
+ class Estimate < ActiveRecord::Base
37
+ belongs_to :company
32
38
  has_many :items
33
39
  end
34
40
 
@@ -39,6 +39,7 @@ module ShardedDatabase
39
39
  ::ActiveRecord::Base.class_eval do
40
40
  silence do
41
41
  connection.create_table :estimates, :force => true do |t|
42
+ t.belongs_to :company
42
43
  t.string :name
43
44
  end
44
45
 
@@ -46,6 +47,11 @@ module ShardedDatabase
46
47
  t.belongs_to :estimate
47
48
  t.string :name
48
49
  end
50
+
51
+ connection.create_table :companies, :force => true do |t|
52
+ t.string :name
53
+ end
54
+
49
55
  end
50
56
  end
51
57
  end
@@ -53,8 +59,11 @@ module ShardedDatabase
53
59
  end
54
60
 
55
61
  def setup_models
56
- one_estimate = Class.new(Connection::One) { set_table_name 'estimates' ; has_many(:items) }
57
- @one_1 = one_estimate.create :name => 'One Estimate'
62
+ one_company = Class.new(Connection::One) { set_table_name 'companies' }
63
+ @company_1 = one_company.create :name => 'One Company'
64
+
65
+ one_estimate = Class.new(Connection::One) { set_table_name 'estimates' ; has_many(:items) ; belongs_to(:company) }
66
+ @one_1 = one_estimate.create :name => 'One Estimate', :company_id => @company_1.id
58
67
 
59
68
  two_estimate = Class.new(Connection::Two) { set_table_name 'estimates' ; has_many(:items) }
60
69
  @two_1 = two_estimate.create :name => 'One Estimate 1'
@@ -1,15 +1,14 @@
1
1
  require File.dirname(__FILE__) + '/../helper'
2
2
 
3
3
  class AssociationTest < ShardedDatabase::TestCase
4
- def setup ; setup_environment ; end
5
4
 
5
+ def setup
6
+ setup_environment
7
+ @parent = AggregateEstimate.find_by_source('one')
8
+ end
6
9
 
7
10
  context 'Connection delegation on has_many associations' do
8
11
 
9
- setup do
10
- @parent = AggregateEstimate.find_by_source('one')
11
- end
12
-
13
12
  should 'fetch items from the parent instance connection' do
14
13
  assert ! @parent.items.empty?
15
14
  assert_connection :one_db, @parent.items.first
@@ -21,4 +20,12 @@ class AssociationTest < ShardedDatabase::TestCase
21
20
 
22
21
  end
23
22
 
23
+ context '[UNFINISHED] Connection delegation on belongs_to associations' do
24
+
25
+ should 'fetch the associated company for an estimate from the respective connection' do
26
+ assert_instance_of Company, @parent.company
27
+ end
28
+
29
+ end
30
+
24
31
  end
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.1.3
4
+ version: 0.1.4
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-12 00:00:00 -08:00
12
+ date: 2009-01-20 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15