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.
@@ -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
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 6
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -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
- #I'm also having problems when I use composite primary keys together with eager loading of associations. Here I'm doing
83
- #ArtistName.find(:all, :include => :artist, ...)
84
- # => ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'artists_names.artist_id,name' in 'field list': SELECT artists_names.`artist_id,name` AS t0_r0, ....
85
- # Had a brief look into it but couldn't spot the code causing this...
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
@@ -1,2 +1,7 @@
1
1
  DROP TABLE reference_codes;
2
2
  DROP TABLE reference_types;
3
+ DROP TABLE products;
4
+ DROP TABLE tariffs;
5
+ DROP TABLE product_tariffs;
6
+ DROP TABLE streets;
7
+ DROP TABLE suburbs;
@@ -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
+
@@ -0,0 +1,3 @@
1
+ class Street < ActiveRecord::Base
2
+ belongs_to :suburb, :foreign_key => [:city_id, :suburb_id]
3
+ end
@@ -0,0 +1,5 @@
1
+ first:
2
+ id: 1
3
+ city_id: 1
4
+ suburb_id: 1
5
+ name: First Street
@@ -0,0 +1,4 @@
1
+ class Suburb < ActiveRecord::Base
2
+ set_primary_keys :city_id, :suburb_id
3
+ has_many :streets, :foreign_key => [:city_id, :suburb_id]
4
+ end
@@ -0,0 +1,4 @@
1
+ first:
2
+ city_id: 1
3
+ suburb_id: 1
4
+ name: First Suburb
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.0
7
- date: 2006-08-02 00:00:00 +02:00
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: []