composite_primary_keys 0.9.91 → 0.9.92

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ == 0.9.92 2008-02-22
2
+
3
+ * Support for has_and_belongs_to_many
4
+
1
5
  == 0.9.91 2008-01-27
2
6
 
3
7
  * Incremented activerecord dependency to 2.0.2 [thx emmanuel.pirsch]
@@ -251,6 +251,15 @@ module ActiveRecord::Associations
251
251
  "(#{where_clause})"
252
252
  end
253
253
 
254
+ def full_composite_join_clause(table1, full_keys1, table2, full_keys2)
255
+ full_keys1 = full_keys1.split(CompositePrimaryKeys::ID_SEP) if full_keys1.is_a?(String)
256
+ full_keys2 = full_keys2.split(CompositePrimaryKeys::ID_SEP) if full_keys2.is_a?(String)
257
+ where_clause = [full_keys1, full_keys2].transpose.map do |key_pair|
258
+ "#{table1}.#{key_pair.first}=#{table2}.#{key_pair.last}"
259
+ end.join(" AND ")
260
+ "(#{where_clause})"
261
+ end
262
+
254
263
  def full_keys(table_name, keys)
255
264
  keys = keys.split(CompositePrimaryKeys::ID_SEP) if keys.is_a?(String)
256
265
  keys.is_a?(Array) ?
@@ -269,6 +278,25 @@ module ActiveRecord::Associations
269
278
  end
270
279
  end
271
280
 
281
+ class HasAndBelongsToManyAssociation < AssociationCollection #:nodoc:
282
+ def construct_sql
283
+ interpolate_sql_options!(@reflection.options, :finder_sql)
284
+
285
+ if @reflection.options[:finder_sql]
286
+ @finder_sql = @reflection.options[:finder_sql]
287
+ else
288
+ @finder_sql = full_columns_equals(
289
+ @reflection.options[:join_table],
290
+ @reflection.primary_key_name,
291
+ @owner.quoted_id)
292
+ @finder_sql << " AND (#{conditions})" if conditions
293
+ end
294
+
295
+ @join_sql = "INNER JOIN #{@reflection.options[:join_table]} ON " +
296
+ full_composite_join_clause(@reflection.klass.table_name, @reflection.klass.primary_key, @reflection.options[:join_table], @reflection.association_foreign_key)
297
+ end
298
+ end
299
+
272
300
  class HasManyAssociation < AssociationCollection #:nodoc:
273
301
  def construct_sql
274
302
  case
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 9
5
- TINY = 91
5
+ TINY = 92
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -97,3 +97,17 @@ create table kitchen_sinks (
97
97
  a_string varchar(100),
98
98
  primary key (id_1, id_2)
99
99
  );
100
+
101
+ create table restaurants (
102
+ franchise_id integer not null,
103
+ store_id integer not null,
104
+ name varchar(100),
105
+ primary key (franchise_id, store_id)
106
+ );
107
+
108
+ create table restaurants_suburbs (
109
+ franchise_id integer not null,
110
+ store_id integer not null,
111
+ city_id integer not null,
112
+ suburb_id integer not null
113
+ );
@@ -12,3 +12,5 @@ drop table USERS;
12
12
  drop table SUBURBS;
13
13
  drop table PRODUCT_TARIFFS;
14
14
  drop table KITCHEN_SINK;
15
+ drop table RESTAURANTS;
16
+ drop table RESTAURANTS_SUBURBS;
@@ -121,4 +121,18 @@ create table kitchen_sinks (
121
121
  a_date date,
122
122
  a_string varchar(100),
123
123
  primary key (id_1, id_2)
124
- ) TYPE=InnoDB
124
+ ) TYPE=InnoDB;
125
+
126
+ create table restaurants (
127
+ franchise_id int(11) not null,
128
+ store_id int(11) not null,
129
+ name varchar(100),
130
+ primary key (franchise_id, store_id)
131
+ ) TYPE=InnoDB;
132
+
133
+ create table restaurants_suburbs (
134
+ franchise_id int(11) not null,
135
+ store_id int(11) not null,
136
+ city_id int(11) not null,
137
+ suburb_id int(11) not null
138
+ ) TYPE=InnoDB;
@@ -25,4 +25,6 @@ drop sequence employees_seq;
25
25
  drop table comments;
26
26
  drop sequence comments_seq;
27
27
  drop table hacks;
28
- drop table kitchen_sinks;
28
+ drop table kitchen_sinks;
29
+ drop table restaurants;
30
+ drop table restaurants_suburbs;
@@ -143,4 +143,18 @@ create table kitchen_sinks (
143
143
  a_date date,
144
144
  a_string varchar(100),
145
145
  constraint kitchen_sinks_pk primary key(id_1, id_2)
146
+ );
147
+
148
+ create table restaurants (
149
+ franchise_id number(11) not null,
150
+ store_id number(11) not null,
151
+ name varchar(100),
152
+ constraint restaurants_pk primary key (franchise_id, store_id)
153
+ );
154
+
155
+ create table restaurants_suburbs (
156
+ franchise_id number(11) not null,
157
+ store_id number(11) not null,
158
+ city_id number(11) not null,
159
+ suburb_id number(11) not null
146
160
  );
@@ -110,4 +110,18 @@ create table kitchen_sinks (
110
110
  a_date date,
111
111
  a_string varchar(100),
112
112
  primary key (id_1, id_2)
113
+ );
114
+
115
+ create table restaurants (
116
+ franchise_id integer not null,
117
+ store_id integer not null,
118
+ name varchar(100),
119
+ primary key (franchise_id, store_id)
120
+ );
121
+
122
+ create table restaurants_suburbs (
123
+ franchise_id integer not null,
124
+ store_id integer not null,
125
+ city_id integer not null,
126
+ suburb_id integer not null
113
127
  );
@@ -4,9 +4,10 @@ require 'fixtures/tariff'
4
4
  require 'fixtures/product_tariff'
5
5
  require 'fixtures/suburb'
6
6
  require 'fixtures/street'
7
+ require 'fixtures/restaurant'
7
8
 
8
9
  class TestAssociations < Test::Unit::TestCase
9
- fixtures :products, :tariffs, :product_tariffs, :suburbs, :streets
10
+ fixtures :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants, :restaurants_suburbs
10
11
 
11
12
  def test_quoted_table_columns
12
13
  assert_equal "product_tariffs.product_id,product_tariffs.tariff_id,product_tariffs.tariff_start_date",
@@ -107,4 +108,12 @@ class TestAssociations < Test::Unit::TestCase
107
108
  @suburb = Suburb.find([2, 1], :include => :first_streets)
108
109
  assert_equal 1, @suburb.first_streets.size
109
110
  end
111
+
112
+ def test_has_and_belongs_to_many
113
+ @restaurant = Restaurant.find([1,1])
114
+ assert_equal 2, @restaurant.suburbs.size
115
+
116
+ @restaurant = Restaurant.find([1,1], :include => :suburbs)
117
+ assert_equal 2, @restaurant.suburbs.size
118
+ end
110
119
  end
Binary file
@@ -33,7 +33,7 @@
33
33
  <h1>Composite Primary Keys</h1>
34
34
  <div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/compositekeys"; return false'>
35
35
  Get Version
36
- <a href="http://rubyforge.org/projects/compositekeys" class="numbers">0.9.91</a>
36
+ <a href="http://rubyforge.org/projects/compositekeys" class="numbers">0.9.92</a>
37
37
  </div>
38
38
  <h1>&#x2192; Ruby on Rails</h1>
39
39
 
@@ -81,7 +81,7 @@ to the database layer of Rails &#8211; <a href="http://wiki.rubyonrails.com/rail
81
81
  <span class="ident">set_primary_keys</span> <span class="symbol">:user_id</span><span class="punct">,</span> <span class="symbol">:group_id</span>
82
82
  <span class="ident">belongs_to</span> <span class="symbol">:user</span>
83
83
  <span class="ident">belongs_to</span> <span class="symbol">:group</span>
84
- <span class="ident">has_many</span> <span class="symbol">:statuses</span><span class="punct">,</span> <span class="symbol">:class</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">MembershipStatus</span><span class="punct">',</span> <span class="symbol">:foreign_key</span> <span class="punct">=&gt;</span> <span class="punct">[</span><span class="symbol">:user_id</span><span class="punct">,</span> <span class="symbol">:group_id</span><span class="punct">]</span>
84
+ <span class="ident">has_many</span> <span class="symbol">:statuses</span><span class="punct">,</span> <span class="symbol">:class_name</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">MembershipStatus</span><span class="punct">',</span> <span class="symbol">:foreign_key</span> <span class="punct">=&gt;</span> <span class="punct">[</span><span class="symbol">:user_id</span><span class="punct">,</span> <span class="symbol">:group_id</span><span class="punct">]</span>
85
85
  <span class="keyword">end</span></pre></p>
86
86
 
87
87
 
@@ -291,7 +291,7 @@ other stories and things.</p>
291
291
 
292
292
  <p>Comments are welcome. Send an email to <a href="mailto:drnicwilliams@gmail.com">Dr Nic Williams</a>.</p>
293
293
  <p class="coda">
294
- <a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>, 19th December 2007<br>
294
+ <a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>, 25th February 2008<br>
295
295
  Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
296
296
  </p>
297
297
  </div>
@@ -33,7 +33,7 @@ A model with composite primary keys would look like...
33
33
  set_primary_keys :user_id, :group_id
34
34
  belongs_to :user
35
35
  belongs_to :group
36
- has_many :statuses, :class => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
36
+ has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
37
37
  end</pre>
38
38
 
39
39
  A model associated with a composite key model would be defined like...
@@ -1,3 +1,3 @@
1
1
  // Announcement JS file
2
- var version = "0.9.91";
2
+ var version = "0.9.92";
3
3
  MagicAnnouncement.show('compositekeys', version);
@@ -1,4 +1,4 @@
1
1
  // Version JS file
2
- var version = "0.9.91";
2
+ var version = "0.9.92";
3
3
 
4
4
  document.write(" - " + version);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.91
4
+ version: 0.9.92
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dr Nic Williams
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-02-19 00:00:00 +10:00
12
+ date: 2008-02-25 00:00:00 +10:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency