-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathenum_column_adapter.rb
More file actions
111 lines (99 loc) · 3.06 KB
/
enum_column_adapter.rb
File metadata and controls
111 lines (99 loc) · 3.06 KB
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
102
103
104
105
106
107
108
109
110
111
# This module provides all the column helper methods to deal with the
# values and adds the common type management code for the adapters.
# try rails 3.1, then rails 3.2+, mysql column adapters
column_class = if defined? ActiveRecord::ConnectionAdapters::Mysql2Column
ActiveRecord::ConnectionAdapters::Mysql2Column
elsif defined? ActiveRecord::ConnectionAdapters::MysqlColumn
ActiveRecord::ConnectionAdapters::MysqlColumn
elsif defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter::Column
ActiveRecord::ConnectionAdapters::Mysql2Adapter::Column
elsif defined? ActiveRecord::ConnectionAdapters::MysqlAdapter::Column
ActiveRecord::ConnectionAdapters::MysqlAdapter::Column
elsif defined? ActiveRecord::ConnectionAdapters::MySQL::Column
ActiveRecord::ConnectionAdapters::MySQL::Column
end
if column_class
column_class.class_eval do
if instance_methods.include?(:extract_default)
alias __extract_default_enum extract_default
def extract_default
if type == :enum
if @default == '' || @default.nil?
@default = nil
else
@default = @default.intern
end
end
__extract_default_enum
end
end
def __enum_type_cast(value)
if type == :enum
self.class.value_to_symbol(value)
else
__type_cast_enum(value)
end
end
if instance_methods.include?(:type_cast_from_database)
alias __type_cast_enum type_cast_from_database
# Convert to a symbol.
def type_cast_from_database(value)
__enum_type_cast(value)
end
elsif instance_methods.include?(:type_cast)
alias __type_cast_enum type_cast
def type_cast(value)
__enum_type_cast(value)
end
end
# Deprecated in Rails 4.1
if instance_methods.include?(:type_cast_code)
alias __type_cast_code_enum type_cast_code
# Code to convert to a symbol.
def type_cast_code(var_name)
if type == :enum
"#{self.class.name}.value_to_symbol(#{var_name})"
else
__type_cast_code_enum(var_name)
end
end
end
class << self
# Safely convert the value to a symbol.
def value_to_symbol(value)
case value
when Symbol
value
when String
value.empty? ? nil : value.intern
else
nil
end
end
end
private
# Deprecated in Rails 4.2
if private_instance_methods.include?(:simplified_type)
alias __simplified_type_enum simplified_type
# The enum simple type.
def simplified_type(field_type)
if field_type =~ /enum/i
:enum
else
__simplified_type_enum(field_type)
end
end
end
# Deprecated in Rails 4.2
if private_instance_methods.include?(:extract_limit)
alias __extract_limit_enum extract_limit
def extract_limit(sql_type)
if sql_type =~ /^enum/i
sql_type.sub(/^enum\('(.+)'\)/i, '\1').split("','").map { |v| v.intern }
else
__extract_limit_enum(sql_type)
end
end
end
end
end