aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/models/contact.rb
blob: d5f6f0069106f282132f1f0933e956bdc4caaf9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true

module ContactFakeColumns
  def self.extended(base)
    base.class_eval do
      establish_connection(adapter: "fake")

      connection.data_sources = [table_name]
      connection.primary_keys = {
        table_name => "id"
      }

      column :id,             "integer"
      column :name,           "string"
      column :age,            "integer"
      column :avatar,         "binary"
      column :created_at,     "datetime"
      column :awesome,        "boolean"
      column :preferences,    "string"
      column :alternative_id, "integer"

      serialize :preferences

      belongs_to :alternative, class_name: "Contact"
    end
  end

  # mock out self.columns so no pesky db is needed for these tests
  def column(name, sql_type = nil, options = {})
    connection.merge_column(table_name, name, sql_type, options)
  end
end

class Contact < ActiveRecord::Base
  extend ContactFakeColumns
end

class ContactSti < ActiveRecord::Base
  extend ContactFakeColumns
  column :type, "string"

  def type; "ContactSti" end
end