composite_primary_keys 7.0.1 → 7.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.rdoc +5 -0
- data/README.rdoc +7 -0
- data/lib/composite_primary_keys.rb +2 -0
- data/lib/composite_primary_keys/active_model/dirty.rb +2 -2
- data/lib/composite_primary_keys/composite_predicates.rb +6 -5
- data/lib/composite_primary_keys/relation.rb +1 -1
- data/lib/composite_primary_keys/relation/predicate_builder.rb +22 -0
- data/lib/composite_primary_keys/version.rb +1 -1
- data/test/fixtures/membership.rb +0 -1
- data/test/test_callbacks.rb +77 -0
- data/test/test_find.rb +6 -0
- data/test/test_predicates.rb +1 -1
- data/test/test_update.rb +3 -3
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a95b00436decb1d0c5da9fc42f706be34aaf6dc
|
4
|
+
data.tar.gz: 08b6d26c4d0e136b577c59394becfc82af3b0a21
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e02f25ffa1220a27361433d224c07f584ec130dc7cb2dfbf1f542e903486d80dec239555b62a96a96a3375eec45fa82bf9810374700d40184075aeb7ddd30b0
|
7
|
+
data.tar.gz: a5d9c69b6eccada21cfed579bcd2bd9a4f6ea1987a9bb3cf4c43700c31a4859d24fb0514d8b27d8d46d00cd6ccc79b59dd89e5568bb8646953051810ecd4ef3e
|
data/History.rdoc
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
== 7.0.2 (2014-06-01)
|
2
|
+
|
3
|
+
* Fix id_was when one of the column names is called id (Adrian Hooper)
|
4
|
+
* Fix queries that use ActiveRecord objects in the where clause (Adrian Hooper)
|
5
|
+
|
1
6
|
== 7.0.1 (2014-05-27)
|
2
7
|
|
3
8
|
* Fix setting of timestamps automatically by Rails (Charlie Savage)
|
data/README.rdoc
CHANGED
@@ -71,6 +71,13 @@ Now we want to be able to find instances using the same syntax we always use for
|
|
71
71
|
|
72
72
|
Notice the use of an array to specify the composite key values.
|
73
73
|
|
74
|
+
NOTE - API CHANGE. CPK Version 6.x and earlier used to allow composite keys to be listed out
|
75
|
+
like this:
|
76
|
+
|
77
|
+
Membership.find(1,1)
|
78
|
+
|
79
|
+
This usage is no longer supported.
|
80
|
+
|
74
81
|
== Databases
|
75
82
|
|
76
83
|
CPK supports the following databases:
|
@@ -64,6 +64,7 @@ require 'active_record/connection_adapters/abstract_adapter'
|
|
64
64
|
require 'active_record/relation/batches'
|
65
65
|
require 'active_record/relation/calculations'
|
66
66
|
require 'active_record/relation/finder_methods'
|
67
|
+
require 'active_record/relation/predicate_builder'
|
67
68
|
require 'active_record/relation/query_methods'
|
68
69
|
|
69
70
|
require 'active_record/validations/uniqueness'
|
@@ -104,6 +105,7 @@ require 'composite_primary_keys/connection_adapters/abstract/connection_specific
|
|
104
105
|
require 'composite_primary_keys/relation/batches'
|
105
106
|
require 'composite_primary_keys/relation/calculations'
|
106
107
|
require 'composite_primary_keys/relation/finder_methods'
|
108
|
+
require 'composite_primary_keys/relation/predicate_builder'
|
107
109
|
require 'composite_primary_keys/relation/query_methods'
|
108
110
|
|
109
111
|
require 'composite_primary_keys/validations/uniqueness'
|
@@ -3,8 +3,8 @@ module ActiveModel
|
|
3
3
|
def attribute_was(attr)
|
4
4
|
# CPK
|
5
5
|
if self.composite? && attr == "id"
|
6
|
-
self.class.primary_keys.map do |
|
7
|
-
attribute_changed?(
|
6
|
+
self.class.primary_keys.map do |key_attr|
|
7
|
+
attribute_changed?(key_attr) ? changed_attributes[key_attr] : self.ids_hash[key_attr]
|
8
8
|
end
|
9
9
|
else
|
10
10
|
attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr)
|
@@ -9,9 +9,12 @@ module CompositePrimaryKeys
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def cpk_or_predicate(predicates)
|
12
|
-
|
12
|
+
or_predicate = predicates.map do |predicate|
|
13
|
+
::Arel::Nodes::Grouping.new(predicate)
|
14
|
+
end.inject do |memo, node|
|
13
15
|
::Arel::Nodes::Or.new(memo, node)
|
14
|
-
|
16
|
+
end
|
17
|
+
::Arel::Nodes::Grouping.new(or_predicate)
|
15
18
|
end
|
16
19
|
|
17
20
|
def cpk_id_predicate(table, keys, values)
|
@@ -45,6 +48,4 @@ ActiveRecord::Associations::JoinDependency::JoinAssociation.send(:include, Compo
|
|
45
48
|
ActiveRecord::Associations::Preloader::Association.send(:include, CompositePrimaryKeys::Predicates)
|
46
49
|
ActiveRecord::Associations::HasManyThroughAssociation.send(:include, CompositePrimaryKeys::Predicates)
|
47
50
|
ActiveRecord::Relation.send(:include, CompositePrimaryKeys::Predicates)
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
+
ActiveRecord::PredicateBuilder.send(:extend, CompositePrimaryKeys::Predicates)
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
class PredicateBuilder
|
3
|
+
def self.expand(klass, table, column, value)
|
4
|
+
queries = []
|
5
|
+
if klass && reflection = klass.reflect_on_association(column.to_sym)
|
6
|
+
if reflection.polymorphic? && base_class = polymorphic_base_class_from_value(value)
|
7
|
+
queries << build(table[reflection.foreign_type], base_class)
|
8
|
+
end
|
9
|
+
|
10
|
+
column = reflection.foreign_key
|
11
|
+
end
|
12
|
+
|
13
|
+
#CPK
|
14
|
+
if Base === value && value.composite?
|
15
|
+
queries << cpk_id_predicate(table, column, value.id)
|
16
|
+
else
|
17
|
+
queries << build(table[column], value)
|
18
|
+
end
|
19
|
+
queries
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/test/fixtures/membership.rb
CHANGED
@@ -0,0 +1,77 @@
|
|
1
|
+
require File.expand_path('../abstract_unit', __FILE__)
|
2
|
+
|
3
|
+
class TestUpdate < ActiveSupport::TestCase
|
4
|
+
fixtures :suburbs
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@@callbacks = OpenStruct.new
|
8
|
+
end
|
9
|
+
|
10
|
+
Suburb.class_eval do
|
11
|
+
before_create do
|
12
|
+
@@callbacks.before_create = true
|
13
|
+
end
|
14
|
+
|
15
|
+
after_create do
|
16
|
+
@@callbacks.after_create = true
|
17
|
+
end
|
18
|
+
|
19
|
+
around_create do |suburb, block|
|
20
|
+
@@callbacks.around_create = true
|
21
|
+
block.call
|
22
|
+
end
|
23
|
+
|
24
|
+
before_save do
|
25
|
+
@@callbacks.before_save = true
|
26
|
+
end
|
27
|
+
|
28
|
+
after_save do
|
29
|
+
@@callbacks.after_save = true
|
30
|
+
end
|
31
|
+
|
32
|
+
around_save do |suburb, block|
|
33
|
+
@@callbacks.around_save = true
|
34
|
+
block.call
|
35
|
+
end
|
36
|
+
|
37
|
+
before_update do
|
38
|
+
@@callbacks.before_update = true
|
39
|
+
end
|
40
|
+
|
41
|
+
after_update do
|
42
|
+
@@callbacks.after_update = true
|
43
|
+
end
|
44
|
+
|
45
|
+
around_update do |suburb, block|
|
46
|
+
@@callbacks.around_update = true
|
47
|
+
block.call
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_create
|
52
|
+
suburb = Suburb.new(:city_id => 3, :suburb_id => 3, :name => 'created')
|
53
|
+
suburb.save!
|
54
|
+
|
55
|
+
assert(@@callbacks.before_save)
|
56
|
+
assert(@@callbacks.after_save)
|
57
|
+
assert(@@callbacks.around_save)
|
58
|
+
|
59
|
+
assert(@@callbacks.before_create)
|
60
|
+
assert(@@callbacks.after_create)
|
61
|
+
assert(@@callbacks.around_create)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_update
|
65
|
+
suburb = suburbs(:first)
|
66
|
+
suburb.name = 'Updated'
|
67
|
+
suburb.save
|
68
|
+
|
69
|
+
assert(@@callbacks.before_update)
|
70
|
+
assert(@@callbacks.after_update)
|
71
|
+
assert(@@callbacks.around_update)
|
72
|
+
|
73
|
+
assert(@@callbacks.before_save)
|
74
|
+
assert(@@callbacks.after_save)
|
75
|
+
assert(@@callbacks.around_save)
|
76
|
+
end
|
77
|
+
end
|
data/test/test_find.rb
CHANGED
@@ -85,4 +85,10 @@ class TestFind < ActiveSupport::TestCase
|
|
85
85
|
assert_equal(Department.count, batch.size)
|
86
86
|
end
|
87
87
|
end
|
88
|
+
|
89
|
+
def test_expand
|
90
|
+
department = departments(:engineering)
|
91
|
+
employees = Employee.where(:department => department)
|
92
|
+
assert_equal(2, employees.count)
|
93
|
+
end
|
88
94
|
end
|
data/test/test_predicates.rb
CHANGED
@@ -16,7 +16,7 @@ class TestEqual < ActiveSupport::TestCase
|
|
16
16
|
|
17
17
|
connection = ActiveRecord::Base.connection
|
18
18
|
quoted = "#{connection.quote_table_name('departments')}.#{connection.quote_column_name('id')}"
|
19
|
-
expected = "(#{quoted} = 0 OR #{quoted} = 1 OR #{quoted} = 2)"
|
19
|
+
expected = "((#{quoted} = 0) OR (#{quoted} = 1) OR (#{quoted} = 2))"
|
20
20
|
|
21
21
|
pred = cpk_or_predicate(predicates)
|
22
22
|
assert_equal(with_quoted_identifiers(expected), pred.to_sql)
|
data/test/test_update.rb
CHANGED
@@ -62,9 +62,9 @@ class TestUpdate < ActiveSupport::TestCase
|
|
62
62
|
|
63
63
|
def test_update_all
|
64
64
|
assert_nothing_raised do
|
65
|
-
|
66
|
-
primary_key =
|
67
|
-
ReferenceCode.where(primary_key =>
|
65
|
+
reference_code = ReferenceCode.create
|
66
|
+
primary_key = reference_code.class.primary_key
|
67
|
+
ReferenceCode.where(primary_key => reference_code[primary_key]).
|
68
68
|
update_all(description: 'random value')
|
69
69
|
end
|
70
70
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.2
|
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-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- lib/composite_primary_keys/relation/batches.rb
|
69
69
|
- lib/composite_primary_keys/relation/calculations.rb
|
70
70
|
- lib/composite_primary_keys/relation/finder_methods.rb
|
71
|
+
- lib/composite_primary_keys/relation/predicate_builder.rb
|
71
72
|
- lib/composite_primary_keys/relation/query_methods.rb
|
72
73
|
- lib/composite_primary_keys/sanitization.rb
|
73
74
|
- lib/composite_primary_keys/validations/uniqueness.rb
|
@@ -167,6 +168,7 @@ files:
|
|
167
168
|
- test/test_attribute_methods.rb
|
168
169
|
- test/test_attributes.rb
|
169
170
|
- test/test_calculations.rb
|
171
|
+
- test/test_callbacks.rb
|
170
172
|
- test/test_composite_arrays.rb
|
171
173
|
- test/test_counter_cache.rb
|
172
174
|
- test/test_create.rb
|
@@ -190,7 +192,8 @@ files:
|
|
190
192
|
- test/test_update.rb
|
191
193
|
- test/test_validations.rb
|
192
194
|
homepage: https://github.com/composite-primary-keys/composite_primary_keys
|
193
|
-
licenses:
|
195
|
+
licenses:
|
196
|
+
- MIT
|
194
197
|
metadata: {}
|
195
198
|
post_install_message:
|
196
199
|
rdoc_options: []
|
@@ -207,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
207
210
|
- !ruby/object:Gem::Version
|
208
211
|
version: '0'
|
209
212
|
requirements: []
|
210
|
-
rubyforge_project:
|
213
|
+
rubyforge_project:
|
211
214
|
rubygems_version: 2.2.2
|
212
215
|
signing_key:
|
213
216
|
specification_version: 4
|
@@ -222,6 +225,7 @@ test_files:
|
|
222
225
|
- test/test_attributes.rb
|
223
226
|
- test/test_attribute_methods.rb
|
224
227
|
- test/test_calculations.rb
|
228
|
+
- test/test_callbacks.rb
|
225
229
|
- test/test_composite_arrays.rb
|
226
230
|
- test/test_counter_cache.rb
|
227
231
|
- test/test_create.rb
|