composite_primary_keys 8.1.8 → 9.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +5 -5
  2. data/History.rdoc +3 -25
  3. data/README.rdoc +1 -0
  4. data/README_DB2.rdoc +33 -33
  5. data/Rakefile +34 -34
  6. data/lib/composite_primary_keys.rb +4 -11
  7. data/lib/composite_primary_keys/associations/association.rb +14 -12
  8. data/lib/composite_primary_keys/associations/association_scope.rb +27 -54
  9. data/lib/composite_primary_keys/associations/collection_association.rb +22 -8
  10. data/lib/composite_primary_keys/associations/has_many_association.rb +16 -54
  11. data/lib/composite_primary_keys/associations/has_many_through_association.rb +58 -58
  12. data/lib/composite_primary_keys/associations/join_dependency.rb +74 -56
  13. data/lib/composite_primary_keys/associations/join_dependency/join_association.rb +13 -11
  14. data/lib/composite_primary_keys/associations/preloader/association.rb +75 -72
  15. data/lib/composite_primary_keys/associations/singular_association.rb +8 -12
  16. data/lib/composite_primary_keys/attribute_methods.rb +6 -4
  17. data/lib/composite_primary_keys/attribute_methods/primary_key.rb +13 -11
  18. data/lib/composite_primary_keys/attribute_methods/read.rb +16 -15
  19. data/lib/composite_primary_keys/attribute_methods/write.rb +21 -19
  20. data/lib/composite_primary_keys/attribute_set/builder.rb +13 -11
  21. data/lib/composite_primary_keys/base.rb +5 -69
  22. data/lib/composite_primary_keys/composite_arrays.rb +8 -51
  23. data/lib/composite_primary_keys/composite_predicates.rb +7 -16
  24. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
  25. data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +42 -11
  26. data/lib/composite_primary_keys/core.rb +46 -45
  27. data/lib/composite_primary_keys/dirty.rb +19 -19
  28. data/lib/composite_primary_keys/fixtures.rb +19 -17
  29. data/lib/composite_primary_keys/locking/optimistic.rb +48 -44
  30. data/lib/composite_primary_keys/nested_attributes.rb +64 -53
  31. data/lib/composite_primary_keys/persistence.rb +49 -41
  32. data/lib/composite_primary_keys/relation.rb +22 -47
  33. data/lib/composite_primary_keys/relation/batches.rb +33 -30
  34. data/lib/composite_primary_keys/relation/calculations.rb +3 -7
  35. data/lib/composite_primary_keys/relation/finder_methods.rb +123 -56
  36. data/lib/composite_primary_keys/relation/predicate_builder.rb +18 -29
  37. data/lib/composite_primary_keys/relation/where_clause.rb +33 -0
  38. data/lib/composite_primary_keys/sanitization.rb +45 -38
  39. data/lib/composite_primary_keys/validations/uniqueness.rb +37 -37
  40. data/lib/composite_primary_keys/version.rb +4 -4
  41. data/scripts/console.rb +48 -48
  42. data/scripts/txt2html +76 -76
  43. data/scripts/txt2js +65 -65
  44. data/tasks/databases/mysql.rake +42 -42
  45. data/tasks/databases/postgresql.rake +47 -47
  46. data/tasks/databases/sqlite3.rake +27 -27
  47. data/tasks/website.rake +18 -18
  48. data/test/README_tests.rdoc +56 -56
  49. data/test/abstract_unit.rb +10 -9
  50. data/test/connections/connection_spec.rb +18 -18
  51. data/test/connections/databases.yml +9 -39
  52. data/test/connections/native_ibm_db/connection.rb +18 -18
  53. data/test/connections/native_mysql/connection.rb +17 -17
  54. data/test/connections/native_postgresql/connection.rb +12 -12
  55. data/test/connections/native_sqlite3/connection.rb +9 -9
  56. data/test/db_test.rb +52 -52
  57. data/test/fixtures/article.rb +5 -5
  58. data/test/fixtures/articles.yml +6 -6
  59. data/test/fixtures/capitol.rb +3 -3
  60. data/test/fixtures/capitols.yml +16 -16
  61. data/test/fixtures/comments.yml +15 -15
  62. data/test/fixtures/db_definitions/mysql.sql +2 -12
  63. data/test/fixtures/db_definitions/oracle.sql +1 -2
  64. data/test/fixtures/db_definitions/postgresql.sql +0 -10
  65. data/test/fixtures/db_definitions/sqlite.sql +0 -9
  66. data/test/fixtures/db_definitions/sqlserver.sql +1 -2
  67. data/test/fixtures/department.rb +5 -5
  68. data/test/fixtures/departments.yml +15 -15
  69. data/test/fixtures/dorms.yml +4 -4
  70. data/test/fixtures/employee.rb +1 -2
  71. data/test/fixtures/employees.yml +19 -23
  72. data/test/fixtures/group.rb +2 -2
  73. data/test/fixtures/groups.yml +6 -6
  74. data/test/fixtures/hack.rb +4 -4
  75. data/test/fixtures/hacks.yml +2 -2
  76. data/test/fixtures/membership_status.rb +2 -2
  77. data/test/fixtures/product.rb +9 -9
  78. data/test/fixtures/product_tariff.rb +5 -5
  79. data/test/fixtures/products.yml +11 -11
  80. data/test/fixtures/reading.rb +4 -4
  81. data/test/fixtures/readings.yml +10 -10
  82. data/test/fixtures/reference_code_using_composite_key_alias.rb +8 -8
  83. data/test/fixtures/reference_code_using_simple_key_alias.rb +8 -8
  84. data/test/fixtures/reference_codes.yml +28 -28
  85. data/test/fixtures/reference_type.rb +1 -1
  86. data/test/fixtures/reference_types.yml +9 -9
  87. data/test/fixtures/restaurant.rb +9 -9
  88. data/test/fixtures/restaurants.yml +14 -14
  89. data/test/fixtures/restaurants_suburbs.yml +10 -10
  90. data/test/fixtures/room.rb +11 -11
  91. data/test/fixtures/room_assignment.rb +13 -13
  92. data/test/fixtures/room_assignments.yml +24 -24
  93. data/test/fixtures/room_attribute.rb +2 -2
  94. data/test/fixtures/room_attribute_assignment.rb +4 -4
  95. data/test/fixtures/room_attribute_assignments.yml +4 -4
  96. data/test/fixtures/room_attributes.yml +2 -2
  97. data/test/fixtures/rooms.yml +12 -12
  98. data/test/fixtures/seat.rb +5 -5
  99. data/test/fixtures/seats.yml +8 -8
  100. data/test/fixtures/street.rb +2 -2
  101. data/test/fixtures/streets.yml +16 -16
  102. data/test/fixtures/student.rb +3 -3
  103. data/test/fixtures/students.yml +15 -15
  104. data/test/fixtures/suburbs.yml +14 -14
  105. data/test/fixtures/tariff.rb +5 -5
  106. data/test/fixtures/tariffs.yml +14 -14
  107. data/test/fixtures/user.rb +0 -1
  108. data/test/plugins/pagination.rb +405 -405
  109. data/test/plugins/pagination_helper.rb +135 -135
  110. data/test/setup.rb +50 -50
  111. data/test/test_aliases.rb +18 -18
  112. data/test/test_associations.rb +7 -18
  113. data/test/test_composite_arrays.rb +24 -38
  114. data/test/test_counter_cache.rb +30 -30
  115. data/test/test_create.rb +5 -5
  116. data/test/test_delete_all.rb +7 -13
  117. data/test/test_dup.rb +37 -37
  118. data/test/test_exists.rb +39 -39
  119. data/test/test_find.rb +16 -12
  120. data/test/test_habtm.rb +26 -2
  121. data/test/test_ids.rb +109 -116
  122. data/test/test_miscellaneous.rb +32 -32
  123. data/test/test_pagination.rb +35 -35
  124. data/test/test_polymorphic.rb +0 -7
  125. data/test/test_predicates.rb +9 -28
  126. data/test/test_update.rb +3 -5
  127. data/test/test_validations.rb +13 -13
  128. metadata +24 -32
  129. data/lib/composite_primary_keys/arel/visitors/to_sql.rb +0 -36
  130. data/lib/composite_primary_keys/attribute_methods/dirty.rb +0 -29
  131. data/lib/composite_primary_keys/autosave_association.rb +0 -67
  132. data/lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb +0 -23
  133. data/test/fixtures/pk_called_id.rb +0 -5
  134. data/test/fixtures/pk_called_ids.yml +0 -11
  135. data/test/test_find_in_batches.rb +0 -30
  136. data/test/test_update_all.rb +0 -17
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 98777241ea29aef880ba2b53631fb47686751a898df5b7200cd64a06fb84b838
4
- data.tar.gz: 0f5d62c48beab81bcc7a31dd9a025af2867eeb8821482e2aabc0933ae10b95e5
2
+ SHA1:
3
+ metadata.gz: 2e62c2248e714242837c9bd00862bd0411c02f00
4
+ data.tar.gz: 587c5f4beabb743d14fd3d7febb134d2fdfa3ab5
5
5
  SHA512:
6
- metadata.gz: 498c215b18b6056347095a00e81edf399cb0a684c8130413fbb0db87181451bf0801d9c5446b2d34d707137df0e984267d79a9f432accf0287003719a1e229b3
7
- data.tar.gz: 1ed759331f302c751f4f93899d3d95945c8c768cd279f817b6431add9c7d97e5caa6b47f8500b8871fa6993a145b288fc6e56f7067ab2f5c479956a10e303d71
6
+ metadata.gz: 82894133f71054b13e6fa8293e968fff35732cddd7b7856749d9b67b40a81798130f66974acb6561e137899feb591119cab35657b4b98eaf892a12a938dd5ccf
7
+ data.tar.gz: d5605252ae0c73bef65b92993562032808e86d6ba192eda0e8c5f12681d9465679ab947b2a76b8a93e8423ef1ae673ce0611f66275d16611e7af9ceb4c8de03e
@@ -1,28 +1,5 @@
1
- == 8.1.8 (2020-01-01)
2
- Fix handling CPK with fields containing comma, #505 (Sergey Semyonov)
3
-
4
- == 8.1.7 (2019-05-04)
5
-
6
- Fix key not being an array in subquery on has_many :through (Eric Proulx)
7
- Convert batches to arrays before continuing loop (Jeremy Mickelson)
8
-
9
- == 8.1.6 (2017-05-20)
10
-
11
- Query cache with bind params (ttw)
12
- Fix TestIds#test_set_ids_string. Sqlite's serial and integer are a bit different (Boris Peterbarg)
13
- Associations with inverse fix (Boris Peterbarg)
14
- Fix save_has_one_association and add a test for it (Boris Peterbarg)
15
- Fix save_belongs_to_association override (Boris Peterbarg)
16
- Extend approach from #344 to fix autosave for has_one associations as well (Cameron Finucane)
17
-
18
- == 8.1.5 (2017-01-01)
19
-
20
- * Don't nest PK twice when looking up id, fixes #319 (Kerey Roper)
21
-
22
- == 8.1.4 (2016-07-27)
23
-
24
- * Create OR predicates in a nicely balanced tree fixing #320 (Nathan Samson)
25
- * Fix find in batches (apurvis)
1
+ == 9.0.0.beta1 (2015-08-04)
2
+ * Rails 5 support (Sammy Larbi)
26
3
 
27
4
  == 8.1.3 (2016-04-16)
28
5
 
@@ -46,6 +23,7 @@ Extend approach from #344 to fix autosave for has_one associations as well (Came
46
23
  * Remove call to verify_active_connections! which was removed from AR 4.1 (Steve Pletcher)
47
24
  * Aligned the establish connection paramater handling to be similar to latest version of rails (Harish Shetty)
48
25
 
26
+
49
27
  == 8.1.0 (2014-03-23)
50
28
 
51
29
  * ActiveRecord 4.2.1 support (Charlie Savage)
@@ -20,6 +20,7 @@ Every major version of ActiveRecord has included numerous internal changes. As
20
20
  CPK has to be rewritten for each version of ActiveRecord. To help keep
21
21
  things straight, here is the mapping:
22
22
 
23
+ Version 9.x is designed to work with ActiveRecord 5.0.x
23
24
  Version 8.x is designed to work with ActiveRecord 4.2.x
24
25
  Version 7.x is designed to work with ActiveRecord 4.1.x
25
26
  Version 6.x is designed to work with ActiveRecord 4.0.x
@@ -1,33 +1,33 @@
1
- Composite Primary key support for db2
2
-
3
- == Driver Support
4
-
5
- DB2 support requires the IBM_DB driver provided by http://rubyforge.org/projects/rubyibm/
6
- project. Install using gem install ibm_db. Tested against version 0.60 of the driver.
7
- This rubyforge project appears to be permenant location for the IBM adapter.
8
- Older versions of the driver available from IBM Alphaworks will not work.
9
-
10
- == Driver Bug and workaround provided as part of this plugin
11
-
12
- Unlike the basic quote routine available for Rails AR, the DB2 adapter's quote
13
- method doesn't return " column_name = 1 " when string values (integers in string type variable)
14
- are passed for quoting numeric column. Rather it returns "column_name = '1'.
15
- DB2 doesn't accept single quoting numeric columns in SQL. Currently, as part of
16
- this plugin a fix is provided for the DB2 adapter since this plugin does
17
- pass string values like this. Perhaps a patch should be sent to the DB2 adapter
18
- project for a permanant fix.
19
-
20
- == Database Setup
21
-
22
- Database must be manually created using a separate command. Read the rake task
23
- for creating tables and change the db name, user and passwords accordingly.
24
-
25
- == Tested Database Server version
26
-
27
- This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
28
-
29
- == Tested Database Client version
30
-
31
- This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
32
-
33
-
1
+ Composite Primary key support for db2
2
+
3
+ == Driver Support
4
+
5
+ DB2 support requires the IBM_DB driver provided by http://rubyforge.org/projects/rubyibm/
6
+ project. Install using gem install ibm_db. Tested against version 0.60 of the driver.
7
+ This rubyforge project appears to be permenant location for the IBM adapter.
8
+ Older versions of the driver available from IBM Alphaworks will not work.
9
+
10
+ == Driver Bug and workaround provided as part of this plugin
11
+
12
+ Unlike the basic quote routine available for Rails AR, the DB2 adapter's quote
13
+ method doesn't return " column_name = 1 " when string values (integers in string type variable)
14
+ are passed for quoting numeric column. Rather it returns "column_name = '1'.
15
+ DB2 doesn't accept single quoting numeric columns in SQL. Currently, as part of
16
+ this plugin a fix is provided for the DB2 adapter since this plugin does
17
+ pass string values like this. Perhaps a patch should be sent to the DB2 adapter
18
+ project for a permanant fix.
19
+
20
+ == Database Setup
21
+
22
+ Database must be manually created using a separate command. Read the rake task
23
+ for creating tables and change the db name, user and passwords accordingly.
24
+
25
+ == Tested Database Server version
26
+
27
+ This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
28
+
29
+ == Tested Database Client version
30
+
31
+ This is tested against DB2 v9.1 in Ubuntu Feisty Fawn (7.04)
32
+
33
+
data/Rakefile CHANGED
@@ -1,34 +1,34 @@
1
- require 'rubygems'
2
- require 'rake'
3
- require 'rake/clean'
4
- require 'rake/testtask'
5
- require 'rubygems/package_task'
6
-
7
- # Set global variable so other tasks can access them
8
- ::PROJECT_ROOT = File.expand_path(".")
9
- ::GEM_NAME = 'composite_primary_keys'
10
-
11
- # Read the spec file
12
- spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
13
-
14
- # Setup Rake tasks for managing the gem
15
- Gem::PackageTask.new(spec).define
16
-
17
- # Now load in other task files
18
- Dir.glob('tasks/**/*.rake').each do |rake_file|
19
- load File.join(File.dirname(__FILE__), rake_file)
20
- end
21
-
22
- # Set up test tasks for each supported connection adapter
23
- %w(mysql sqlite3 oracle oracle_enhanced postgresql ibm_db sqlserver).each do |adapter|
24
- namespace adapter do
25
- desc "Run tests using the #{adapter} adapter"
26
- task "test" do
27
- ENV["ADAPTER"] = adapter
28
- Rake::TestTask.new("subtest_#{adapter}") do |t|
29
- t.libs << "test"
30
- end
31
- Rake::Task["subtest_#{adapter}"].invoke
32
- end
33
- end
34
- end
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rubygems/package_task'
6
+
7
+ # Set global variable so other tasks can access them
8
+ ::PROJECT_ROOT = File.expand_path(".")
9
+ ::GEM_NAME = 'composite_primary_keys'
10
+
11
+ # Read the spec file
12
+ spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
13
+
14
+ # Setup Rake tasks for managing the gem
15
+ Gem::PackageTask.new(spec).define
16
+
17
+ # Now load in other task files
18
+ Dir.glob('tasks/**/*.rake').each do |rake_file|
19
+ load File.join(File.dirname(__FILE__), rake_file)
20
+ end
21
+
22
+ # Set up test tasks for each supported connection adapter
23
+ %w(mysql sqlite3 oracle oracle_enhanced postgresql ibm_db sqlserver).each do |adapter|
24
+ namespace adapter do
25
+ desc "Run tests using the #{adapter} adapter"
26
+ task "test" do
27
+ ENV["ADAPTER"] = adapter
28
+ Rake::TestTask.new("subtest_#{adapter}") do |t|
29
+ t.libs << "test"
30
+ end
31
+ Rake::Task["subtest_#{adapter}"].invoke
32
+ end
33
+ end
34
+ end
@@ -26,13 +26,10 @@ $:.unshift(File.dirname(__FILE__)) unless
26
26
 
27
27
  unless defined?(ActiveRecord)
28
28
  require 'rubygems'
29
- gem 'activerecord', '~>4.2.0'
29
+ gem 'activerecord', '~>5.0.0.beta3'
30
30
  require 'active_record'
31
31
  end
32
32
 
33
- # Arel files we override
34
- require 'arel/visitors/to_sql'
35
-
36
33
  # AR files we override
37
34
  require 'active_record/counter_cache'
38
35
  require 'active_record/fixtures'
@@ -61,19 +58,17 @@ require 'active_record/locking/optimistic'
61
58
  require 'active_record/nested_attributes'
62
59
 
63
60
  require 'active_record/connection_adapters/abstract_adapter'
64
- require 'active_record/connection_adapters/abstract_mysql_adapter'
65
61
 
66
62
  require 'active_record/relation/batches'
63
+ require 'active_record/relation/where_clause'
67
64
  require 'active_record/relation/calculations'
68
65
  require 'active_record/relation/finder_methods'
69
66
  require 'active_record/relation/predicate_builder'
70
67
  require 'active_record/relation/query_methods'
71
68
 
72
- require 'active_record/validations/uniqueness'
69
+ require 'active_record/validations/uniqueness' unless ENV["TESTING_CPK"] == "true"
73
70
 
74
71
  # CPK files
75
- require 'composite_primary_keys/arel/visitors/to_sql'
76
-
77
72
  require 'composite_primary_keys/persistence'
78
73
  require 'composite_primary_keys/base'
79
74
  require 'composite_primary_keys/core'
@@ -98,20 +93,18 @@ require 'composite_primary_keys/associations/singular_association'
98
93
  require 'composite_primary_keys/associations/collection_association'
99
94
 
100
95
  require 'composite_primary_keys/dirty'
101
- require 'composite_primary_keys/autosave_association'
102
96
 
103
97
  require 'composite_primary_keys/attribute_methods/primary_key'
104
- require 'composite_primary_keys/attribute_methods/dirty'
105
98
  require 'composite_primary_keys/attribute_methods/read'
106
99
  require 'composite_primary_keys/attribute_methods/write'
107
100
  require 'composite_primary_keys/locking/optimistic'
108
101
  require 'composite_primary_keys/nested_attributes'
109
102
 
110
103
  require 'composite_primary_keys/connection_adapters/abstract_adapter'
111
- require 'composite_primary_keys/connection_adapters/abstract_mysql_adapter'
112
104
  require 'composite_primary_keys/connection_adapters/abstract/connection_specification_changes'
113
105
 
114
106
  require 'composite_primary_keys/relation/batches'
107
+ require 'composite_primary_keys/relation/where_clause'
115
108
  require 'composite_primary_keys/relation/calculations'
116
109
  require 'composite_primary_keys/relation/finder_methods'
117
110
  require 'composite_primary_keys/relation/predicate_builder'
@@ -1,22 +1,24 @@
1
1
  module ActiveRecord
2
2
  module Associations
3
3
  class Association
4
- def creation_attributes
5
- attributes = {}
4
+ silence_warnings do
5
+ def creation_attributes
6
+ attributes = {}
6
7
 
7
- if (reflection.has_one? || reflection.collection?) && !options[:through]
8
- # CPK
9
- # attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key]
10
- Array(reflection.foreign_key).zip(Array(reflection.active_record_primary_key)).each do |key1, key2|
11
- attributes[key1] = owner[key2]
12
- end
8
+ if (reflection.has_one? || reflection.collection?) && !options[:through]
9
+ # CPK
10
+ # attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key]
11
+ Array(reflection.foreign_key).zip(Array(reflection.active_record_primary_key)).each do |key1, key2|
12
+ attributes[key1] = owner[key2]
13
+ end
13
14
 
14
- if reflection.options[:as]
15
- attributes[reflection.type] = owner.class.base_class.name
15
+ if reflection.options[:as]
16
+ attributes[reflection.type] = owner.class.base_class.name
17
+ end
16
18
  end
17
- end
18
19
 
19
- attributes
20
+ attributes
21
+ end
20
22
  end
21
23
  end
22
24
  end
@@ -1,69 +1,42 @@
1
1
  module ActiveRecord
2
2
  module Associations
3
3
  class AssociationScope
4
+ silence_warnings do
5
+ def next_chain_scope(scope, table, reflection, association_klass, foreign_table, next_reflection)
6
+ join_keys = reflection.join_keys(association_klass)
7
+ key = join_keys.key
8
+ foreign_key = join_keys.foreign_key
9
+ # CPK
10
+ # constraint = table[key].eq(foreign_table[foreign_key])
11
+ constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
4
12
 
5
- def self.get_bind_values(owner, chain)
6
- binds = []
7
- last_reflection = chain.last
8
-
9
- # CPK
10
- # binds << last_reflection.join_id_for(owner)
11
- values = last_reflection.join_id_for(owner)
12
- binds += Array(values)
13
-
14
- if last_reflection.type
15
- binds << owner.class.base_class.name
16
- end
17
-
18
- chain.each_cons(2).each do |reflection, next_reflection|
19
13
  if reflection.type
20
- binds << next_reflection.klass.base_class.name
14
+ value = transform_value(next_reflection.klass.base_class.name)
15
+ scope = scope.where(table.name => { reflection.type => value })
21
16
  end
22
- end
23
- binds
24
- end
25
-
26
- def next_chain_scope(scope, table, reflection, tracker, assoc_klass, foreign_table, next_reflection)
27
- join_keys = reflection.join_keys(assoc_klass)
28
- key = join_keys.key
29
- foreign_key = join_keys.foreign_key
30
17
 
31
- # CPK
32
- # constraint = table[key].eq(foreign_table[foreign_key])
33
- constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
34
-
35
- if reflection.type
36
- value = next_reflection.klass.base_class.name
37
- bind_val = bind scope, table.table_name, reflection.type, value, tracker
38
- scope = scope.where(table[reflection.type].eq(bind_val))
18
+ scope = scope.joins(join(foreign_table, constraint))
39
19
  end
40
20
 
41
- scope.joins(join(foreign_table, constraint))
42
- end
43
-
44
- def last_chain_scope(scope, table, reflection, owner, tracker, assoc_klass)
45
- join_keys = reflection.join_keys(assoc_klass)
46
- key = join_keys.key
47
- foreign_key = join_keys.foreign_key
48
-
49
- if key.kind_of?(Array) || foreign_key.kind_of?(Array)
50
- key = Array(key) unless key.kind_of?(Array)
51
- foreign_key = Array(foreign_key) unless foreign_key.kind_of?(Array)
52
- key.zip(foreign_key).map do |k, fk|
53
- bind_val = bind scope, table.table_name, k.to_s, owner[fk], tracker
54
- scope = scope.where(table[k].eq(bind_val))
21
+ def last_chain_scope(scope, table, reflection, owner, association_klass)
22
+ join_keys = reflection.join_keys(association_klass)
23
+ key = join_keys.key
24
+ foreign_key = join_keys.foreign_key
25
+
26
+ # CPK
27
+ if key.kind_of?(Array) || foreign_key.kind_of?(Array)
28
+ predicate = cpk_join_predicate(table, key, owner, foreign_key)
29
+ scope = scope.where(predicate)
30
+ else
31
+ value = transform_value(owner[foreign_key])
32
+ scope = scope.where(table.name => { key => value })
55
33
  end
56
- else
57
- bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key], tracker
58
- scope = scope.where(table[key].eq(bind_val))
59
- end
60
34
 
61
- if reflection.type
62
- value = owner.class.base_class.name
63
- bind_val = bind scope, table.table_name, reflection.type, value, tracker
35
+ if reflection.type
36
+ polymorphic_type = transform_value(owner.class.base_class.name)
37
+ scope = scope.where(table.name => { reflection.type => polymorphic_type })
38
+ end
64
39
 
65
- scope.where(table[reflection.type].eq(bind_val))
66
- else
67
40
  scope
68
41
  end
69
42
  end
@@ -1,15 +1,29 @@
1
1
  module CompositePrimaryKeys
2
2
  module CollectionAssociation
3
- extend ActiveSupport::Concern
4
- included do
5
- def get_records_with_cpk_support
6
- cpk_applies = (target && target.respond_to?(:composite?) && target.composite?) || (owner && owner.respond_to?(:composite?) && owner.composite?)
7
- return scope.to_a if cpk_applies
8
- get_records_without_cpk_support
3
+ def get_records
4
+ cpk_applies = target.try(:composite?) || owner.try(:composite?)
5
+ return scope.to_a if cpk_applies
6
+ super
7
+ end
8
+
9
+ def ids_writer(ids)
10
+ pk_type = reflection.primary_key_type
11
+ ids = Array(ids).reject(&:blank?)
12
+ ids.map! { |i| pk_type.cast(i) }
13
+ # CPK
14
+ if reflection.association_primary_key.is_a?(Array)
15
+ predicate = Class.new.extend(CompositePrimaryKeys::Predicates).cpk_in_predicate(klass.arel_table, reflection.association_primary_key, ids)
16
+ records = klass.where(predicate).index_by do |r|
17
+ reflection.association_primary_key.map{ |k| r.send(k) }
18
+ end.values_at(*ids)
19
+ else
20
+ records = klass.where(reflection.association_primary_key => ids).index_by do |r|
21
+ r.send(reflection.association_primary_key)
22
+ end.values_at(*ids)
9
23
  end
10
- alias_method_chain :get_records, :cpk_support
24
+ replace(records)
11
25
  end
12
26
  end
13
27
  end
14
28
 
15
- ActiveRecord::Associations::CollectionAssociation.send(:include, CompositePrimaryKeys::CollectionAssociation)
29
+ ActiveRecord::Associations::CollectionAssociation.prepend CompositePrimaryKeys::CollectionAssociation
@@ -1,67 +1,29 @@
1
1
  module ActiveRecord
2
2
  module Associations
3
3
  class HasManyAssociation
4
- def delete_count(method, scope)
5
- if method == :delete_all
6
- scope.delete_all
7
- else
8
- # CPK
9
- # scope.update_all(reflection.foreign_key => nil)
10
- conds = Array(reflection.foreign_key).inject(Hash.new) do |mem, key|
11
- mem[key] = nil
12
- mem
13
- end
14
- scope.update_all(conds)
15
- end
16
- end
17
-
18
- def delete_records(records, method)
19
- if method == :destroy
20
- records.each(&:destroy!)
21
- update_counter(-records.length) unless inverse_updates_counter_cache?
22
- else
23
- if records == :all || !reflection.klass.primary_key
24
- scope = self.scope
25
- else
26
- # CPK
27
- # scope = self.scope.where(reflection.klass.primary_key => records)
28
- table = Arel::Table.new(reflection.table_name)
29
- and_conditions = records.map do |record|
30
- eq_conditions = Array(reflection.association_primary_key).map do |name|
31
- table[name].eq(record[name])
32
- end
33
- Arel::Nodes::And.new(eq_conditions)
34
- end
35
-
36
- condition = and_conditions.shift
37
- and_conditions.each do |and_condition|
38
- condition = condition.or(and_condition)
39
- end
40
-
41
- scope = self.scope.where(condition)
42
- end
43
-
4
+ silence_warnings do
5
+ def delete_count(method, scope)
44
6
  if method == :delete_all
45
- update_counter(-scope.delete_all)
7
+ scope.delete_all
46
8
  else
47
9
  # CPK
48
- # update_counter(-scope.update_all(reflection.foreign_key => nil))
49
- update_hash = Array(reflection.foreign_key).inject(Hash.new) do |hash, key|
50
- hash[key] = nil
51
- hash
10
+ # scope.update_all(reflection.foreign_key => nil)
11
+ conds = Array(reflection.foreign_key).inject(Hash.new) do |mem, key|
12
+ mem[key] = nil
13
+ mem
52
14
  end
53
- update_counter(-scope.update_all(update_hash))
15
+ scope.update_all(conds)
54
16
  end
55
17
  end
56
- end
57
18
 
58
- def foreign_key_present?
59
- if reflection.klass.primary_key
60
- # CPK
61
- # owner.attribute_present?(reflection.association_primary_key)
62
- Array(reflection.klass.primary_key).all? {|key| owner.attribute_present?(key)}
63
- else
64
- false
19
+ def foreign_key_present?
20
+ if reflection.klass.primary_key
21
+ # CPK
22
+ # owner.attribute_present?(reflection.association_primary_key)
23
+ Array(reflection.klass.primary_key).all? {|key| owner.attribute_present?(key)}
24
+ else
25
+ false
26
+ end
65
27
  end
66
28
  end
67
29
  end