composite_primary_keys 3.1.5 → 3.1.6

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.
data/History.txt CHANGED
@@ -1,7 +1,14 @@
1
+ == 3.1.6 2011-04-03
2
+ * Updated belongs_to association to be a bit more flexible with non-CPK
3
+ base models (Jacques Fuentes)
4
+ * Fix uniqueness check (David Rueck)
5
+ * Fix write issue when one of they keys in a composite key is
6
+ called id (Tom Hughes)
7
+
1
8
  == 3.1.5 2011-03-24
2
9
  * Fix simple calculation methods
3
10
  * Fix instantiation of cpk records via associations.
4
- * Fix Relation#delete_all
11
+ * Fix Relation#delete
5
12
  * Fix Relation#destroy
6
13
 
7
14
  == 3.1.4 2011-03-06
@@ -62,6 +62,7 @@ require 'composite_primary_keys/persistence'
62
62
  require 'composite_primary_keys/reflection'
63
63
  require 'composite_primary_keys/relation'
64
64
  require 'composite_primary_keys/read'
65
+ require 'composite_primary_keys/write'
65
66
  require 'composite_primary_keys/finder_methods'
66
67
  require 'composite_primary_keys/base'
67
68
  require 'composite_primary_keys/calculations'
@@ -18,10 +18,11 @@ module ActiveRecord
18
18
  record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s
19
19
  else
20
20
  unless @owner.new_record?
21
- primary_key = @reflection.options[:primary_key] || :id
21
+ primary_keys = Array(@reflection.options[:primary_key] || :id)
22
22
  # CPK
23
23
  # record[@reflection.primary_key_name] = @owner.send(primary_key)
24
- values = [@owner.send(primary_key)].flatten
24
+ # Need to flatten because a key may be :id giving a composite key
25
+ values = primary_keys.map {|key| @owner.send(key)}.flatten
25
26
  key_values = @reflection.cpk_primary_key.zip(values)
26
27
  key_values.each {|key, value| record[key] = value}
27
28
  end
@@ -26,7 +26,7 @@ module ActiveRecord
26
26
  predicate = nil
27
27
  record.ids_hash.each do |key, value|
28
28
  neq = relation.table[key].not_eq(value)
29
- predicate = predicate ? predicate.and(neq) : neq
29
+ predicate = predicate ? predicate.or(neq) : neq
30
30
  end
31
31
  relation = relation.where(predicate)
32
32
  else
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
4
  MINOR = 1
5
- TINY = 5
5
+ TINY = 6
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -0,0 +1,18 @@
1
+ module ActiveRecord
2
+ module AttributeMethods
3
+ module Write
4
+ def write_attribute(attr_name, value)
5
+ attr_name = attr_name.to_s
6
+ # CPK
7
+ # attr_name = self.class.primary_key if attr_name == 'id'
8
+ attr_name = self.class.primary_key if (attr_name == 'id' and !self.composite?)
9
+ @attributes_cache.delete(attr_name)
10
+ if (column = column_for_attribute(attr_name)) && column.number?
11
+ @attributes[attr_name] = convert_number_column_value(value)
12
+ else
13
+ @attributes[attr_name] = value
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -111,3 +111,10 @@ create table restaurants_suburbs (
111
111
  city_id integer not null,
112
112
  suburb_id integer not null
113
113
  );
114
+
115
+ create table way_nodes (
116
+ id integer not null,
117
+ node_id integer not null,
118
+ sequence_id integer not null,
119
+ primary key (id, sequence_id)
120
+ );
@@ -14,3 +14,4 @@ drop table PRODUCT_TARIFFS;
14
14
  drop table KITCHEN_SINK;
15
15
  drop table RESTAURANTS;
16
16
  drop table RESTAURANTS_SUBURBS;
17
+ drop table WAY_NODES;
@@ -184,3 +184,10 @@ create table capitols (
184
184
  city varchar(100) default null,
185
185
  primary key (country, city)
186
186
  ) type=InnoDB;
187
+
188
+ create table way_nodes (
189
+ id int(11) not null,
190
+ node_id int(11) not null,
191
+ sequence_id int(11) not null,
192
+ primary key (id, sequence_id)
193
+ ) type=InnoDB;
@@ -37,3 +37,4 @@ drop table room_attribute_assignments;
37
37
  drop table room_assignments;
38
38
  drop table students;
39
39
  drop sequence students_seq;
40
+ drop table way_nodes;
@@ -186,3 +186,9 @@ create table room_assignments (
186
186
  room_id number(11) not null
187
187
  );
188
188
 
189
+ create table way_nodes (
190
+ id number(11) not null,
191
+ node_id number(11) not null,
192
+ sequence_id number(11) not null,
193
+ constraint way_nodes_pk primary key (id, sequence_id)
194
+ );
@@ -210,3 +210,10 @@ create table capitols (
210
210
  city text not null,
211
211
  primary key (country, city)
212
212
  );
213
+
214
+ create table way_nodes (
215
+ id int not null,
216
+ node_id int not null,
217
+ sequence_id int not null,
218
+ primary key (id, sequence_id)
219
+ );
@@ -170,3 +170,10 @@ create table capitols (
170
170
  city text not null,
171
171
  primary key (country, city)
172
172
  );
173
+
174
+ create table way_nodes (
175
+ id integer not null,
176
+ node_id integer not null,
177
+ sequence_id integer not null,
178
+ primary key (id, sequence_id)
179
+ );
@@ -2,3 +2,8 @@ seat1:
2
2
  flight_number: 1
3
3
  seat: 1
4
4
  customer: 1
5
+
6
+ seat2:
7
+ flight_number: 1
8
+ seat: 2
9
+ customer: 2
@@ -0,0 +1,3 @@
1
+ class WayNode < ActiveRecord::Base
2
+ set_primary_keys :id, :sequence_id
3
+ end
data/test/test_ids.rb CHANGED
@@ -8,27 +8,27 @@ class TestIds < ActiveSupport::TestCase
8
8
  :class => ReferenceType,
9
9
  :primary_keys => [:reference_type_id],
10
10
  },
11
- :dual => {
11
+ :dual => {
12
12
  :class => ReferenceCode,
13
13
  :primary_keys => [:reference_type_id, :reference_code],
14
14
  },
15
- :dual_strs => {
15
+ :dual_strs => {
16
16
  :class => ReferenceCode,
17
17
  :primary_keys => ['reference_type_id', 'reference_code'],
18
18
  },
19
19
  }
20
-
20
+
21
21
  def setup
22
22
  self.class.classes = CLASSES
23
23
  end
24
-
24
+
25
25
  def test_id
26
26
  testing_with do
27
27
  assert_equal @first.id, @first.ids if composite?
28
28
  assert_kind_of(CompositePrimaryKeys::CompositeKeys, @first.id) if composite?
29
29
  end
30
30
  end
31
-
31
+
32
32
  def test_ids_to_s
33
33
  testing_with do
34
34
  order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
@@ -37,7 +37,7 @@ class TestIds < ActiveSupport::TestCase
37
37
  assert_equal '1,1;1,2', @klass.ids_to_s(to_test, ',', ';', '', '') if @key_test == :dual
38
38
  end
39
39
  end
40
-
40
+
41
41
  def test_set_ids_string
42
42
  testing_with do
43
43
  array = @primary_keys.collect {|key| 5}
@@ -46,7 +46,7 @@ class TestIds < ActiveSupport::TestCase
46
46
  assert_equal expected, @first.id
47
47
  end
48
48
  end
49
-
49
+
50
50
  def test_set_ids_array
51
51
  testing_with do
52
52
  array = @primary_keys.collect {|key| 5}
@@ -55,7 +55,7 @@ class TestIds < ActiveSupport::TestCase
55
55
  assert_equal expected, @first.id
56
56
  end
57
57
  end
58
-
58
+
59
59
  def test_set_ids_comp
60
60
  testing_with do
61
61
  array = @primary_keys.collect {|key| 5}
@@ -64,7 +64,7 @@ class TestIds < ActiveSupport::TestCase
64
64
  assert_equal expected, @first.id
65
65
  end
66
66
  end
67
-
67
+
68
68
  def test_primary_keys
69
69
  testing_with do
70
70
  if composite?
@@ -79,4 +79,20 @@ class TestIds < ActiveSupport::TestCase
79
79
  end
80
80
  end
81
81
  end
82
- end
82
+
83
+ def test_assign_ids
84
+ ref_code = ReferenceCode.new
85
+ assert_equal([nil, nil], ref_code.id)
86
+
87
+ ref_code.id = [2,1]
88
+ assert_equal([2,1], ref_code.id)
89
+ end
90
+
91
+ def test_id_as_component
92
+ way_node = WayNode.new
93
+ assert_equal([nil, nil], way_node.id)
94
+
95
+ way_node.id = [2,1]
96
+ assert_equal([2,1], way_node.id)
97
+ end
98
+ end
@@ -3,8 +3,11 @@ require 'abstract_unit'
3
3
  class TestValidations < ActiveSupport::TestCase
4
4
  fixtures :seats
5
5
 
6
- def test_uniqueness_validation_on_saved_record
7
- s = Seat.find([1,1])
8
- assert s.valid?
6
+ def test_uniqueness_validation_persisted
7
+ seat = Seat.find([1,1])
8
+ assert(seat.valid?)
9
+
10
+ seat.customer = 2
11
+ assert(!seat.valid?)
9
12
  end
10
13
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 3
8
8
  - 1
9
- - 5
10
- version: 3.1.5
9
+ - 6
10
+ version: 3.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dr Nic Williams
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-03-24 00:00:00 -06:00
19
+ date: 2011-04-03 00:00:00 -06:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -94,6 +94,7 @@ files:
94
94
  - lib/composite_primary_keys/relation.rb
95
95
  - lib/composite_primary_keys/validations/uniqueness.rb
96
96
  - lib/composite_primary_keys/version.rb
97
+ - lib/composite_primary_keys/write.rb
97
98
  - lib/composite_primary_keys.rb
98
99
  - scripts/console.rb
99
100
  - scripts/txt2html
@@ -179,6 +180,7 @@ files:
179
180
  - test/fixtures/tariffs.yml
180
181
  - test/fixtures/user.rb
181
182
  - test/fixtures/users.yml
183
+ - test/fixtures/way_node.rb
182
184
  - test/hash_tricks.rb
183
185
  - test/plugins/pagination.rb
184
186
  - test/plugins/pagination_helper.rb