-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathstruct.rb
101 lines (86 loc) · 2.34 KB
/
struct.rb
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require "active_support/concern"
require "torque/postgresql/adapter"
module Torque
class BaseStruct
def to_s
# Avoid printing excessive volumes
"#<#{self.class.name}>"
end
def _run_find_callbacks
end
def _run_initialize_callbacks
end
class << self
def connection
# Lets you overwrite `connection` per-class
ActiveRecord::Base.connection
end
class_attribute :primary_key
self.primary_key = "id"
def base_class?
self == BaseStruct || self == Struct
end
def base_class
BaseStruct
end
def table_name
nil
end
def abstract_class?
base_class?
end
end
end
class Struct < BaseStruct
include ActiveRecord::Core
include ActiveRecord::Persistence
include ActiveRecord::ModelSchema
include ActiveRecord::Attributes
include ActiveRecord::AttributeMethods
include ActiveRecord::Serialization
include ActiveRecord::AttributeAssignment
self.pluralize_table_names = false
def initialize(attributes = nil)
@attributes = self.class.attributes_builder.build_from_database
assign_attributes(attributes) if attributes
self.class.define_attribute_methods
yield self if block_given?
end
class << self
# ActiveRecord modules call `superclass.foo`, so we need an extra layer of inheritance
def database_type
::Torque::PostgreSQL::Adapter::OID::Struct.for_type(table_name, klass: self)
end
def database_array_type
::Torque::PostgreSQL::Adapter::OID::Struct.for_type(table_name + "[]", klass: self)
end
def table_exists?
::Torque::PostgreSQL::Adapter::OID::Struct.for_type(table_name).present?
end
def type_name
table_name
end
def type_name=(value)
@type_name = value
end
def table_name
return @type_name if @type_name
if self === Struct
nil
else
self.name.underscore
end
end
end
end
class ActiveRecord::Base
class << self
def database_type
::Torque::PostgreSQL::Adapter::OID::Struct.for_type(table_name, klass: self)
end
def database_array_type
::Torque::PostgreSQL::Adapter::OID::Struct.for_type(table_name + "[]", klass: self)
end
end
end
end