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.
- data/History.txt +4 -0
- data/lib/composite_primary_keys/associations.rb +26 -19
- data/lib/composite_primary_keys/version.rb +1 -1
- data/test/fixtures/comment.rb +1 -0
- data/test/fixtures/comments.yml +3 -1
- data/test/fixtures/db_definitions/mysql.sql +1 -0
- data/test/fixtures/db_definitions/oracle.sql +2 -1
- data/test/fixtures/db_definitions/postgresql.sql +1 -0
- data/test/fixtures/db_definitions/sqlite.sql +2 -1
- data/test/fixtures/user.rb +1 -0
- data/test/test_polymorphic.rb +13 -2
- data/tmp/test.db +0 -0
- data/website/index.html +1 -1
- data/website/version-raw.js +1 -1
- data/website/version.js +1 -1
- metadata +2 -2
data/History.txt
CHANGED
@@ -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
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
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
|
386
|
-
|
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
|
-
|
393
|
-
|
394
|
-
|
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
|
-
|
397
|
-
|
398
|
-
|
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
|
data/test/fixtures/comment.rb
CHANGED
data/test/fixtures/comments.yml
CHANGED
@@ -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
|
@@ -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 (
|
data/test/fixtures/user.rb
CHANGED
@@ -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"])
|
data/test/test_polymorphic.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
data/tmp/test.db
CHANGED
Binary file
|
data/website/index.html
CHANGED
@@ -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.
|
36
|
+
<a href="http://rubyforge.org/projects/compositekeys" class="numbers">1.0.8</a>
|
37
37
|
</div>
|
38
38
|
<h1>→ Ruby on Rails</h1>
|
39
39
|
|
data/website/version-raw.js
CHANGED
data/website/version.js
CHANGED
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.
|
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
|
+
date: 2008-08-27 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|