composite_primary_keys 3.1.2 → 3.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/Rakefile +2 -2
- data/lib/composite_primary_keys.rb +2 -0
- data/lib/composite_primary_keys/associations.rb +18 -9
- data/lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb +2 -2
- data/lib/composite_primary_keys/associations/has_many_association.rb +1 -1
- data/lib/composite_primary_keys/base.rb +0 -19
- data/lib/composite_primary_keys/persistence.rb +29 -0
- data/lib/composite_primary_keys/version.rb +1 -1
- data/test/fixtures/reference_type.rb +5 -0
- data/test/fixtures/room_assignment.rb +8 -0
- data/test/fixtures/student.rb +1 -1
- metadata +9 -9
- data/tasks/Rakefile.rb +0 -13
data/History.txt
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== 3.1.3 2011-03-05
|
2
|
+
* Support ActiveRecord 3.0.5 (interpolate_sql was removed and
|
3
|
+
replaced by interpolate_and_sanitize_sql)
|
4
|
+
* Fix persistence methods to support destroy callbacks
|
5
|
+
|
6
|
+
|
1
7
|
== 3.1.2 2011-02-26
|
2
8
|
* Add back in support for find('3,3') which makes it possible to
|
3
9
|
do find(params[id]). This implementation is simpler than earlier version
|
data/Rakefile
CHANGED
@@ -7,8 +7,8 @@ require 'rake/packagetask'
|
|
7
7
|
require 'rake/gempackagetask'
|
8
8
|
|
9
9
|
# Set global variable so other tasks can access them
|
10
|
-
PROJECT_ROOT = File.expand_path(".")
|
11
|
-
GEM_NAME = 'composite_primary_keys'
|
10
|
+
::PROJECT_ROOT = File.expand_path(".")
|
11
|
+
::GEM_NAME = 'composite_primary_keys'
|
12
12
|
|
13
13
|
# Read the spec file
|
14
14
|
spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
|
@@ -44,6 +44,7 @@ require 'active_record/associations/has_many_association'
|
|
44
44
|
require 'active_record/associations/has_one_association'
|
45
45
|
require 'active_record/associations/has_one_through_association'
|
46
46
|
require 'active_record/associations/through_association_scope'
|
47
|
+
require 'active_record/persistence'
|
47
48
|
require 'active_record/relation/query_methods'
|
48
49
|
require 'active_record/attribute_methods/primary_key'
|
49
50
|
require 'active_record/fixtures'
|
@@ -56,6 +57,7 @@ require 'composite_primary_keys/associations/has_many_association'
|
|
56
57
|
require 'composite_primary_keys/associations/has_and_belongs_to_many_association'
|
57
58
|
require 'composite_primary_keys/associations/through_association_scope'
|
58
59
|
require 'composite_primary_keys/association_preload'
|
60
|
+
require 'composite_primary_keys/persistence'
|
59
61
|
require 'composite_primary_keys/reflection'
|
60
62
|
require 'composite_primary_keys/relation'
|
61
63
|
require 'composite_primary_keys/read'
|
@@ -35,8 +35,13 @@ module ActiveRecord
|
|
35
35
|
def association_join
|
36
36
|
return @join if @join
|
37
37
|
|
38
|
-
aliased_table = Arel::Table.new(table_name, :as
|
39
|
-
|
38
|
+
aliased_table = Arel::Table.new(table_name, :as => @aliased_table_name,
|
39
|
+
:engine => arel_engine,
|
40
|
+
:columns => klass.columns)
|
41
|
+
|
42
|
+
parent_table = Arel::Table.new(parent.table_name, :as => parent.aliased_table_name,
|
43
|
+
:engine => arel_engine,
|
44
|
+
:columns => parent.active_record.columns)
|
40
45
|
|
41
46
|
@join = case reflection.macro
|
42
47
|
when :has_and_belongs_to_many
|
@@ -51,14 +56,19 @@ module ActiveRecord
|
|
51
56
|
when :has_many, :has_one
|
52
57
|
if reflection.options[:through]
|
53
58
|
join_table = Arel::Table.new(through_reflection.klass.table_name, :as => aliased_join_table_name, :engine => arel_engine)
|
54
|
-
|
59
|
+
jt_as_extra = jt_source_extra = jt_sti_extra = nil
|
55
60
|
first_key = second_key = as_extra = nil
|
56
61
|
|
57
|
-
if through_reflection.
|
58
|
-
|
59
|
-
|
62
|
+
if through_reflection.macro == :belongs_to
|
63
|
+
jt_primary_key = through_reflection.primary_key_name
|
64
|
+
jt_foreign_key = through_reflection.association_primary_key
|
60
65
|
else
|
66
|
+
jt_primary_key = through_reflection.active_record_primary_key
|
61
67
|
jt_foreign_key = through_reflection.primary_key_name
|
68
|
+
|
69
|
+
if through_reflection.options[:as] # has_many :through against a polymorphic join
|
70
|
+
jt_as_extra = join_table[through_reflection.options[:as].to_s + '_type'].eq(parent.active_record.base_class.name)
|
71
|
+
end
|
62
72
|
end
|
63
73
|
|
64
74
|
case source_reflection.macro
|
@@ -86,7 +96,7 @@ module ActiveRecord
|
|
86
96
|
end
|
87
97
|
|
88
98
|
[
|
89
|
-
[parent_table[
|
99
|
+
[parent_table[jt_primary_key].eq(join_table[jt_foreign_key]), jt_as_extra, jt_source_extra, jt_sti_extra].reject{|x| x.blank? },
|
90
100
|
aliased_table[first_key].eq(join_table[second_key])
|
91
101
|
]
|
92
102
|
elsif reflection.options[:as]
|
@@ -95,7 +105,6 @@ module ActiveRecord
|
|
95
105
|
[id_rel, type_rel]
|
96
106
|
else
|
97
107
|
foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
|
98
|
-
|
99
108
|
# CPK
|
100
109
|
#[aliased_table[foreign_key].eq(parent_table[reflection.options[:primary_key] || parent.primary_key])]
|
101
110
|
composite_join_predicates(aliased_table, foreign_key,
|
@@ -115,7 +124,7 @@ module ActiveRecord
|
|
115
124
|
|
116
125
|
[through_reflection, reflection].each do |ref|
|
117
126
|
if ref && ref.options[:conditions]
|
118
|
-
@join <<
|
127
|
+
@join << process_conditions(ref.options[:conditions], aliased_table_name)
|
119
128
|
end
|
120
129
|
end
|
121
130
|
|
@@ -3,7 +3,7 @@ module ActiveRecord
|
|
3
3
|
class HasAndBelongsToManyAssociation
|
4
4
|
def construct_sql
|
5
5
|
if @reflection.options[:finder_sql]
|
6
|
-
@finder_sql =
|
6
|
+
@finder_sql = interpolate_and_sanitize_sql(@reflection.options[:finder_sql])
|
7
7
|
else
|
8
8
|
# CPK
|
9
9
|
# @finder_sql = "#{@owner.connection.quote_table_name @reflection.options[:join_table]}.#{@reflection.primary_key_name} = #{owner_quoted_id} "
|
@@ -36,7 +36,7 @@ module ActiveRecord
|
|
36
36
|
end
|
37
37
|
|
38
38
|
if @reflection.options[:insert_sql]
|
39
|
-
@owner.connection.insert(
|
39
|
+
@owner.connection.insert(interpolate_and_sanitize_sql(@reflection.options[:insert_sql], record))
|
40
40
|
else
|
41
41
|
relation = Arel::Table.new(@reflection.options[:join_table])
|
42
42
|
timestamps = record_timestamp_columns(record)
|
@@ -4,7 +4,7 @@ module ActiveRecord
|
|
4
4
|
def construct_sql
|
5
5
|
case
|
6
6
|
when @reflection.options[:finder_sql]
|
7
|
-
@finder_sql =
|
7
|
+
@finder_sql = interpolate_and_sanitize_sql(@reflection.options[:finder_sql])
|
8
8
|
|
9
9
|
when @reflection.options[:as]
|
10
10
|
@finder_sql =
|
@@ -172,25 +172,6 @@ module ActiveRecord
|
|
172
172
|
create_with.each { |att,value| self.send("#{att}=", value) } if create_with
|
173
173
|
end
|
174
174
|
end
|
175
|
-
|
176
|
-
def destroy
|
177
|
-
if persisted?
|
178
|
-
# CPK
|
179
|
-
# self.class.unscoped.where(self.class.arel_table[self.class.primary_key].eq(id)).delete_all
|
180
|
-
self.class.unscoped.where(ids_hash).delete_all
|
181
|
-
end
|
182
|
-
|
183
|
-
@destroyed = true
|
184
|
-
freeze
|
185
|
-
end
|
186
|
-
|
187
|
-
def update(attribute_names = @attributes.keys)
|
188
|
-
attributes_with_values = arel_attributes_values(false, false, attribute_names)
|
189
|
-
return 0 if attributes_with_values.empty?
|
190
|
-
# CPK
|
191
|
-
# self.class.unscoped.where(self.class.arel_table[self.class.primary_key].eq(id)).arel.update(attributes_with_values)
|
192
|
-
self.class.unscoped.where(ids_hash).arel.update(attributes_with_values)
|
193
|
-
end
|
194
175
|
end
|
195
176
|
end
|
196
177
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Persistence
|
3
|
+
def cpk_conditions
|
4
|
+
if self.composite?
|
5
|
+
ids_hash
|
6
|
+
else
|
7
|
+
self.class.arel_table[self.class.primary_key].eq(id)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def destroy
|
12
|
+
if persisted?
|
13
|
+
# self.class.unscoped.where(self.class.arel_table[self.class.primary_key].eq(id)).delete_all
|
14
|
+
self.class.unscoped.where(cpk_conditions).delete_all
|
15
|
+
end
|
16
|
+
|
17
|
+
@destroyed = true
|
18
|
+
freeze
|
19
|
+
end
|
20
|
+
|
21
|
+
def update(attribute_names = @attributes.keys)
|
22
|
+
attributes_with_values = arel_attributes_values(false, false, attribute_names)
|
23
|
+
return 0 if attributes_with_values.empty?
|
24
|
+
# CPK
|
25
|
+
# self.class.unscoped.where(self.class.arel_table[self.class.primary_key].eq(id)).arel.update(attributes_with_values)
|
26
|
+
self.class.unscoped.where(cpk_conditions).arel.update(attributes_with_values)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/test/fixtures/student.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composite_primary_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 3
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 3.1.
|
9
|
+
- 4
|
10
|
+
version: 3.1.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dr Nic Williams
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-
|
19
|
+
date: 2011-03-06 00:00:00 -07:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -27,12 +27,12 @@ dependencies:
|
|
27
27
|
requirements:
|
28
28
|
- - ">="
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
hash:
|
30
|
+
hash: 13
|
31
31
|
segments:
|
32
32
|
- 3
|
33
33
|
- 0
|
34
|
-
-
|
35
|
-
version: 3.0.
|
34
|
+
- 5
|
35
|
+
version: 3.0.5
|
36
36
|
type: :runtime
|
37
37
|
version_requirements: *id001
|
38
38
|
- !ruby/object:Gem::Dependency
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb
|
86
86
|
- lib/composite_primary_keys/finder_methods.rb
|
87
87
|
- lib/composite_primary_keys/fixtures.rb
|
88
|
+
- lib/composite_primary_keys/persistence.rb
|
88
89
|
- lib/composite_primary_keys/primary_key.rb
|
89
90
|
- lib/composite_primary_keys/query_methods.rb
|
90
91
|
- lib/composite_primary_keys/read.rb
|
@@ -100,7 +101,6 @@ files:
|
|
100
101
|
- tasks/databases/oracle.rake
|
101
102
|
- tasks/databases/postgresql.rake
|
102
103
|
- tasks/databases/sqlite3.rake
|
103
|
-
- tasks/Rakefile.rb
|
104
104
|
- tasks/website.rake
|
105
105
|
- test/abstract_unit.rb
|
106
106
|
- test/connections/connection_spec.rb
|
@@ -234,7 +234,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
234
234
|
requirements: []
|
235
235
|
|
236
236
|
rubyforge_project: compositekeys
|
237
|
-
rubygems_version: 1.
|
237
|
+
rubygems_version: 1.6.1
|
238
238
|
signing_key:
|
239
239
|
specification_version: 3
|
240
240
|
summary: Composite key support for ActiveRecord
|
data/tasks/Rakefile.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
#require 'rake'
|
2
|
-
#require 'rake/testtask'
|
3
|
-
#
|
4
|
-
#require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
|
5
|
-
#
|
6
|
-
## Set up test tasks
|
7
|
-
#for adapter in %w( mysql sqlite oracle oracle_enhanced postgresql ibm_db )
|
8
|
-
# Rake::TestTask.new("test_#{adapter}") do |t|
|
9
|
-
# t.libs << "test" << "test/connections/native_#{adapter}"
|
10
|
-
# t.pattern = "test/test_*.rb"
|
11
|
-
# t.verbose = true
|
12
|
-
# end
|
13
|
-
#nd
|