composite_primary_keys 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -206,16 +206,19 @@ module ActiveRecord::Associations
206
206
  def full_keys(table_name, keys)
207
207
  keys = keys.split(CompositePrimaryKeys::ID_SEP) if keys.is_a?(String)
208
208
  keys.is_a?(Array) ?
209
- keys.collect {|key| "#{table_name}.#{key}"}.join(CompositePrimaryKeys::ID_SEP) :
209
+ keys.collect {|key| "#{table_name}.#{key}"}.join(CompositePrimaryKeys::ID_SEP) :
210
210
  "#{table_name}.#{keys}"
211
211
  end
212
212
 
213
213
  def full_columns_equals(table_name, keys, quoted_ids)
214
+ if keys.is_a?(Symbol) or (keys.is_a?(String) and keys == keys.split(CompositePrimaryKeys::ID_SEP))
215
+ return "#{table_name}.#{keys} = #{quoted_ids}"
216
+ end
214
217
  keys = keys.split(CompositePrimaryKeys::ID_SEP) if keys.is_a?(String)
215
- keys_ids = [keys, quoted_ids]
216
- keys_ids = keys.is_a?(Symbol) ? [keys_ids] : keys_ids.transpose
218
+ quoted_ids = quoted_ids.split(CompositePrimaryKeys::ID_SEP) if quoted_ids.is_a?(String)
219
+ keys_ids = [keys, quoted_ids].transpose
217
220
  keys_ids.collect {|key, id| "(#{table_name}.#{key} = #{id})"}.join(' AND ')
218
- end
221
+ end
219
222
  end
220
223
 
221
224
  class HasManyAssociation < AssociationCollection #:nodoc:
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 6
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -81,11 +81,4 @@ class AssociationTest < Test::Unit::TestCase
81
81
 
82
82
  end
83
83
 
84
- def test_santiago
85
- assert_not_nil @suburb = Suburb.find(1,1)
86
- assert_equal 1, @suburb.streets.length
87
-
88
- assert_not_nil @street = Street.find(1)
89
- assert_not_nil @street.suburb
90
- end
91
84
  end
@@ -11,7 +11,7 @@ class AttributesTest < Test::Unit::TestCase
11
11
  CLASSES = {
12
12
  :single => {
13
13
  :class => ReferenceType,
14
- :primary_keys => [:reference_type_id],
14
+ :primary_keys => :reference_type_id,
15
15
  },
16
16
  :dual => {
17
17
  :class => ReferenceCode,
@@ -34,7 +34,7 @@ class AttributesTest < Test::Unit::TestCase
34
34
 
35
35
  def test_brackets_primary_key
36
36
  testing_with do
37
- assert_equal @first.id, @first[@primary_keys]
37
+ assert_equal @first.id, @first[@primary_keys], "[] failing for #{@klass}"
38
38
  assert_equal @first.id, @first[@first.class.primary_key]
39
39
  end
40
40
  end
data/test/clone_test.rb CHANGED
@@ -8,7 +8,7 @@ class CloneTest < Test::Unit::TestCase
8
8
  CLASSES = {
9
9
  :single => {
10
10
  :class => ReferenceType,
11
- :primary_keys => [:reference_type_id],
11
+ :primary_keys => :reference_type_id,
12
12
  },
13
13
  :dual => {
14
14
  :class => ReferenceCode,
data/test/create_test.rb CHANGED
@@ -8,7 +8,7 @@ class DummyTest < Test::Unit::TestCase
8
8
  CLASSES = {
9
9
  :single => {
10
10
  :class => ReferenceType,
11
- :primary_keys => [:reference_type_id],
11
+ :primary_keys => :reference_type_id,
12
12
  :create => {:reference_type_id => 10, :type_label => 'NEW_TYPE', :abbreviation => 'New Type'}
13
13
  },
14
14
  :dual => {
data/test/delete_test.rb CHANGED
@@ -8,7 +8,7 @@ class DeleteTest < Test::Unit::TestCase
8
8
  CLASSES = {
9
9
  :single => {
10
10
  :class => ReferenceType,
11
- :primary_keys => [:reference_type_id],
11
+ :primary_keys => :reference_type_id,
12
12
  },
13
13
  :dual => {
14
14
  :class => ReferenceCode,
data/test/dummy_test.rb CHANGED
@@ -8,7 +8,7 @@ class DummyTest < Test::Unit::TestCase
8
8
  CLASSES = {
9
9
  :single => {
10
10
  :class => ReferenceType,
11
- :primary_keys => [:reference_type_id],
11
+ :primary_keys => :reference_type_id,
12
12
  },
13
13
  :dual => {
14
14
  :class => ReferenceCode,
@@ -0,0 +1,5 @@
1
+ class Article < ActiveRecord::Base
2
+ has_many :readings
3
+ has_many :users, :through => :readings
4
+ end
5
+
@@ -0,0 +1,6 @@
1
+ first:
2
+ id: 1
3
+ name: Article One
4
+ second:
5
+ id: 2
6
+ name: Article Two
@@ -5,3 +5,6 @@ DROP TABLE tariffs;
5
5
  DROP TABLE product_tariffs;
6
6
  DROP TABLE streets;
7
7
  DROP TABLE suburbs;
8
+ DROP TABLE users;
9
+ DROP TABLE articles;
10
+ DROP TABLE readings;
@@ -50,3 +50,23 @@ CREATE TABLE `streets` (
50
50
  PRIMARY KEY (`id`)
51
51
  ) TYPE=InnoDB;
52
52
 
53
+ CREATE TABLE `users` (
54
+ `id` int(11) NOT NULL auto_increment,
55
+ `name` varchar(50) NOT NULL,
56
+ PRIMARY KEY (`id`)
57
+ ) TYPE=InnoDB;
58
+
59
+ CREATE TABLE `articles` (
60
+ `id` int(11) NOT NULL auto_increment,
61
+ `name` varchar(50) NOT NULL,
62
+ PRIMARY KEY (`id`)
63
+ ) TYPE=InnoDB;
64
+
65
+ CREATE TABLE `readings` (
66
+ `id` int(11) NOT NULL auto_increment,
67
+ `user_id` int(11) NOT NULL,
68
+ `article_id` int(11) NOT NULL,
69
+ `rating` int(11) NOT NULL,
70
+ PRIMARY KEY (`id`)
71
+ ) TYPE=InnoDB;
72
+
@@ -0,0 +1,4 @@
1
+ class Reading < ActiveRecord::Base
2
+ belongs_to :article
3
+ belongs_to :user
4
+ end
@@ -0,0 +1,10 @@
1
+ santiago_first:
2
+ id: 1
3
+ user_id: 1
4
+ article_id: 1
5
+ rating: 4
6
+ santiago_second:
7
+ id: 2
8
+ user_id: 1
9
+ article_id: 2
10
+ rating: 5
@@ -0,0 +1,5 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :readings
3
+ has_many :articles, :through => :readings
4
+ end
5
+
@@ -0,0 +1,6 @@
1
+ santiago:
2
+ id: 1
3
+ name: Santiago
4
+ drnic:
5
+ id: 2
6
+ name: Dr Nic
@@ -8,7 +8,7 @@ class MiscellaneousTest < Test::Unit::TestCase
8
8
  CLASSES = {
9
9
  :single => {
10
10
  :class => ReferenceType,
11
- :primary_keys => [:reference_type_id],
11
+ :primary_keys => :reference_type_id,
12
12
  },
13
13
  :dual => {
14
14
  :class => ReferenceCode,
@@ -11,7 +11,7 @@ class PaginationTest < Test::Unit::TestCase
11
11
  CLASSES = {
12
12
  :single => {
13
13
  :class => ReferenceType,
14
- :primary_keys => [:reference_type_id],
14
+ :primary_keys => :reference_type_id,
15
15
  :table => :reference_types,
16
16
  },
17
17
  :dual => {
@@ -0,0 +1,27 @@
1
+ # Test cases devised by Santiago that broke the Composite Primary Keys
2
+ # code at one point in time. But no more!!!
3
+
4
+ require 'abstract_unit'
5
+ require 'fixtures/user'
6
+ require 'fixtures/article'
7
+ require 'fixtures/reading'
8
+
9
+ class AssociationTest < Test::Unit::TestCase
10
+ fixtures :suburbs, :streets, :users, :articles, :readings
11
+
12
+ def test_normal_and_composite_associations
13
+ assert_not_nil @suburb = Suburb.find(1,1)
14
+ assert_equal 1, @suburb.streets.length
15
+
16
+ assert_not_nil @street = Street.find(1)
17
+ assert_not_nil @street.suburb
18
+ end
19
+
20
+ def test_single_keys
21
+ @santiago = users(:santiago)
22
+ assert_not_nil @santiago.articles
23
+ assert_equal 2, @santiago.articles.length
24
+ assert_not_nil @santiago.readings
25
+ assert_equal 2, @santiago.readings.length
26
+ end
27
+ end
data/test/update_test.rb CHANGED
@@ -8,7 +8,7 @@ class UpdateTest < Test::Unit::TestCase
8
8
  CLASSES = {
9
9
  :single => {
10
10
  :class => ReferenceType,
11
- :primary_keys => [:reference_type_id],
11
+ :primary_keys => :reference_type_id,
12
12
  :update => { :description => 'RT Desc' },
13
13
  },
14
14
  :dual => {
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: composite_primary_keys
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.6.1
6
+ version: 0.6.2
7
7
  date: 2006-08-03 00:00:00 +02:00
8
8
  summary: Support for composite primary keys in ActiveRecords
9
9
  require_paths:
@@ -56,6 +56,7 @@ files:
56
56
  - test/associations_test.rb
57
57
  - test/attributes_test.rb
58
58
  - test/create_test.rb
59
+ - test/santiago_test.rb
59
60
  - test/connections/native_mysql
60
61
  - test/connections/native_mysql/connection.rb
61
62
  - test/fixtures/reference_type.rb
@@ -73,6 +74,12 @@ files:
73
74
  - test/fixtures/suburb.rb
74
75
  - test/fixtures/streets.yml
75
76
  - test/fixtures/suburbs.yml
77
+ - test/fixtures/article.rb
78
+ - test/fixtures/user.rb
79
+ - test/fixtures/reading.rb
80
+ - test/fixtures/articles.yml
81
+ - test/fixtures/users.yml
82
+ - test/fixtures/readings.yml
76
83
  - test/fixtures/db_definitions/mysql.drop.sql
77
84
  - test/fixtures/db_definitions/mysql.sql
78
85
  test_files: []