composite_primary_keys 4.0.0.beta2 → 4.0.0.beta3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -2
- data/lib/composite_primary_keys.rb +4 -0
- data/lib/composite_primary_keys/base.rb +4 -0
- data/lib/composite_primary_keys/dirty.rb +19 -0
- data/lib/composite_primary_keys/persistence.rb +12 -5
- data/lib/composite_primary_keys/version.rb +1 -1
- data/test/connections/native_postgresql/connection.rb +2 -0
- data/test/fixtures/products.yml +0 -1
- data/test/fixtures/suburbs.yml +4 -1
- data/test/test_habtm.rb +42 -0
- data/test/test_update.rb +15 -0
- metadata +5 -4
data/History.txt
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
== 4.0.0.
|
1
|
+
== 4.0.0.beta3 2011-07-08
|
2
|
+
* Fix the ability to update the values of a primary key (Travis Warlick)
|
3
|
+
* Support port and host configurations for postgres rake tasks (Travis Warlick)
|
4
|
+
|
5
|
+
== 4.0.0.beta2 2011-06-21
|
2
6
|
* ActiveRecord 3.1 RC4 compatibility.
|
3
7
|
* Fix instantiation of CPK models with included associations
|
4
8
|
|
@@ -6,10 +10,11 @@
|
|
6
10
|
* ActiveRecord 3.1 RC1 compatibility. This required a significant rewrite due to
|
7
11
|
all the changes in AR 3.1 versus 3.0.
|
8
12
|
|
9
|
-
== 3.1.10
|
13
|
+
== 3.1.10 2011-07-08
|
10
14
|
* Bugfix for belongs_to with includes (John Ash)
|
11
15
|
* Improved tests for calling clear on a habtm association, which involved (David Rueck)
|
12
16
|
* Fixed bug that resulted in unrelated records being deleted when calling (David Rueck)
|
17
|
+
* Output deprecation warnings about extra columns in join table CPK-aware (David Rueck)
|
13
18
|
|
14
19
|
|
15
20
|
== 3.1.9 2011-06-04
|
@@ -46,6 +46,8 @@ require 'active_record/associations/preloader/association'
|
|
46
46
|
require 'active_record/associations/preloader/belongs_to'
|
47
47
|
require 'active_record/associations/preloader/has_and_belongs_to_many'
|
48
48
|
|
49
|
+
require 'active_model/dirty'
|
50
|
+
|
49
51
|
require 'active_record/attribute_methods/dirty'
|
50
52
|
require 'active_record/attribute_methods/read'
|
51
53
|
require 'active_record/attribute_methods/write'
|
@@ -80,6 +82,8 @@ require 'composite_primary_keys/associations/preloader/association'
|
|
80
82
|
require 'composite_primary_keys/associations/preloader/belongs_to'
|
81
83
|
require 'composite_primary_keys/associations/preloader/has_and_belongs_to_many'
|
82
84
|
|
85
|
+
require 'composite_primary_keys/dirty'
|
86
|
+
|
83
87
|
require 'composite_primary_keys/attribute_methods/dirty'
|
84
88
|
require 'composite_primary_keys/attribute_methods/read'
|
85
89
|
require 'composite_primary_keys/attribute_methods/write'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Dirty
|
3
|
+
def can_change_primary_key?
|
4
|
+
true
|
5
|
+
end
|
6
|
+
|
7
|
+
def primary_key_changed?
|
8
|
+
!!changed.detect { |key| ids_hash.keys.include?(key.to_sym) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def primary_key_was
|
12
|
+
ids_hash.keys.inject(Hash.new) do |result, attribute_name|
|
13
|
+
result[attribute_name.to_sym] = attribute_was(attribute_name.to_s)
|
14
|
+
result
|
15
|
+
end
|
16
|
+
end
|
17
|
+
alias_method :ids_hash_was, :primary_key_was
|
18
|
+
end
|
19
|
+
end
|
@@ -32,15 +32,22 @@ module ActiveRecord
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def update(attribute_names = @attributes.keys)
|
35
|
-
attributes_with_values = arel_attributes_values(false, false, attribute_names)
|
36
|
-
return 0 if attributes_with_values.empty?
|
37
35
|
klass = self.class
|
38
|
-
# CPK
|
39
36
|
if !self.composite?
|
37
|
+
attributes_with_values = arel_attributes_values(false, false, attribute_names)
|
38
|
+
return 0 if attributes_with_values.empty?
|
40
39
|
stmt = klass.unscoped.where(klass.arel_table[klass.primary_key].eq(id)).arel.compile_update(attributes_with_values)
|
41
40
|
else
|
42
|
-
|
43
|
-
|
41
|
+
attributes_with_values = arel_attributes_values(can_change_primary_key?, false, attribute_names)
|
42
|
+
return 0 if attributes_with_values.empty?
|
43
|
+
|
44
|
+
if !can_change_primary_key? and primary_key_changed?
|
45
|
+
raise ActiveRecord::CompositeKeyError, "Cannot update primary key values without ActiveModel::Dirty"
|
46
|
+
elsif primary_key_changed?
|
47
|
+
stmt = klass.unscoped.where(primary_key_was).arel.compile_update(attributes_with_values)
|
48
|
+
else
|
49
|
+
stmt = klass.unscoped.where(ids_hash).arel.compile_update(attributes_with_values)
|
50
|
+
end
|
44
51
|
end
|
45
52
|
klass.connection.update stmt.to_sql
|
46
53
|
end
|
@@ -7,6 +7,8 @@ require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys', 'connection_ada
|
|
7
7
|
def connection_string
|
8
8
|
options = Hash.new
|
9
9
|
options['U'] = SPEC['username'] if SPEC['username']
|
10
|
+
options['h'] = SPEC['host'] if SPEC['host']
|
11
|
+
options['p'] = SPEC['port'] if SPEC['port']
|
10
12
|
options.map { |key, value| "-#{key} #{value}" }.join(" ")
|
11
13
|
end
|
12
14
|
|
data/test/fixtures/products.yml
CHANGED
data/test/fixtures/suburbs.yml
CHANGED
data/test/test_habtm.rb
CHANGED
@@ -13,6 +13,48 @@ class TestHabtm < ActiveSupport::TestCase
|
|
13
13
|
assert_equal 2, @restaurant.suburbs.size
|
14
14
|
end
|
15
15
|
|
16
|
+
def test_include_cpk_both_sides
|
17
|
+
# assuming the association was set up in the fixtures
|
18
|
+
# file restaurants_suburbs.yml
|
19
|
+
mcdonalds = restaurants(:mcdonalds)
|
20
|
+
# check positive
|
21
|
+
suburb = mcdonalds.suburbs[0]
|
22
|
+
assert mcdonalds.suburbs.include?(suburb)
|
23
|
+
# check negative
|
24
|
+
suburb_with_no_mcdonalds = suburbs(:no_mcdonalds)
|
25
|
+
assert !mcdonalds.suburbs.include?(suburb_with_no_mcdonalds)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_include_cpk_owner_side_only
|
29
|
+
subway = restaurants(:subway_one)
|
30
|
+
product = products(:first_product)
|
31
|
+
subway.products << product
|
32
|
+
|
33
|
+
# reload
|
34
|
+
# test positive
|
35
|
+
subway = restaurants(:subway_one)
|
36
|
+
assert subway.products.include?(product)
|
37
|
+
|
38
|
+
# test negative
|
39
|
+
product_two = products(:second_product)
|
40
|
+
assert !subway.products.include?(product_two)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_include_cpk_association_side_only
|
44
|
+
product = products(:first_product)
|
45
|
+
subway = restaurants(:subway_one)
|
46
|
+
product.restaurants << subway
|
47
|
+
|
48
|
+
# reload
|
49
|
+
# test positive
|
50
|
+
product = products(:first_product)
|
51
|
+
assert product.restaurants.include?(subway)
|
52
|
+
|
53
|
+
# test negative
|
54
|
+
mcdonalds = restaurants(:mcdonalds)
|
55
|
+
assert !product.restaurants.include?(mcdonalds)
|
56
|
+
end
|
57
|
+
|
16
58
|
def test_habtm_clear_cpk_both_sides
|
17
59
|
@restaurant = restaurants(:mcdonalds)
|
18
60
|
assert_equal 2, @restaurant.suburbs.size
|
data/test/test_update.rb
CHANGED
@@ -35,4 +35,19 @@ class TestUpdate < ActiveSupport::TestCase
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
|
+
|
39
|
+
def test_update_primary_key
|
40
|
+
obj = ReferenceCode.find([1,1])
|
41
|
+
obj.reference_type_id = 2
|
42
|
+
obj.reference_code = 3
|
43
|
+
assert(obj.primary_key_changed?)
|
44
|
+
assert_equal({:reference_type_id => 1, :reference_code => 1}, obj.primary_key_was)
|
45
|
+
assert_equal({:reference_type_id => 2, :reference_code => 3}, obj.ids_hash)
|
46
|
+
assert(obj.save)
|
47
|
+
assert(obj.reload)
|
48
|
+
assert_equal(2, obj.reference_type_id)
|
49
|
+
assert_equal(3, obj.reference_code)
|
50
|
+
assert_equal({:reference_type_id => 2, :reference_code => 3}, obj.ids_hash)
|
51
|
+
assert_equal([2, 3], obj.id)
|
52
|
+
end
|
38
53
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composite_primary_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 62196261
|
5
5
|
prerelease: 6
|
6
6
|
segments:
|
7
7
|
- 4
|
8
8
|
- 0
|
9
9
|
- 0
|
10
10
|
- beta
|
11
|
-
-
|
12
|
-
version: 4.0.0.
|
11
|
+
- 3
|
12
|
+
version: 4.0.0.beta3
|
13
13
|
platform: ruby
|
14
14
|
authors:
|
15
15
|
- Dr Nic Williams
|
@@ -18,7 +18,7 @@ autorequire:
|
|
18
18
|
bindir: bin
|
19
19
|
cert_chain: []
|
20
20
|
|
21
|
-
date: 2011-
|
21
|
+
date: 2011-07-08 00:00:00 Z
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|
24
24
|
name: activerecord
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- lib/composite_primary_keys/composite_predicates.rb
|
74
74
|
- lib/composite_primary_keys/connection_adapters/abstract_adapter.rb
|
75
75
|
- lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb
|
76
|
+
- lib/composite_primary_keys/dirty.rb
|
76
77
|
- lib/composite_primary_keys/fixtures.rb
|
77
78
|
- lib/composite_primary_keys/persistence.rb
|
78
79
|
- lib/composite_primary_keys/relation/calculations.rb
|