5
5
6
6
module Connections
7
7
def self . extended ( host )
8
+ fields =
9
+ { host . primary_key => "integer PRIMARY KEY AUTOINCREMENT" }
10
+
11
+ fields . merge! ( host . connection_fields ) if host . respond_to? ( :connection_fields )
12
+
8
13
host . connection . execute <<-EOSQL
9
- CREATE TABLE #{ host . table_name } (
10
- #{ host . primary_key } integer PRIMARY KEY AUTOINCREMENT,
11
- associated_model_id integer,
12
- mockable_model_id integer,
13
- nonexistent_model_id integer
14
- )
14
+ CREATE TABLE #{ host . table_name } ( #{ fields . map { |column , type | "#{ column } #{ type } " } . join ( ", " ) } )
15
15
EOSQL
16
16
17
17
host . reset_column_information
@@ -24,21 +24,30 @@ class NonActiveRecordModel
24
24
end
25
25
26
26
class MockableModel < ActiveRecord ::Base
27
+ def self . connection_fields
28
+ { associated_model_id : :integer }
29
+ end
27
30
extend Connections
31
+
28
32
has_one :associated_model
29
33
end
30
34
31
35
class SubMockableModel < MockableModel
32
36
end
33
37
34
38
class AssociatedModel < ActiveRecord ::Base
39
+ def self . connection_fields
40
+ { mockable_model_id : :integer , nonexistent_model_id : :integer }
41
+ end
35
42
extend Connections
43
+
36
44
belongs_to :mockable_model
37
45
belongs_to :nonexistent_model , class_name : "Other"
38
46
end
39
47
40
48
class AlternatePrimaryKeyModel < ActiveRecord ::Base
41
49
self . primary_key = :my_id
42
50
extend Connections
51
+
43
52
attr_accessor :my_id
44
53
end
0 commit comments