composite_primary_keys 3.0.3 → 3.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,4 +1,10 @@
1
- == 3.0.1 2010-11-19
1
+ == 3.0.4 2010-11-21
2
+ * Support deleting records when dependent is set to :delete_all
3
+ * Switch the method #id to return a CompositeKeys instance (thus going back to how
4
+ this was done in previous CPK versions)
5
+
6
+
7
+ == 3.0.3 2010-11-19
2
8
  * Support ActiveRecord 3.0.3 and Arel 2+
3
9
  * Require ActiveRecord 3.0.3 as minimum dependency
4
10
  * Fix double quoting issue with table names - thanks to Kevin Motschiedler for a patch.
@@ -96,7 +96,7 @@ module ActiveRecord
96
96
  # whether you name it the default 'id' or set it to something else.
97
97
  def id
98
98
  attr_names = self.class.primary_keys
99
- Array.new(attr_names.map { |attr_name| read_attribute(attr_name) })
99
+ ::CompositePrimaryKeys::CompositeKeys.new(attr_names.map { |attr_name| read_attribute(attr_name) })
100
100
  end
101
101
  alias_method :ids, :id
102
102
 
@@ -40,23 +40,35 @@ module CompositePrimaryKeys
40
40
  end
41
41
 
42
42
  def exists?(id = nil)
43
+ # ID can be:
44
+ # Array - ['department_id = ? and location_id = ?', 1, 1]
45
+ # Array -> [1,2]
46
+ # CompositeKeys -> [1,2]
47
+
48
+ id = id.id if ::ActiveRecord::Base === id
49
+
43
50
  case id
51
+ # CPK
52
+ when CompositePrimaryKeys::CompositeKeys
53
+ relation = select(primary_keys).limit(1)
54
+ relation = relation.where(ids_predicate(id)) if id
55
+ relation.first ? true : false
44
56
  when Array
45
57
  # CPK
46
58
  if id.first.is_a?(String) and id.first.match(/\?/)
47
59
  where(id).exists?
48
60
  else
49
- where(ids_predicate(id)).exists?
61
+ exists?(id.to_composite_keys)
50
62
  end
51
63
  when Hash
52
64
  where(id).exists?
53
65
  else
54
- # CPK
55
- #relation = select(primary_key).limit(1)
56
- #relation = relation.where(primary_key.eq(id)) if id
66
+ # CPK
67
+ #relation = select(primary_key).limit(1)
68
+ #relation = relation.where(primary_key.eq(id)) if id
69
+ relation = select(primary_keys).limit(1)
70
+ relation = relation.where(ids_predicate(id)) if id
57
71
 
58
- relation = select(primary_keys).limit(1)
59
- relation = relation.where(ids_predicate(id)) if id
60
72
  relation.first ? true : false
61
73
  end
62
74
  end
@@ -4,9 +4,16 @@ module CompositePrimaryKeys
4
4
  module InstanceMethods
5
5
  def ids_predicate(id)
6
6
  predicate = nil
7
- self.primary_keys.zip(id).each do |key, value|
8
- eq = table[key].eq(value)
9
- predicate = predicate ? predicate.and(eq) : eq
7
+
8
+ if id.kind_of?(CompositePrimaryKeys::CompositeKeys)
9
+ id = [id]
10
+ end
11
+
12
+ id.each do |composite_id|
13
+ self.primary_keys.zip(composite_id).each do |key, value|
14
+ eq = table[key].eq(value)
15
+ predicate = predicate ? predicate.and(eq) : eq
16
+ end
10
17
  end
11
18
  predicate
12
19
  end
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
4
  MINOR = 0
5
- TINY = 3
5
+ TINY = 4
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
data/test/debug.log CHANGED
@@ -285,3 +285,168 @@ HINT: No operator matches the given name and argument type(s). You might need t
285
285
  SQL (0.0ms) SET standard_conforming_strings = on
286
286
  SQL (0.0ms) SET client_min_messages TO 'notice'
287
287
  SQL (0.0ms) SHOW TIME ZONE
288
+ SQL (29.0ms) SHOW client_min_messages
289
+ SQL (0.0ms) SET client_min_messages TO 'panic'
290
+ SQL (0.0ms) SET standard_conforming_strings = on
291
+ SQL (1.0ms) SET client_min_messages TO 'notice'
292
+ SQL (0.0ms) SHOW TIME ZONE
293
+ PGError: ERROR: null value in column "product_id" violates not-null constraint
294
+ : UPDATE "product_tariffs" SET "product_id" = NULL WHERE "product_tariffs"."product_id" = 1 AND "product_tariffs"."product_id" = 1 AND "product_tariffs"."tariff_id" = 2 AND "product_tariffs"."tariff_start_date" = '2010-11-20'
295
+ SQL (1.0ms) SHOW client_min_messages
296
+ SQL (0.0ms) SET client_min_messages TO 'panic'
297
+ SQL (0.0ms) SET standard_conforming_strings = on
298
+ SQL (0.0ms) SET client_min_messages TO 'notice'
299
+ SQL (0.0ms) SHOW TIME ZONE
300
+ SQL (1.0ms) SHOW client_min_messages
301
+ SQL (0.0ms) SET client_min_messages TO 'panic'
302
+ SQL (1.0ms) SET standard_conforming_strings = on
303
+ SQL (0.0ms) SET client_min_messages TO 'notice'
304
+ SQL (0.0ms) SHOW TIME ZONE
305
+ SQL (0.0ms) SHOW client_min_messages
306
+ SQL (0.0ms) SET client_min_messages TO 'panic'
307
+ SQL (0.0ms) SET standard_conforming_strings = on
308
+ SQL (15.6ms) SET client_min_messages TO 'notice'
309
+ SQL (0.0ms) SHOW TIME ZONE
310
+ SQL (0.0ms) SHOW client_min_messages
311
+ SQL (0.0ms) SET client_min_messages TO 'panic'
312
+ SQL (0.0ms) SET standard_conforming_strings = on
313
+ SQL (0.0ms) SET client_min_messages TO 'notice'
314
+ SQL (0.0ms) SHOW TIME ZONE
315
+ SQL (0.0ms) SHOW client_min_messages
316
+ SQL (0.0ms) SET client_min_messages TO 'panic'
317
+ SQL (0.0ms) SET standard_conforming_strings = on
318
+ SQL (0.0ms) SET client_min_messages TO 'notice'
319
+ SQL (0.0ms) SHOW TIME ZONE
320
+ SQL (0.0ms) SHOW client_min_messages
321
+ SQL (0.0ms) SET client_min_messages TO 'panic'
322
+ SQL (0.0ms) SET standard_conforming_strings = on
323
+ SQL (0.0ms) SET client_min_messages TO 'notice'
324
+ SQL (0.0ms) SHOW TIME ZONE
325
+ SQL (0.0ms) SHOW client_min_messages
326
+ SQL (0.0ms) SET client_min_messages TO 'panic'
327
+ SQL (0.0ms) SET standard_conforming_strings = on
328
+ SQL (0.0ms) SET client_min_messages TO 'notice'
329
+ SQL (0.0ms) SHOW TIME ZONE
330
+ SQL (0.0ms) SHOW client_min_messages
331
+ SQL (0.0ms) SET client_min_messages TO 'panic'
332
+ SQL (0.0ms) SET standard_conforming_strings = on
333
+ SQL (0.0ms) SET client_min_messages TO 'notice'
334
+ SQL (0.0ms) SHOW TIME ZONE
335
+ SQL (0.0ms) SHOW client_min_messages
336
+ SQL (0.0ms) SET client_min_messages TO 'panic'
337
+ SQL (0.0ms) SET standard_conforming_strings = on
338
+ SQL (0.0ms) SET client_min_messages TO 'notice'
339
+ SQL (0.0ms) SHOW TIME ZONE
340
+ SQL (0.0ms) SHOW client_min_messages
341
+ SQL (0.0ms) SET client_min_messages TO 'panic'
342
+ SQL (0.0ms) SET standard_conforming_strings = on
343
+ SQL (0.0ms) SET client_min_messages TO 'notice'
344
+ SQL (0.0ms) SHOW TIME ZONE
345
+ SQL (0.0ms) SHOW client_min_messages
346
+ SQL (0.0ms) SET client_min_messages TO 'panic'
347
+ SQL (0.0ms) SET standard_conforming_strings = on
348
+ SQL (0.0ms) SET client_min_messages TO 'notice'
349
+ SQL (0.0ms) SHOW TIME ZONE
350
+ SQL (0.0ms) SHOW client_min_messages
351
+ SQL (0.0ms) SET client_min_messages TO 'panic'
352
+ SQL (0.0ms) SET standard_conforming_strings = on
353
+ SQL (0.0ms) SET client_min_messages TO 'notice'
354
+ SQL (15.6ms) SHOW TIME ZONE
355
+ SQL (0.0ms) SHOW client_min_messages
356
+ SQL (0.0ms) SET client_min_messages TO 'panic'
357
+ SQL (0.0ms) SET standard_conforming_strings = on
358
+ SQL (0.0ms) SET client_min_messages TO 'notice'
359
+ SQL (0.0ms) SHOW TIME ZONE
360
+ SQL (0.0ms) SHOW client_min_messages
361
+ SQL (0.0ms) SET client_min_messages TO 'panic'
362
+ SQL (0.0ms) SET standard_conforming_strings = on
363
+ SQL (0.0ms) SET client_min_messages TO 'notice'
364
+ SQL (0.0ms) SHOW TIME ZONE
365
+ SQL (0.0ms) SHOW client_min_messages
366
+ SQL (0.0ms) SET client_min_messages TO 'panic'
367
+ SQL (0.0ms) SET standard_conforming_strings = on
368
+ SQL (0.0ms) SET client_min_messages TO 'notice'
369
+ SQL (0.0ms) SHOW TIME ZONE
370
+ SQL (0.0ms) SHOW client_min_messages
371
+ SQL (0.0ms) SET client_min_messages TO 'panic'
372
+ SQL (0.0ms) SET standard_conforming_strings = on
373
+ SQL (0.0ms) SET client_min_messages TO 'notice'
374
+ SQL (0.0ms) SHOW TIME ZONE
375
+ SQL (0.0ms) SHOW client_min_messages
376
+ SQL (0.0ms) SET client_min_messages TO 'panic'
377
+ SQL (0.0ms) SET standard_conforming_strings = on
378
+ SQL (0.0ms) SET client_min_messages TO 'notice'
379
+ SQL (0.0ms) SHOW TIME ZONE
380
+ SQL (0.0ms) SHOW client_min_messages
381
+ SQL (0.0ms) SET client_min_messages TO 'panic'
382
+ SQL (0.0ms) SET standard_conforming_strings = on
383
+ SQL (0.0ms) SET client_min_messages TO 'notice'
384
+ SQL (0.0ms) SHOW TIME ZONE
385
+ PGError: ERROR: column "tariff_idstart_date" does not exist
386
+ LINE 1: SELECT COUNT(DISTINCT tariff_idstart_date) FROM "tariffs" LE...
387
+ ^
388
+ : SELECT COUNT(DISTINCT tariff_idstart_date) FROM "tariffs" LEFT OUTER JOIN "product_tariffs" ON "product_tariffs"."tariff_id" = "tariffs"."tariff_id" AND "product_tariffs"."tariff_start_date" = "tariffs"."start_date"
389
+ PGError: ERROR: operator does not exist: character varying = integer
390
+ LINE 1: ...ments".person_type = 'User') AND ("comments".person_id = 1))
391
+ ^
392
+ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
393
+ : SELECT "hacks".* FROM "hacks" INNER JOIN "comments" ON ("hacks"."name" = "comments"."hack_id") WHERE (("comments".person_type = 'User') AND ("comments".person_id = 1))
394
+ SQL (0.0ms) SHOW client_min_messages
395
+ SQL (0.0ms) SET client_min_messages TO 'panic'
396
+ SQL (0.0ms) SET standard_conforming_strings = on
397
+ SQL (0.0ms) SET client_min_messages TO 'notice'
398
+ SQL (0.0ms) SHOW TIME ZONE
399
+ SQL (0.0ms) SHOW client_min_messages
400
+ SQL (0.0ms) SET client_min_messages TO 'panic'
401
+ SQL (0.0ms) SET standard_conforming_strings = on
402
+ SQL (0.0ms) SET client_min_messages TO 'notice'
403
+ SQL (0.0ms) SHOW TIME ZONE
404
+ SQL (0.0ms) SHOW client_min_messages
405
+ SQL (0.0ms) SET client_min_messages TO 'panic'
406
+ SQL (0.0ms) SET standard_conforming_strings = on
407
+ SQL (0.0ms) SET client_min_messages TO 'notice'
408
+ SQL (0.0ms) SHOW TIME ZONE
409
+ SQL (0.0ms) SHOW client_min_messages
410
+ SQL (0.0ms) SET client_min_messages TO 'panic'
411
+ SQL (0.0ms) SET standard_conforming_strings = on
412
+ SQL (0.0ms) SET client_min_messages TO 'notice'
413
+ SQL (0.0ms) SHOW TIME ZONE
414
+ SQL (0.0ms) SHOW client_min_messages
415
+ SQL (0.0ms) SET client_min_messages TO 'panic'
416
+ SQL (0.0ms) SET standard_conforming_strings = on
417
+ SQL (0.0ms) SET client_min_messages TO 'notice'
418
+ SQL (0.0ms) SHOW TIME ZONE
419
+ SQL (0.0ms) SHOW client_min_messages
420
+ SQL (0.0ms) SET client_min_messages TO 'panic'
421
+ SQL (0.0ms) SET standard_conforming_strings = on
422
+ SQL (0.0ms) SET client_min_messages TO 'notice'
423
+ SQL (0.0ms) SHOW TIME ZONE
424
+ SQL (0.0ms) SHOW client_min_messages
425
+ SQL (0.0ms) SET client_min_messages TO 'panic'
426
+ SQL (15.6ms) SET standard_conforming_strings = on
427
+ SQL (0.0ms) SET client_min_messages TO 'notice'
428
+ SQL (0.0ms) SHOW TIME ZONE
429
+ SQL (0.0ms) SHOW client_min_messages
430
+ SQL (0.0ms) SET client_min_messages TO 'panic'
431
+ SQL (0.0ms) SET standard_conforming_strings = on
432
+ SQL (0.0ms) SET client_min_messages TO 'notice'
433
+ SQL (0.0ms) SHOW TIME ZONE
434
+ PGError: ERROR: column "tariff_idstart_date" does not exist
435
+ LINE 1: SELECT COUNT(DISTINCT tariff_idstart_date) FROM "tariffs" LE...
436
+ ^
437
+ : SELECT COUNT(DISTINCT tariff_idstart_date) FROM "tariffs" LEFT OUTER JOIN "product_tariffs" ON "product_tariffs"."tariff_id" = "tariffs"."tariff_id" AND "product_tariffs"."tariff_start_date" = "tariffs"."start_date"
438
+ PGError: ERROR: operator does not exist: character varying = integer
439
+ LINE 1: ...ments".person_type = 'User') AND ("comments".person_id = 1))
440
+ ^
441
+ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
442
+ : SELECT "hacks".* FROM "hacks" INNER JOIN "comments" ON ("hacks"."name" = "comments"."hack_id") WHERE (("comments".person_type = 'User') AND ("comments".person_id = 1))
443
+ SQL (0.0ms) SHOW client_min_messages
444
+ SQL (0.0ms) SET client_min_messages TO 'panic'
445
+ SQL (0.0ms) SET standard_conforming_strings = on
446
+ SQL (0.0ms) SET client_min_messages TO 'notice'
447
+ SQL (0.0ms) SHOW TIME ZONE
448
+ SQL (0.0ms) SHOW client_min_messages
449
+ SQL (0.0ms) SET client_min_messages TO 'panic'
450
+ SQL (0.0ms) SET standard_conforming_strings = on
451
+ SQL (0.0ms) SET client_min_messages TO 'notice'
452
+ SQL (0.0ms) SHOW TIME ZONE
@@ -1,6 +1,6 @@
1
1
  class Product < ActiveRecord::Base
2
2
  set_primary_keys :id # redundant
3
- has_many :product_tariffs, :foreign_key => :product_id
3
+ has_many :product_tariffs, :foreign_key => :product_id, :dependent => :delete_all
4
4
  has_one :product_tariff, :foreign_key => :product_id
5
5
 
6
6
  has_many :tariffs, :through => :product_tariffs, :foreign_key => [:tariff_id, :tariff_start_date]
data/test/test_delete.rb CHANGED
@@ -27,26 +27,26 @@ class TestDelete < ActiveSupport::TestCase
27
27
  assert @first.destroy
28
28
  end
29
29
  end
30
-
30
+
31
31
  def test_destroy_one_alone_via_class
32
32
  testing_with do
33
33
  assert @klass.destroy(@first.id)
34
34
  end
35
35
  end
36
-
36
+
37
37
  def test_delete_one_alone
38
38
  testing_with do
39
39
  assert @klass.delete(@first.id)
40
40
  end
41
41
  end
42
-
42
+
43
43
  def test_delete_many
44
44
  testing_with do
45
45
  to_delete = @klass.find(:all)[0..1]
46
46
  assert_equal 2, to_delete.length
47
47
  end
48
48
  end
49
-
49
+
50
50
  def test_delete_all
51
51
  testing_with do
52
52
  @klass.delete_all
@@ -63,21 +63,34 @@ class TestDelete < ActiveSupport::TestCase
63
63
  end
64
64
 
65
65
  def test_delete_association
66
- department = Department.find(1,1)
67
- assert_equal 2, department.employees.size , "Before delete employee count should be 2."
68
- first_employee = department.employees[0]
69
- department.employees.delete(first_employee)
70
- assert_equal 1, department.employees.size, "After delete employee count should be 1."
71
- department.reload
72
- assert_equal 1, department.employees.size, "After delete and a reload from DB employee count should be 1."
66
+ department = Department.find(1,1)
67
+ assert_equal 2, department.employees.size , "Before delete employee count should be 2."
68
+ first_employee = department.employees[0]
69
+ department.employees.delete(first_employee)
70
+ assert_equal 1, department.employees.size, "After delete employee count should be 1."
71
+ department.reload
72
+ assert_equal 1, department.employees.size, "After delete and a reload from DB employee count should be 1."
73
+ end
74
+
75
+ def test_delete_cpk_all_association
76
+ # In this case the association is a has_many composite key with
77
+ # dependent set to :delete_all
78
+ product = Product.find(1)
79
+ assert_equal(2, product.product_tariffs.length)
80
+
81
+ product_tariff = product.product_tariffs.first
82
+ product.product_tariffs.delete(product_tariff)
83
+
84
+ product.reload
85
+ assert_equal(1, product.product_tariffs.length)
73
86
  end
74
87
 
75
88
  def test_delete_records_for_has_many_association_with_composite_primary_key
76
- reference_type = ReferenceType.find(1)
77
- codes_to_delete = reference_type.reference_codes[0..1]
78
- assert_equal 3, reference_type.reference_codes.size, "Before deleting records reference_code count should be 3."
79
- reference_type.reference_codes.delete_records(codes_to_delete)
80
- reference_type.reload
81
- assert_equal 1, reference_type.reference_codes.size, "After deleting 2 records and a reload from DB reference_code count should be 1."
89
+ reference_type = ReferenceType.find(1)
90
+ codes_to_delete = reference_type.reference_codes[0..1]
91
+ assert_equal 3, reference_type.reference_codes.size, "Before deleting records reference_code count should be 3."
92
+ reference_type.reference_codes.delete_records(codes_to_delete)
93
+ reference_type.reload
94
+ assert_equal 1, reference_type.reference_codes.size, "After deleting 2 records and a reload from DB reference_code count should be 1."
82
95
  end
83
96
  end
data/test/test_ids.rb CHANGED
@@ -27,6 +27,7 @@ class TestIds < ActiveSupport::TestCase
27
27
  def test_id
28
28
  testing_with do
29
29
  assert_equal @first.id, @first.ids if composite?
30
+ assert_kind_of(CompositePrimaryKeys::CompositeKeys, @first.id) if composite?
30
31
  end
31
32
  end
32
33
 
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: 1
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 3
8
8
  - 0
9
- - 3
10
- version: 3.0.3
9
+ - 4
10
+ version: 3.0.4
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: 2010-11-20 00:00:00 -07:00
19
+ date: 2010-11-21 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency