Skip to content

Commit cf2ec15

Browse files
committed
Refactor active_record class creation
1 parent cbc6fcc commit cf2ec15

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

spec/support/ar_classes.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
module Connections
77
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+
813
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(", ") })
1515
EOSQL
1616

1717
host.reset_column_information
@@ -24,21 +24,30 @@ class NonActiveRecordModel
2424
end
2525

2626
class MockableModel < ActiveRecord::Base
27+
def self.connection_fields
28+
{ associated_model_id: :integer }
29+
end
2730
extend Connections
31+
2832
has_one :associated_model
2933
end
3034

3135
class SubMockableModel < MockableModel
3236
end
3337

3438
class AssociatedModel < ActiveRecord::Base
39+
def self.connection_fields
40+
{ mockable_model_id: :integer, nonexistent_model_id: :integer }
41+
end
3542
extend Connections
43+
3644
belongs_to :mockable_model
3745
belongs_to :nonexistent_model, class_name: "Other"
3846
end
3947

4048
class AlternatePrimaryKeyModel < ActiveRecord::Base
4149
self.primary_key = :my_id
4250
extend Connections
51+
4352
attr_accessor :my_id
4453
end

0 commit comments

Comments
 (0)