composite_primary_keys 1.0.7 → 1.0.8

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.
@@ -1,3 +1,7 @@
1
+ == 1.0.8 2008-08-27
2
+
3
+ * fix has_many :through for non composite models [thx rcarver]
4
+
1
5
  == 1.0.7 2008-08-12
2
6
 
3
7
  * fix for the last fix -- when has_many is composite and belongs_to is single
@@ -372,31 +372,38 @@ module ActiveRecord::Associations
372
372
  @finder_sql << " AND (#{conditions})" if conditions
373
373
  end
374
374
  end
375
-
376
375
 
377
376
  class HasManyThroughAssociation < HasManyAssociation #:nodoc:
378
- def construct_conditions
379
- raise 'polymorphic joins are not supported by composite_primary_keys' if @reflection.through_reflection.options[:as]
380
- conditions = full_columns_equals(@reflection.through_reflection.table_name, @reflection.through_reflection.primary_key_name, @owner.quoted_id)
381
- conditions << " AND (#{sql_conditions})" if sql_conditions
382
- conditions
377
+ def construct_conditions_with_composite_keys
378
+ if @reflection.through_reflection.options[:as]
379
+ construct_conditions_without_composite_keys;
380
+ else
381
+ conditions = full_columns_equals(@reflection.through_reflection.table_name, @reflection.through_reflection.primary_key_name, @owner.quoted_id)
382
+ conditions << " AND (#{sql_conditions})" if sql_conditions
383
+ conditions
384
+ end
383
385
  end
386
+ alias_method_chain :construct_conditions, :composite_keys
384
387
 
385
- def construct_joins(custom_joins = nil)
386
- raise 'polymorphic joins are not supported by composite_primary_keys' if @reflection.through_reflection.options[:as] || @reflection.source_reflection.options[:as]
387
-
388
- if @reflection.source_reflection.macro == :belongs_to
389
- reflection_primary_key = @reflection.klass.primary_key
390
- source_primary_key = @reflection.source_reflection.primary_key_name
388
+ def construct_joins_with_composite_keys(custom_joins = nil)
389
+ if @reflection.through_reflection.options[:as] || @reflection.source_reflection.options[:as]
390
+ construct_joins_without_composite_keys(custom_joins)
391
391
  else
392
- reflection_primary_key = @reflection.source_reflection.primary_key_name
393
- source_primary_key = @reflection.klass.primary_key
394
- end
392
+ if @reflection.source_reflection.macro == :belongs_to
393
+ reflection_primary_key = @reflection.klass.primary_key
394
+ source_primary_key = @reflection.source_reflection.primary_key_name
395
+ else
396
+ reflection_primary_key = @reflection.source_reflection.primary_key_name
397
+ source_primary_key = @reflection.klass.primary_key
398
+ end
395
399
 
396
- "INNER JOIN %s ON %s #{@reflection.options[:joins]} #{custom_joins}" % [
397
- @reflection.through_reflection.table_name,
398
- composite_join_clause(full_keys(@reflection.table_name, reflection_primary_key), full_keys(@reflection.through_reflection.table_name, source_primary_key))
399
- ]
400
+ "INNER JOIN %s ON %s #{@reflection.options[:joins]} #{custom_joins}" % [
401
+ @reflection.through_reflection.table_name,
402
+ composite_join_clause(full_keys(@reflection.table_name, reflection_primary_key), full_keys(@reflection.through_reflection.table_name, source_primary_key))
403
+ ]
404
+ end
400
405
  end
406
+ alias_method_chain :construct_joins, :composite_keys
401
407
  end
408
+
402
409
  end
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- TINY = 7
5
+ TINY = 8
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -1,5 +1,6 @@
1
1
  class Comment < ActiveRecord::Base
2
2
  set_primary_keys :id
3
3
  belongs_to :person, :polymorphic => true
4
+ belongs_to :hack
4
5
  end
5
6
 
@@ -7,8 +7,10 @@ comment2:
7
7
  id: 2
8
8
  person_id: 1
9
9
  person_type: User
10
+ hack_id: andrew
10
11
 
11
12
  comment3:
12
13
  id: 3
13
14
  person_id: andrew
14
- person_type: Hack
15
+ person_type: Hack
16
+
@@ -107,6 +107,7 @@ CREATE TABLE comments (
107
107
  id int(11) NOT NULL auto_increment,
108
108
  person_id varchar(100) DEFAULT NULL,
109
109
  person_type varchar(100) DEFAULT NULL,
110
+ hack_id varchar(100) DEFAULT NULL,
110
111
  PRIMARY KEY (id)
111
112
  ) TYPE=InnoDB;
112
113
 
@@ -127,7 +127,8 @@ create sequence employees_seq
127
127
  CREATE TABLE comments (
128
128
  id number(11) NOT NULL PRIMARY KEY,
129
129
  person_id varchar(100) DEFAULT NULL,
130
- person_type varchar(100) DEFAULT NULL
130
+ person_type varchar(100) DEFAULT NULL,
131
+ hack_id varchar(100) DEFAULT NULL
131
132
  );
132
133
 
133
134
  create sequence comments_seq
@@ -115,6 +115,7 @@ CREATE TABLE comments (
115
115
  id int NOT NULL,
116
116
  person_id varchar(100) DEFAULT NULL,
117
117
  person_type varchar(100) DEFAULT NULL,
118
+ hack_id varchar(100) DEFAULT NULL,
118
119
  PRIMARY KEY (id)
119
120
  );
120
121
 
@@ -97,7 +97,8 @@ CREATE TABLE employees (
97
97
  CREATE TABLE comments (
98
98
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
99
99
  person_id varchar(100) NULL,
100
- person_type varchar(100) NULL
100
+ person_type varchar(100) NULL,
101
+ hack_id varchar(100) NULL
101
102
  );
102
103
 
103
104
  CREATE TABLE hacks (
@@ -2,6 +2,7 @@ class User < ActiveRecord::Base
2
2
  has_many :readings
3
3
  has_many :articles, :through => :readings
4
4
  has_many :comments, :as => :person
5
+ has_many :hacks, :through => :comments, :source => :hack
5
6
 
6
7
  def find_custom_articles
7
8
  articles.find(:all, :conditions => ["name = ?", "Article One"])
@@ -9,12 +9,23 @@ class TestPolymorphic < Test::Unit::TestCase
9
9
 
10
10
  def test_polymorphic_has_many
11
11
  comments = Hack.find('andrew').comments
12
- assert comments[0].person_id = 'andrew'
12
+ assert_equal 'andrew', comments[0].person_id
13
13
  end
14
14
 
15
15
  def test_polymorphic_has_one
16
16
  first_comment = Hack.find('andrew').first_comment
17
- assert first_comment.person_id = 'andrew'
17
+ assert_equal 'andrew', first_comment.person_id
18
+ end
19
+
20
+ def test_has_many_through
21
+ user = users(:santiago)
22
+ article_names = user.articles.collect { |a| a.name }.sort
23
+ assert_equal ['Article One', 'Article Two'], article_names
24
+ end
25
+
26
+ def test_polymorphic_has_many_through
27
+ user = users(:santiago)
28
+ assert_equal ['andrew'], user.hacks.collect { |a| a.name }.sort
18
29
  end
19
30
 
20
31
  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">1.0.6</a>
36
+ <a href="http://rubyforge.org/projects/compositekeys" class="numbers">1.0.8</a>
37
37
  </div>
38
38
  <h1>&#x2192; Ruby on Rails</h1>
39
39
 
@@ -1,3 +1,3 @@
1
1
  // Announcement JS file
2
- var version = "1.0.6";
2
+ var version = "1.0.8";
3
3
  MagicAnnouncement.show('compositekeys', version);
@@ -1,4 +1,4 @@
1
1
  // Version JS file
2
- var version = "1.0.6";
2
+ var version = "1.0.8";
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: 1.0.7
4
+ version: 1.0.8
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-08-12 00:00:00 -05:00
12
+ date: 2008-08-27 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency