composite_primary_keys 0.6.0 → 0.6.1
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/lib/composite_primary_keys/base.rb +38 -30
- data/lib/composite_primary_keys/version.rb +1 -1
- data/test/associations_test.rb +10 -5
- data/test/fixtures/db_definitions/mysql.drop.sql +5 -0
- data/test/fixtures/db_definitions/mysql.sql +15 -0
- data/test/fixtures/street.rb +3 -0
- data/test/fixtures/streets.yml +5 -0
- data/test/fixtures/suburb.rb +4 -0
- data/test/fixtures/suburbs.yml +4 -0
- metadata +6 -2
@@ -87,36 +87,6 @@ module CompositePrimaryKeys
|
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
|
-
# Allows +attr_name+ to be the list of primary_keys, and returns the id
|
91
|
-
# of the object
|
92
|
-
# e.g. @object[@object.class.primary_key] => [1,1]
|
93
|
-
def [](attr_name)
|
94
|
-
if attr_name.is_a?(String) and attr_name != attr_name.split(ID_SEP).first
|
95
|
-
attr_name = attr_name.split(ID_SEP)
|
96
|
-
end
|
97
|
-
attr_name.is_a?(Array) ?
|
98
|
-
attr_name.map {|name| read_attribute(name)} :
|
99
|
-
read_attribute(attr_name)
|
100
|
-
end
|
101
|
-
|
102
|
-
# Updates the attribute identified by <tt>attr_name</tt> with the specified +value+.
|
103
|
-
# (Alias for the protected write_attribute method).
|
104
|
-
def []=(attr_name, value)
|
105
|
-
if attr_name.is_a?(String) and attr_name != attr_name.split(ID_SEP).first
|
106
|
-
attr_name = attr_name.split(ID_SEP)
|
107
|
-
end
|
108
|
-
if attr_name.is_a? Array
|
109
|
-
value = value.split(ID_SEP) if value.is_a? String
|
110
|
-
unless value.length == attr_name.length
|
111
|
-
raise "Number of attr_names and values do not match"
|
112
|
-
end
|
113
|
-
#breakpoint
|
114
|
-
[attr_name, value].transpose.map {|name,val| write_attribute(name.to_s, val)}
|
115
|
-
else
|
116
|
-
write_attribute(attr_name, value)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
90
|
# Define an attribute reader method. Cope with nil column.
|
121
91
|
def define_read_method(symbol, attr_name, column)
|
122
92
|
cast_code = column.type_cast_code('v') if column
|
@@ -323,3 +293,41 @@ module CompositePrimaryKeys
|
|
323
293
|
end
|
324
294
|
end
|
325
295
|
end
|
296
|
+
|
297
|
+
module ActiveRecord
|
298
|
+
ID_SEP = ','
|
299
|
+
ID_SET_SEP = ';'
|
300
|
+
|
301
|
+
class Base
|
302
|
+
# Allows +attr_name+ to be the list of primary_keys, and returns the id
|
303
|
+
# of the object
|
304
|
+
# e.g. @object[@object.class.primary_key] => [1,1]
|
305
|
+
def [](attr_name)
|
306
|
+
if attr_name.is_a?(String) and attr_name != attr_name.split(ID_SEP).first
|
307
|
+
attr_name = attr_name.split(ID_SEP)
|
308
|
+
end
|
309
|
+
attr_name.is_a?(Array) ?
|
310
|
+
attr_name.map {|name| read_attribute(name)} :
|
311
|
+
read_attribute(attr_name)
|
312
|
+
end
|
313
|
+
|
314
|
+
# Updates the attribute identified by <tt>attr_name</tt> with the specified +value+.
|
315
|
+
# (Alias for the protected write_attribute method).
|
316
|
+
def []=(attr_name, value)
|
317
|
+
if attr_name.is_a?(String) and attr_name != attr_name.split(ID_SEP).first
|
318
|
+
attr_name = attr_name.split(ID_SEP)
|
319
|
+
end
|
320
|
+
if attr_name.is_a? Array
|
321
|
+
value = value.split(ID_SEP) if value.is_a? String
|
322
|
+
unless value.length == attr_name.length
|
323
|
+
raise "Number of attr_names and values do not match"
|
324
|
+
end
|
325
|
+
#breakpoint
|
326
|
+
[attr_name, value].transpose.map {|name,val| write_attribute(name.to_s, val)}
|
327
|
+
else
|
328
|
+
write_attribute(attr_name, value)
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
end
|
333
|
+
end
|
data/test/associations_test.rb
CHANGED
@@ -2,9 +2,11 @@ require 'abstract_unit'
|
|
2
2
|
require 'fixtures/product'
|
3
3
|
require 'fixtures/tariff'
|
4
4
|
require 'fixtures/product_tariff'
|
5
|
+
require 'fixtures/suburb'
|
6
|
+
require 'fixtures/street'
|
5
7
|
|
6
8
|
class AssociationTest < Test::Unit::TestCase
|
7
|
-
fixtures :products, :tariffs, :product_tariffs
|
9
|
+
fixtures :products, :tariffs, :product_tariffs, :suburbs, :streets
|
8
10
|
|
9
11
|
def setup
|
10
12
|
super
|
@@ -79,8 +81,11 @@ class AssociationTest < Test::Unit::TestCase
|
|
79
81
|
|
80
82
|
end
|
81
83
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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
|
86
91
|
end
|
@@ -35,3 +35,18 @@ CREATE TABLE `product_tariffs` (
|
|
35
35
|
PRIMARY KEY (`product_id`,`tariff_id`,`tariff_start_date`)
|
36
36
|
) TYPE=InnoDB;
|
37
37
|
|
38
|
+
CREATE TABLE `suburbs` (
|
39
|
+
`city_id` int(11) NOT NULL,
|
40
|
+
`suburb_id` int(11) NOT NULL,
|
41
|
+
`name` varchar(50) NOT NULL,
|
42
|
+
PRIMARY KEY (`city_id`,`suburb_id`)
|
43
|
+
) TYPE=InnoDB;
|
44
|
+
|
45
|
+
CREATE TABLE `streets` (
|
46
|
+
`id` int(11) NOT NULL auto_increment,
|
47
|
+
`city_id` int(11) NOT NULL,
|
48
|
+
`suburb_id` int(11) NOT NULL,
|
49
|
+
`name` varchar(50) NOT NULL,
|
50
|
+
PRIMARY KEY (`id`)
|
51
|
+
) TYPE=InnoDB;
|
52
|
+
|
metadata
CHANGED
@@ -3,8 +3,8 @@ 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.
|
7
|
-
date: 2006-08-
|
6
|
+
version: 0.6.1
|
7
|
+
date: 2006-08-03 00:00:00 +02:00
|
8
8
|
summary: Support for composite primary keys in ActiveRecords
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -69,6 +69,10 @@ files:
|
|
69
69
|
- test/fixtures/products.yml
|
70
70
|
- test/fixtures/tariffs.yml
|
71
71
|
- test/fixtures/product_tariffs.yml
|
72
|
+
- test/fixtures/street.rb
|
73
|
+
- test/fixtures/suburb.rb
|
74
|
+
- test/fixtures/streets.yml
|
75
|
+
- test/fixtures/suburbs.yml
|
72
76
|
- test/fixtures/db_definitions/mysql.drop.sql
|
73
77
|
- test/fixtures/db_definitions/mysql.sql
|
74
78
|
test_files: []
|