composite_primary_keys 7.0.10 → 7.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.rdoc +12 -0
- data/lib/composite_primary_keys.rb +3 -8
- data/lib/composite_primary_keys/associations/has_many_association.rb +7 -2
- data/lib/composite_primary_keys/attribute_methods/primary_key.rb +21 -0
- data/lib/composite_primary_keys/attribute_methods/write.rb +1 -0
- data/lib/composite_primary_keys/relation.rb +13 -9
- data/lib/composite_primary_keys/version.rb +1 -1
- data/test/test_delete.rb +89 -89
- metadata +7 -7
- data/lib/composite_primary_keys/active_model/dirty.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4146bd7f227eed41c3dbfeb8cd6e397789839531
|
4
|
+
data.tar.gz: 523601026d76d779cc5b96e216fb04adb562c0a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: feb2446749e8f8872a7073a1ba9d6409071e963cf46ea84d7b3bf8abd922ec580320b99cb33459694d56d4d57d74568a9f13ddb9b2f6e2eda7eb7e9c12a6ef6c
|
7
|
+
data.tar.gz: b842f5f95e23cae95d0af45bb315355542f6601d9407f745db47ac7a15d8315ebcc5a52506e380395a69e45183183f5f8b6191014e5c980f5643b58ed99f2719
|
data/History.rdoc
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
== 7.0.11 (2014-10-10)
|
2
|
+
|
3
|
+
* ActiveRecord 4.1.6 support (Tom Hughes, Charlie Savage)
|
4
|
+
* Verify MariaDB support and update docs (Isi Robayna)
|
5
|
+
* Postgresql binary column support (Piotr Gębala)
|
6
|
+
* Fix deleting of records from collection association (akeuk and Charlie Savage)
|
7
|
+
* Fix invalid include (shiro16)
|
8
|
+
* Improve tests (Yann Marquet)
|
9
|
+
|
1
10
|
== 7.0.10 (2014-08-07)
|
2
11
|
|
3
12
|
* Update attribute dirty methods to allow attribute *_was
|
@@ -69,6 +78,9 @@ The first one no longer works. It was removed because it made the internal code
|
|
69
78
|
and makes the intention of the code clearer (especially when finding multiple records).
|
70
79
|
If this change causes too much pain then please submit a ticket on Github.
|
71
80
|
|
81
|
+
== 6.0.7 (2014-10-06)
|
82
|
+
|
83
|
+
* Support Rails 4.0.6 (Tom Hughes)
|
72
84
|
|
73
85
|
== 6.0.6 (2014-05-26)
|
74
86
|
|
@@ -26,13 +26,10 @@ $:.unshift(File.dirname(__FILE__)) unless
|
|
26
26
|
|
27
27
|
unless defined?(ActiveRecord)
|
28
28
|
require 'rubygems'
|
29
|
-
gem 'activerecord', '~>4.1.
|
29
|
+
gem 'activerecord', '~>4.1.6'
|
30
30
|
require 'active_record'
|
31
31
|
end
|
32
32
|
|
33
|
-
# AM files we override
|
34
|
-
require 'active_model/dirty'
|
35
|
-
|
36
33
|
# AR files we override
|
37
34
|
require 'active_record/counter_cache'
|
38
35
|
require 'active_record/fixtures'
|
@@ -51,9 +48,7 @@ require 'active_record/associations/join_dependency/join_association'
|
|
51
48
|
require 'active_record/associations/preloader/association'
|
52
49
|
require 'active_record/associations/preloader/belongs_to'
|
53
50
|
|
54
|
-
|
55
|
-
require 'active_model/dirty'
|
56
|
-
|
51
|
+
require 'active_record/attribute_methods/primary_key'
|
57
52
|
require 'active_record/attribute_methods/dirty'
|
58
53
|
require 'active_record/attribute_methods/read'
|
59
54
|
require 'active_record/attribute_methods/write'
|
@@ -70,7 +65,6 @@ require 'active_record/relation/query_methods'
|
|
70
65
|
require 'active_record/validations/uniqueness'
|
71
66
|
|
72
67
|
# CPK files
|
73
|
-
require 'composite_primary_keys/active_model/dirty'
|
74
68
|
require 'composite_primary_keys/persistence'
|
75
69
|
require 'composite_primary_keys/base'
|
76
70
|
require 'composite_primary_keys/core'
|
@@ -94,6 +88,7 @@ require 'composite_primary_keys/associations/preloader/belongs_to'
|
|
94
88
|
|
95
89
|
require 'composite_primary_keys/dirty'
|
96
90
|
|
91
|
+
require 'composite_primary_keys/attribute_methods/primary_key'
|
97
92
|
require 'composite_primary_keys/attribute_methods/dirty'
|
98
93
|
require 'composite_primary_keys/attribute_methods/read'
|
99
94
|
require 'composite_primary_keys/attribute_methods/write'
|
@@ -11,7 +11,6 @@ module ActiveRecord
|
|
11
11
|
else
|
12
12
|
# CPK
|
13
13
|
# scope = self.scope.where(reflection.klass.primary_key => records)
|
14
|
-
# scope = self.scope.where(reflection.klass.primary_key => records)
|
15
14
|
table = Arel::Table.new(reflection.table_name)
|
16
15
|
and_conditions = records.map do |record|
|
17
16
|
eq_conditions = Array(reflection.association_primary_key).map do |name|
|
@@ -31,7 +30,13 @@ module ActiveRecord
|
|
31
30
|
if method == :delete_all
|
32
31
|
update_counter(-scope.delete_all)
|
33
32
|
else
|
34
|
-
|
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))
|
35
40
|
end
|
36
41
|
end
|
37
42
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module AttributeMethods
|
3
|
+
module PrimaryKey
|
4
|
+
# Returns the primary key previous value.
|
5
|
+
def id_was
|
6
|
+
sync_with_transaction_state
|
7
|
+
# CPK
|
8
|
+
#attribute_was(self.class.primary_key)
|
9
|
+
|
10
|
+
if self.composite?
|
11
|
+
self.class.primary_keys.map do |key_attr|
|
12
|
+
attribute_changed?(key_attr) ? changed_attributes[key_attr] : self.ids_hash[key_attr]
|
13
|
+
end
|
14
|
+
else
|
15
|
+
attribute_was(self.class.primary_key)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -21,17 +21,21 @@ module ActiveRecord
|
|
21
21
|
include CompositePrimaryKeys::ActiveRecord::QueryMethods
|
22
22
|
|
23
23
|
def delete(id_or_array)
|
24
|
-
#
|
25
|
-
|
24
|
+
# CPK
|
25
|
+
if self.composite?
|
26
|
+
id_or_array = if id_or_array.is_a?(CompositePrimaryKeys::CompositeKeys)
|
27
|
+
[id_or_array]
|
28
|
+
else
|
29
|
+
Array(id_or_array)
|
30
|
+
end
|
26
31
|
|
27
|
-
|
28
|
-
|
32
|
+
id_or_array.each do |id|
|
33
|
+
# Is the passed in id actually a record?
|
34
|
+
id = id.kind_of?(ActiveRecord::Base) ? id.id : id
|
35
|
+
where(cpk_id_predicate(table, self.primary_key, id)).delete_all
|
36
|
+
end
|
29
37
|
else
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
id_or_array.each do |id|
|
34
|
-
where(cpk_id_predicate(table, self.primary_key, id)).delete_all
|
38
|
+
where(primary_key => id_or_array).delete_all
|
35
39
|
end
|
36
40
|
end
|
37
41
|
|
data/test/test_delete.rb
CHANGED
@@ -19,57 +19,57 @@ class TestDelete < ActiveSupport::TestCase
|
|
19
19
|
self.class.classes = CLASSES
|
20
20
|
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
22
|
+
def test_destroy_one
|
23
|
+
testing_with do
|
24
|
+
assert @first.destroy
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_destroy_one_alone_via_class
|
29
|
+
testing_with do
|
30
|
+
assert @klass.destroy(@first.id)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_delete_one_alone
|
35
|
+
testing_with do
|
36
|
+
assert @klass.delete(@first.id)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_delete_many
|
41
|
+
testing_with do
|
42
|
+
to_delete = @klass.limit(2)
|
43
|
+
assert_equal 2, to_delete.length
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_delete_all
|
48
|
+
testing_with do
|
49
|
+
@klass.delete_all
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_clear_association
|
54
|
+
department = Department.find([1,1])
|
55
|
+
assert_equal(2, department.employees.size, "Before clear employee count should be 2.")
|
56
|
+
|
57
|
+
department.employees.clear
|
58
|
+
assert_equal(0, department.employees.size, "After clear employee count should be 0.")
|
59
|
+
|
60
|
+
department.reload
|
61
|
+
assert_equal(0, department.employees.size, "After clear and a reload from DB employee count should be 0.")
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_delete_association
|
65
|
+
department = Department.find([1,1])
|
66
|
+
assert_equal 2, department.employees.size , "Before delete employee count should be 2."
|
67
|
+
first_employee = department.employees[0]
|
68
|
+
department.employees.delete(first_employee)
|
69
|
+
assert_equal 1, department.employees.size, "After delete employee count should be 1."
|
70
|
+
department.reload
|
71
|
+
assert_equal 1, department.employees.size, "After delete and a reload from DB employee count should be 1."
|
72
|
+
end
|
73
73
|
|
74
74
|
def test_destroy_has_one
|
75
75
|
# In this case the association is a has_one with
|
@@ -88,7 +88,7 @@ class TestDelete < ActiveSupport::TestCase
|
|
88
88
|
Employee.find(head_id)
|
89
89
|
end
|
90
90
|
end
|
91
|
-
|
91
|
+
|
92
92
|
def test_destroy_has_and_belongs_to_many_on_non_cpk
|
93
93
|
steve = employees(:steve)
|
94
94
|
records_before = ActiveRecord::Base.connection.execute("select * from employees_groups").count
|
@@ -102,47 +102,47 @@ class TestDelete < ActiveSupport::TestCase
|
|
102
102
|
tariff.delete
|
103
103
|
assert !tariff.persisted?
|
104
104
|
end
|
105
|
-
|
105
|
+
|
106
106
|
def test_delete_not_destroy_on_non_cpk
|
107
107
|
article = Article.first
|
108
108
|
article.delete
|
109
109
|
assert !article.persisted?
|
110
110
|
end
|
111
111
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
112
|
+
def test_destroy_has_many_delete_all
|
113
|
+
# In this case the association is a has_many composite key with
|
114
|
+
# dependent set to :delete_all
|
115
|
+
product = Product.find(1)
|
116
|
+
assert_equal(2, product.product_tariffs.length)
|
117
|
+
|
118
|
+
# Get product_tariff length
|
119
|
+
product_tariff_size = ProductTariff.count
|
120
|
+
|
121
|
+
# Delete product - should delete 2 product tariffs
|
122
|
+
product.destroy
|
123
|
+
|
124
|
+
# Verify product_tariff are deleted
|
125
|
+
assert_equal(product_tariff_size - 2, ProductTariff.count)
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_delete_cpk_association
|
129
|
+
product = Product.find(1)
|
130
|
+
assert_equal(2, product.product_tariffs.length)
|
131
|
+
|
132
|
+
product_tariff = product.product_tariffs.first
|
133
|
+
product.product_tariffs.delete(product_tariff)
|
134
|
+
|
135
|
+
product.reload
|
136
|
+
assert_equal(1, product.product_tariffs.length)
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_delete_records_for_has_many_association_with_composite_primary_key
|
140
|
+
reference_type = ReferenceType.find(1)
|
141
|
+
codes_to_delete = reference_type.reference_codes[0..1]
|
142
|
+
assert_equal(3, reference_type.reference_codes.size, "Before deleting records reference_code count should be 3.")
|
143
|
+
|
144
|
+
reference_type.reference_codes.delete(codes_to_delete)
|
145
|
+
reference_type.reload
|
146
|
+
assert_equal(1, reference_type.reference_codes.size, "After deleting 2 records and a reload from DB reference_code count should be 1.")
|
147
|
+
end
|
148
148
|
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composite_primary_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 7.0.
|
4
|
+
version: 7.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charlie Savage
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 4.1.
|
19
|
+
version: 4.1.6
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 4.1.
|
26
|
+
version: 4.1.6
|
27
27
|
description: Composite key support for ActiveRecord
|
28
28
|
email:
|
29
29
|
executables: []
|
@@ -37,7 +37,6 @@ files:
|
|
37
37
|
- init.rb
|
38
38
|
- install.rb
|
39
39
|
- lib/composite_primary_keys.rb
|
40
|
-
- lib/composite_primary_keys/active_model/dirty.rb
|
41
40
|
- lib/composite_primary_keys/associations/association.rb
|
42
41
|
- lib/composite_primary_keys/associations/association_scope.rb
|
43
42
|
- lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
|
@@ -50,6 +49,7 @@ files:
|
|
50
49
|
- lib/composite_primary_keys/associations/preloader/belongs_to.rb
|
51
50
|
- lib/composite_primary_keys/associations/preloader/has_and_belongs_to_many.rb
|
52
51
|
- lib/composite_primary_keys/attribute_methods/dirty.rb
|
52
|
+
- lib/composite_primary_keys/attribute_methods/primary_key.rb
|
53
53
|
- lib/composite_primary_keys/attribute_methods/read.rb
|
54
54
|
- lib/composite_primary_keys/attribute_methods/write.rb
|
55
55
|
- lib/composite_primary_keys/base.rb
|
@@ -1,14 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
module Dirty
|
3
|
-
def attribute_was(attr)
|
4
|
-
# CPK
|
5
|
-
if self.composite? && attr == "id"
|
6
|
-
self.class.primary_keys.map do |key_attr|
|
7
|
-
attribute_changed?(key_attr) ? changed_attributes[key_attr] : self.ids_hash[key_attr]
|
8
|
-
end
|
9
|
-
else
|
10
|
-
attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|