composite_primary_keys 4.0.0.beta8 → 4.0.0.beta9

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.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ == 4.0.0.beta9 2011-082-22
2
+ * Fix eager-loading in AR 3.1.rc6.
3
+
4
+
1
5
  == 4.0.0.beta8 2011-082-22
2
6
  * Sqlite 3 fixes (Jan Vlnas)
3
7
  * Fixes for to_key method (Jan Vlnas)
@@ -26,7 +26,7 @@ $:.unshift(File.dirname(__FILE__)) unless
26
26
 
27
27
  unless defined?(ActiveRecord)
28
28
  require 'rubygems'
29
- gem 'activerecord', '~> 3.1.0.rc5'
29
+ gem 'activerecord', '~> 3.1.0.rc6'
30
30
  require 'active_record'
31
31
  end
32
32
 
@@ -21,6 +21,51 @@ module ActiveRecord
21
21
  remove_duplicate_results!(active_record, records, @associations)
22
22
  records
23
23
  end
24
+
25
+ protected
26
+
27
+ def construct_association(record, join_part, row)
28
+ return if record.id.to_s != join_part.parent.record_id(row).to_s
29
+
30
+ macro = join_part.reflection.macro
31
+ if macro == :has_one
32
+ return if record.association_cache.key?(join_part.reflection.name)
33
+ # CPK
34
+ # association = join_part.instantiate(row) unless row[join_part.aliased_primary_key].nil?
35
+ association = association_for_primary_key_from_row(join_part, row)
36
+
37
+ set_target_and_inverse(join_part, association, record)
38
+ else
39
+ # CPK
40
+ # association = join_part.instantiate(row) unless row[join_part.aliased_primary_key].nil?
41
+ association = association_for_primary_key_from_row(join_part, row)
42
+
43
+ case macro
44
+ when :has_many, :has_and_belongs_to_many
45
+ other = record.association(join_part.reflection.name)
46
+ other.loaded!
47
+ other.target.push(association) if association
48
+ other.set_inverse_instance(association)
49
+ when :belongs_to
50
+ set_target_and_inverse(join_part, association, record)
51
+ else
52
+ raise ConfigurationError, "unknown macro: #{join_part.reflection.macro}"
53
+ end
54
+ end
55
+ association
56
+ end
57
+
58
+ private
59
+
60
+ def association_for_primary_key_from_row(join_part, row)
61
+ result = nil
62
+ if (cpk = join_part.aliased_primary_key).is_a?(Array)
63
+ result = join_part.instantiate(row) if cpk.detect {|pk| row[pk].nil? }.nil?
64
+ else
65
+ result = join_part.instantiate(row) unless row[join_part.aliased_primary_key].nil?
66
+ end
67
+ result
68
+ end
24
69
  end
25
70
  end
26
71
  end
@@ -3,6 +3,6 @@ module CompositePrimaryKeys
3
3
  MAJOR = 4
4
4
  MINOR = 0
5
5
  TINY = 0
6
- STRING = [MAJOR, MINOR, TINY, 'beta8'].join('.')
6
+ STRING = [MAJOR, MINOR, TINY, 'beta9'].join('.')
7
7
  end
8
8
  end
@@ -42,7 +42,7 @@ class TestAssociations < ActiveSupport::TestCase
42
42
  end
43
43
 
44
44
  # Its not generating the instances of associated classes from the rows
45
- def test_find_includes_products
45
+ def test_find_includes
46
46
  # Old style
47
47
  products = Product.find(:all, :include => :product_tariffs)
48
48
  assert_equal(3, products.length)
@@ -54,6 +54,25 @@ class TestAssociations < ActiveSupport::TestCase
54
54
  assert_equal(3, products.inject(0) {|sum, product| sum + product.product_tariffs.length})
55
55
  end
56
56
 
57
+ def test_find_includes_eager_loading
58
+ product = products(:second_product)
59
+ product_tarrif = product_tariffs(:second_free)
60
+
61
+ # Old style, include a where clause to force eager loading
62
+ products = Product.find(:all, :include => :product_tariffs,
63
+ :conditions => ["product_tariffs.product_id = ?", product.id])
64
+
65
+ assert_equal(1, products.length)
66
+ assert_equal(product, products.first)
67
+ assert_equal([product_tarrif], product.product_tariffs)
68
+
69
+ # New style
70
+ products = Product.includes(:product_tariffs).where('product_tariffs.product_id' => product.id)
71
+ assert_equal(1, products.length)
72
+ assert_equal(product, products.first)
73
+ assert_equal([product_tarrif], product.product_tariffs)
74
+ end
75
+
57
76
  def test_find_includes_tariffs
58
77
  # Old style
59
78
  tariffs = Tariff.find(:all, :include => :product_tariffs)
@@ -234,4 +253,4 @@ class TestAssociations < ActiveSupport::TestCase
234
253
  assert_equal(1, memberships.length)
235
254
  assert_equal([1,1], memberships[0].id)
236
255
  end
237
- end
256
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0.beta8
4
+ version: 4.0.0.beta9
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -14,15 +14,15 @@ date: 2011-08-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
17
- requirement: &21190524 !ruby/object:Gem::Requirement
17
+ requirement: &4158840 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
21
21
  - !ruby/object:Gem::Version
22
- version: 3.1.0.rc5
22
+ version: 3.1.0.rc6
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *21190524
25
+ version_requirements: *4158840
26
26
  description: Composite key support for ActiveRecord 3
27
27
  email:
28
28
  - drnicwilliams@gmail.com