composite_primary_keys 7.0.16 → 8.0.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 (95) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +600 -623
  3. data/lib/composite_primary_keys.rb +113 -115
  4. data/lib/composite_primary_keys/associations/association.rb +23 -23
  5. data/lib/composite_primary_keys/associations/association_scope.rb +73 -77
  6. data/lib/composite_primary_keys/associations/collection_association.rb +15 -0
  7. data/lib/composite_primary_keys/associations/has_many_association.rb +69 -56
  8. data/lib/composite_primary_keys/associations/has_many_through_association.rb +30 -28
  9. data/lib/composite_primary_keys/associations/join_dependency.rb +87 -89
  10. data/lib/composite_primary_keys/associations/join_dependency/join_association.rb +22 -22
  11. data/lib/composite_primary_keys/associations/preloader/association.rb +90 -78
  12. data/lib/composite_primary_keys/associations/preloader/belongs_to.rb +19 -19
  13. data/lib/composite_primary_keys/associations/singular_association.rb +15 -0
  14. data/lib/composite_primary_keys/attribute_methods.rb +9 -0
  15. data/lib/composite_primary_keys/attribute_methods/dirty.rb +29 -26
  16. data/lib/composite_primary_keys/attribute_methods/read.rb +19 -34
  17. data/lib/composite_primary_keys/attribute_methods/write.rb +30 -36
  18. data/lib/composite_primary_keys/attribute_set/builder.rb +20 -0
  19. data/lib/composite_primary_keys/base.rb +135 -129
  20. data/lib/composite_primary_keys/composite_arrays.rb +30 -30
  21. data/lib/composite_primary_keys/composite_predicates.rb +50 -50
  22. data/lib/composite_primary_keys/composite_relation.rb +48 -48
  23. data/lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb +2 -4
  24. data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +46 -60
  25. data/lib/composite_primary_keys/core.rb +69 -47
  26. data/lib/composite_primary_keys/fixtures.rb +22 -22
  27. data/lib/composite_primary_keys/persistence.rb +56 -60
  28. data/lib/composite_primary_keys/relation.rb +68 -56
  29. data/lib/composite_primary_keys/relation/calculations.rb +79 -75
  30. data/lib/composite_primary_keys/relation/finder_methods.rb +175 -196
  31. data/lib/composite_primary_keys/relation/query_methods.rb +40 -40
  32. data/lib/composite_primary_keys/sanitization.rb +52 -52
  33. data/lib/composite_primary_keys/validations/uniqueness.rb +36 -37
  34. data/lib/composite_primary_keys/version.rb +8 -8
  35. data/tasks/databases/oracle.rake +25 -25
  36. data/tasks/databases/sqlserver.rake +27 -40
  37. data/test/abstract_unit.rb +113 -113
  38. data/test/connections/databases.ci.yml +15 -15
  39. data/test/connections/databases.example.yml +18 -18
  40. data/test/connections/native_oracle/connection.rb +11 -11
  41. data/test/connections/native_oracle_enhanced/connection.rb +16 -16
  42. data/test/connections/native_sqlserver/connection.rb +11 -14
  43. data/test/fixtures/comment.rb +7 -7
  44. data/test/fixtures/db_definitions/db2-create-tables.sql +125 -126
  45. data/test/fixtures/db_definitions/db2-drop-tables.sql +18 -18
  46. data/test/fixtures/db_definitions/mysql.sql +207 -208
  47. data/test/fixtures/db_definitions/oracle.drop.sql +45 -45
  48. data/test/fixtures/db_definitions/oracle.sql +222 -223
  49. data/test/fixtures/db_definitions/postgresql.sql +209 -210
  50. data/test/fixtures/db_definitions/sqlite.sql +196 -197
  51. data/test/fixtures/db_definitions/sqlserver.drop.sql +91 -94
  52. data/test/fixtures/db_definitions/sqlserver.sql +225 -232
  53. data/test/fixtures/dorm.rb +2 -2
  54. data/test/fixtures/employee.rb +5 -5
  55. data/test/fixtures/membership.rb +6 -6
  56. data/test/fixtures/membership_statuses.yml +16 -16
  57. data/test/fixtures/memberships.yml +10 -10
  58. data/test/fixtures/product_tariffs.yml +14 -14
  59. data/test/fixtures/reference_code.rb +7 -7
  60. data/test/fixtures/restaurants_suburb.rb +2 -2
  61. data/test/fixtures/suburb.rb +5 -5
  62. data/test/fixtures/topic.rb +5 -5
  63. data/test/fixtures/topic_source.rb +6 -6
  64. data/test/fixtures/topic_sources.yml +3 -3
  65. data/test/fixtures/topics.yml +8 -8
  66. data/test/fixtures/users.yml +10 -10
  67. data/test/test_associations.rb +295 -275
  68. data/test/test_attribute_methods.rb +63 -63
  69. data/test/test_attributes.rb +60 -60
  70. data/test/test_calculations.rb +37 -42
  71. data/test/test_callbacks.rb +99 -99
  72. data/test/test_create.rb +112 -112
  73. data/test/test_delete.rb +148 -152
  74. data/test/test_delete_all.rb +28 -26
  75. data/test/test_dumpable.rb +15 -15
  76. data/test/test_enum.rb +21 -20
  77. data/test/test_equal.rb +26 -26
  78. data/test/test_find.rb +118 -118
  79. data/test/test_habtm.rb +113 -113
  80. data/test/test_nested_attributes.rb +124 -124
  81. data/test/test_polymorphic.rb +26 -26
  82. data/test/test_predicates.rb +40 -40
  83. data/test/test_santiago.rb +23 -23
  84. data/test/test_suite.rb +33 -34
  85. data/test/test_touch.rb +23 -23
  86. data/test/test_tutorial_example.rb +21 -21
  87. data/test/test_update.rb +71 -71
  88. metadata +9 -13
  89. data/lib/composite_primary_keys/arel/visitors/to_sql.rb +0 -20
  90. data/lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb +0 -59
  91. data/lib/composite_primary_keys/associations/join_dependency/join_part.rb +0 -39
  92. data/lib/composite_primary_keys/associations/preloader/has_and_belongs_to_many.rb +0 -46
  93. data/lib/composite_primary_keys/connection_adapters/sqlserver_adapter.rb +0 -17
  94. data/lib/composite_primary_keys/locking/optimistic.rb +0 -55
  95. data/test/test_optimistic.rb +0 -18
@@ -1,115 +1,113 @@
1
- #--
2
- # Copyright (c) 2006-2012 Nic Williams and Charlie Savage
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining
5
- # a copy of this software and associated documentation files (the
6
- # "Software"), to deal in the Software without restriction, including
7
- # without limitation the rights to use, copy, modify, merge, publish,
8
- # distribute, sublicense, and/or sell copies of the Software, and to
9
- # permit persons to whom the Software is furnished to do so, subject to
10
- # the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- #++
23
-
24
- $:.unshift(File.dirname(__FILE__)) unless
25
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
26
-
27
- unless defined?(ActiveRecord)
28
- require 'rubygems'
29
- gem 'activerecord', '~>4.1.7'
30
- require 'active_record'
31
- end
32
-
33
- # Arel files we override
34
- require 'arel/visitors/to_sql'
35
-
36
- # AR files we override
37
- require 'active_record/counter_cache'
38
- require 'active_record/fixtures'
39
- require 'active_record/model_schema'
40
- require 'active_record/persistence'
41
- require 'active_record/relation'
42
- require 'active_record/sanitization'
43
-
44
- require 'active_record/associations/association'
45
- require 'active_record/associations/association_scope'
46
- require 'active_record/associations/has_many_association'
47
- require 'active_record/associations/has_many_through_association'
48
- require 'active_record/associations/join_dependency'
49
- require 'active_record/associations/join_dependency/join_part'
50
- require 'active_record/associations/join_dependency/join_association'
51
- require 'active_record/associations/preloader/association'
52
- require 'active_record/associations/preloader/belongs_to'
53
-
54
- require 'active_record/attribute_methods/primary_key'
55
- require 'active_record/attribute_methods/dirty'
56
- require 'active_record/attribute_methods/read'
57
- require 'active_record/attribute_methods/write'
58
- require 'active_record/locking/optimistic'
59
- require 'active_record/nested_attributes'
60
-
61
- require 'active_record/connection_adapters/abstract_adapter'
62
-
63
- require 'active_record/relation/batches'
64
- require 'active_record/relation/calculations'
65
- require 'active_record/relation/finder_methods'
66
- require 'active_record/relation/predicate_builder'
67
- require 'active_record/relation/query_methods'
68
-
69
- require 'active_record/validations/uniqueness'
70
-
71
- # CPK files
72
- require 'composite_primary_keys/arel/visitors/to_sql'
73
-
74
- require 'composite_primary_keys/persistence'
75
- require 'composite_primary_keys/base'
76
- require 'composite_primary_keys/core'
77
- require 'composite_primary_keys/composite_arrays'
78
- require 'composite_primary_keys/composite_predicates'
79
- require 'composite_primary_keys/fixtures'
80
- require 'composite_primary_keys/model_schema'
81
- require 'composite_primary_keys/relation'
82
- require 'composite_primary_keys/sanitization'
83
- require 'composite_primary_keys/version'
84
-
85
- require 'composite_primary_keys/associations/association'
86
- require 'composite_primary_keys/associations/association_scope'
87
- require 'composite_primary_keys/associations/has_many_association'
88
- require 'composite_primary_keys/associations/has_many_through_association'
89
- require 'composite_primary_keys/associations/join_dependency'
90
- require 'composite_primary_keys/associations/join_dependency/join_part'
91
- require 'composite_primary_keys/associations/join_dependency/join_association'
92
- require 'composite_primary_keys/associations/preloader/association'
93
- require 'composite_primary_keys/associations/preloader/belongs_to'
94
-
95
- require 'composite_primary_keys/dirty'
96
-
97
- require 'composite_primary_keys/attribute_methods/primary_key'
98
- require 'composite_primary_keys/attribute_methods/dirty'
99
- require 'composite_primary_keys/attribute_methods/read'
100
- require 'composite_primary_keys/attribute_methods/write'
101
- require 'composite_primary_keys/locking/optimistic'
102
- require 'composite_primary_keys/nested_attributes'
103
-
104
- require 'composite_primary_keys/connection_adapters/abstract_adapter'
105
- require 'composite_primary_keys/connection_adapters/abstract/connection_specification_changes'
106
-
107
- require 'composite_primary_keys/relation/batches'
108
- require 'composite_primary_keys/relation/calculations'
109
- require 'composite_primary_keys/relation/finder_methods'
110
- require 'composite_primary_keys/relation/predicate_builder'
111
- require 'composite_primary_keys/relation/query_methods'
112
-
113
- require 'composite_primary_keys/validations/uniqueness'
114
-
115
- require 'composite_primary_keys/composite_relation'
1
+ #--
2
+ # Copyright (c) 2006-2012 Nic Williams and Charlie Savage
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ $:.unshift(File.dirname(__FILE__)) unless
25
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
26
+
27
+ unless defined?(ActiveRecord)
28
+ require 'rubygems'
29
+ gem 'activerecord', '4.2.0'
30
+ require 'active_record'
31
+ end
32
+
33
+ # AR files we override
34
+ require 'active_record/counter_cache'
35
+ require 'active_record/fixtures'
36
+ require 'active_record/model_schema'
37
+ require 'active_record/persistence'
38
+ require 'active_record/relation'
39
+ require 'active_record/sanitization'
40
+ require 'active_record/attribute_methods'
41
+
42
+ require 'active_record/associations/association'
43
+ require 'active_record/associations/association_scope'
44
+ require 'active_record/associations/has_many_association'
45
+ require 'active_record/associations/has_many_through_association'
46
+ require 'active_record/associations/join_dependency'
47
+ require 'active_record/associations/join_dependency/join_association'
48
+ require 'active_record/associations/preloader/association'
49
+ require 'active_record/associations/preloader/belongs_to'
50
+ require 'active_record/associations/singular_association'
51
+ require 'active_record/associations/collection_association'
52
+
53
+ require 'active_record/attribute_set/builder'
54
+ require 'active_record/attribute_methods/primary_key'
55
+ require 'active_record/attribute_methods/read'
56
+ require 'active_record/attribute_methods/write'
57
+ require 'active_record/nested_attributes'
58
+
59
+ require 'active_record/connection_adapters/abstract_adapter'
60
+
61
+ require 'active_record/relation/batches'
62
+ require 'active_record/relation/calculations'
63
+ require 'active_record/relation/finder_methods'
64
+ require 'active_record/relation/predicate_builder'
65
+ require 'active_record/relation/query_methods'
66
+
67
+ require 'active_record/validations/uniqueness'
68
+
69
+ # CPK files
70
+ require 'composite_primary_keys/persistence'
71
+ require 'composite_primary_keys/base'
72
+ require 'composite_primary_keys/core'
73
+ require 'composite_primary_keys/composite_arrays'
74
+ require 'composite_primary_keys/composite_predicates'
75
+ require 'composite_primary_keys/fixtures'
76
+ require 'composite_primary_keys/model_schema'
77
+ require 'composite_primary_keys/relation'
78
+ require 'composite_primary_keys/sanitization'
79
+ require 'composite_primary_keys/attribute_set/builder'
80
+ require 'composite_primary_keys/attribute_methods'
81
+ require 'composite_primary_keys/version'
82
+
83
+ require 'composite_primary_keys/associations/association'
84
+ require 'composite_primary_keys/associations/association_scope'
85
+ require 'composite_primary_keys/associations/has_many_association'
86
+ require 'composite_primary_keys/associations/has_many_through_association'
87
+ require 'composite_primary_keys/associations/join_dependency'
88
+ require 'composite_primary_keys/associations/join_dependency/join_association'
89
+ require 'composite_primary_keys/associations/preloader/association'
90
+ require 'composite_primary_keys/associations/preloader/belongs_to'
91
+ require 'composite_primary_keys/associations/singular_association'
92
+ require 'composite_primary_keys/associations/collection_association'
93
+
94
+ require 'composite_primary_keys/dirty'
95
+
96
+ require 'composite_primary_keys/attribute_methods/primary_key'
97
+ require 'composite_primary_keys/attribute_methods/dirty'
98
+ require 'composite_primary_keys/attribute_methods/read'
99
+ require 'composite_primary_keys/attribute_methods/write'
100
+ require 'composite_primary_keys/nested_attributes'
101
+
102
+ require 'composite_primary_keys/connection_adapters/abstract_adapter'
103
+ require 'composite_primary_keys/connection_adapters/abstract/connection_specification_changes'
104
+
105
+ require 'composite_primary_keys/relation/batches'
106
+ require 'composite_primary_keys/relation/calculations'
107
+ require 'composite_primary_keys/relation/finder_methods'
108
+ require 'composite_primary_keys/relation/predicate_builder'
109
+ require 'composite_primary_keys/relation/query_methods'
110
+
111
+ require 'composite_primary_keys/validations/uniqueness'
112
+
113
+ require 'composite_primary_keys/composite_relation'
@@ -1,23 +1,23 @@
1
- module ActiveRecord
2
- module Associations
3
- class Association
4
- def creation_attributes
5
- attributes = {}
6
-
7
- if (reflection.macro == :has_one || reflection.macro == :has_many) && !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
13
-
14
- if reflection.options[:as]
15
- attributes[reflection.type] = owner.class.base_class.name
16
- end
17
- end
18
-
19
- attributes
20
- end
21
- end
22
- end
23
- end
1
+ module ActiveRecord
2
+ module Associations
3
+ class Association
4
+ def creation_attributes
5
+ attributes = {}
6
+
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
13
+
14
+ if reflection.options[:as]
15
+ attributes[reflection.type] = owner.class.base_class.name
16
+ end
17
+ end
18
+
19
+ attributes
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,77 +1,73 @@
1
- module ActiveRecord
2
- module Associations
3
- class AssociationScope
4
- def add_constraints(scope, owner, assoc_klass, refl, tracker)
5
- chain = refl.chain
6
- scope_chain = refl.scope_chain
7
-
8
- tables = construct_tables(chain, assoc_klass, refl, tracker)
9
-
10
- chain.each_with_index do |reflection, i|
11
- table, foreign_table = tables.shift, tables.first
12
-
13
- if reflection.source_macro == :belongs_to
14
- if reflection.options[:polymorphic]
15
- key = reflection.association_primary_key(assoc_klass)
16
- else
17
- key = reflection.association_primary_key
18
- end
19
-
20
- foreign_key = reflection.foreign_key
21
- else
22
- key = reflection.foreign_key
23
- foreign_key = reflection.active_record_primary_key
24
- end
25
-
26
- if reflection == chain.last
27
- # CPK - TODO add back in tracker support
28
- #bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key], tracker
29
- #scope = scope.where(table[key].eq(bind_val))
30
- predicate = cpk_join_predicate(table, key, owner, foreign_key)
31
- scope = scope.where(predicate)
32
-
33
- if reflection.type
34
- value = owner.class.base_class.name
35
- bind_val = bind scope, table.table_name, reflection.type.to_s, value, tracker
36
- scope = scope.where(table[reflection.type].eq(bind_val))
37
- end
38
- else
39
- # CPK
40
- #constraint = table[key].eq(foreign_table[foreign_key])
41
- constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
42
-
43
- if reflection.type
44
- value = chain[i + 1].klass.base_class.name
45
- bind_val = bind scope, table.table_name, reflection.type.to_s, value, tracker
46
- scope = scope.where(table[reflection.type].eq(bind_val))
47
- end
48
-
49
- scope = scope.joins(join(foreign_table, constraint))
50
- end
51
-
52
- is_first_chain = i == 0
53
- klass = is_first_chain ? assoc_klass : reflection.klass
54
-
55
- # Exclude the scope of the association itself, because that
56
- # was already merged in the #scope method.
57
- scope_chain[i].each do |scope_chain_item|
58
- item = eval_scope(klass, scope_chain_item, owner)
59
-
60
- if scope_chain_item == refl.scope
61
- scope.merge! item.except(:where, :includes, :bind)
62
- end
63
-
64
- if is_first_chain
65
- scope.includes! item.includes_values
66
- end
67
-
68
- scope.where_values += item.where_values
69
- scope.order_values |= item.order_values
70
- end
71
- end
72
-
73
- scope
74
- end
75
- end
76
- end
77
- end
1
+ module ActiveRecord
2
+ module Associations
3
+ class AssociationScope
4
+ def add_constraints(scope, owner, assoc_klass, refl, tracker)
5
+ chain = refl.chain
6
+ scope_chain = refl.scope_chain
7
+
8
+ tables = construct_tables(chain, assoc_klass, refl, tracker)
9
+
10
+ chain.each_with_index do |reflection, i|
11
+ table, foreign_table = tables.shift, tables.first
12
+
13
+ join_keys = reflection.join_keys(assoc_klass)
14
+ key = join_keys.key
15
+ foreign_key = join_keys.foreign_key
16
+
17
+ if reflection == chain.last
18
+ # CPK - TODO add back in tracker support
19
+ if key.kind_of?(Array) || foreign_key.kind_of?(Array)
20
+ predicate = cpk_join_predicate(table, key, owner, foreign_key)
21
+ scope = scope.where(predicate)
22
+ else
23
+ bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key], tracker
24
+ scope = scope.where(table[key].eq(bind_val))
25
+ end
26
+
27
+
28
+ if reflection.type
29
+ value = owner.class.base_class.name
30
+ bind_val = bind scope, table.table_name, reflection.type, value, tracker
31
+ scope = scope.where(table[reflection.type].eq(bind_val))
32
+ end
33
+ else
34
+ # CPK
35
+ #constraint = table[key].eq(foreign_table[foreign_key])
36
+ constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
37
+
38
+ if reflection.type
39
+ value = chain[i + 1].klass.base_class.name
40
+ bind_val = bind scope, table.table_name, reflection.type, value, tracker
41
+ scope = scope.where(table[reflection.type].eq(bind_val))
42
+ end
43
+
44
+ scope = scope.joins(join(foreign_table, constraint))
45
+ end
46
+
47
+ is_first_chain = i == 0
48
+ klass = is_first_chain ? assoc_klass : reflection.klass
49
+
50
+ # Exclude the scope of the association itself, because that
51
+ # was already merged in the #scope method.
52
+ scope_chain[i].each do |scope_chain_item|
53
+ item = eval_scope(klass, scope_chain_item, owner)
54
+
55
+ if scope_chain_item == refl.scope
56
+ scope.merge! item.except(:where, :includes, :bind)
57
+ end
58
+
59
+ if is_first_chain
60
+ scope.includes! item.includes_values
61
+ end
62
+
63
+ scope.where_values += item.where_values
64
+ scope.bind_values += item.bind_values
65
+ scope.order_values |= item.order_values
66
+ end
67
+ end
68
+
69
+ scope
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,15 @@
1
+ module CompositePrimaryKeys
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
9
+ end
10
+ alias_method_chain :get_records, :cpk_support
11
+ end
12
+ end
13
+ end
14
+
15
+ ActiveRecord::Associations::CollectionAssociation.send(:include, CompositePrimaryKeys::CollectionAssociation)
@@ -1,56 +1,69 @@
1
- module ActiveRecord
2
- module Associations
3
- class HasManyAssociation
4
- def delete_records(records, method)
5
- if method == :destroy
6
- records.each(&:destroy!)
7
- update_counter(-records.length) unless inverse_updates_counter_cache?
8
- else
9
- if records == :all || !reflection.klass.primary_key
10
- scope = self.scope
11
- else
12
- # CPK
13
- # scope = self.scope.where(reflection.klass.primary_key => records)
14
- table = Arel::Table.new(reflection.table_name)
15
- and_conditions = records.map do |record|
16
- eq_conditions = Array(reflection.association_primary_key).map do |name|
17
- table[name].eq(record[name])
18
- end
19
- Arel::Nodes::And.new(eq_conditions)
20
- end
21
-
22
- condition = and_conditions.shift
23
- and_conditions.each do |and_condition|
24
- condition = condition.or(and_condition)
25
- end
26
-
27
- scope = self.scope.where(condition)
28
- end
29
-
30
- if method == :delete_all
31
- update_counter(-scope.delete_all)
32
- else
33
- # CPK
34
- # update_counter(-scope.update_all(reflection.foreign_key => nil))
35
- update_hash = Array(reflection.foreign_key).inject(Hash.new) do |hash, key|
36
- hash[key] = nil
37
- hash
38
- end
39
- update_counter(-scope.update_all(update_hash))
40
- end
41
- end
42
- end
43
-
44
- def foreign_key_present?
45
- if reflection.klass.primary_key
46
- # CPK
47
- #owner.attribute_present?(reflection.association_primary_key)
48
- owner.attribute_present?(reflection.association_primary_key)
49
- Array(reflection.klass.primary_key).all? {|key| owner.attribute_present?(key)}
50
- else
51
- false
52
- end
53
- end
54
- end
55
- end
56
- end
1
+ module ActiveRecord
2
+ module Associations
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
+
44
+ if method == :delete_all
45
+ update_counter(-scope.delete_all)
46
+ else
47
+ # 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
52
+ end
53
+ update_counter(-scope.update_all(update_hash))
54
+ end
55
+ end
56
+ end
57
+
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
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end