Created
June 29, 2011 20:16
-
-
Save nileshtrivedi/1054826 to your computer and use it in GitHub Desktop.
Make enum-column plugin for Rails work with JDBC MySQL adapter as well
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveRecord | |
module ConnectionAdapters | |
class MysqlAdapter | |
alias __native_database_types_enum native_database_types | |
def native_database_types #:nodoc | |
types = __native_database_types_enum | |
types[:enum] = { :name => "enum" } | |
types | |
end | |
def columns(table_name, name = nil)#:nodoc: | |
sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}" | |
execute(sql, name).map do |field| | |
MysqlColumnWithEnum.new(field["Field"], field["Default"], field["Type"], field["Null"] == "YES") | |
end | |
end | |
end | |
class MysqlColumnWithEnum < MysqlColumn | |
include ActiveRecordEnumerations::Column | |
def initialize(name, default, sql_type = nil, null = true) | |
if sql_type =~ /^enum/i | |
values = sql_type.sub(/^enum\('([^)]+)'\)/i, '\1').split("','").map { |v| v.intern } | |
default = default.intern if default and !default.empty? | |
end | |
super(name, default, sql_type, null, values) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The plug-in provides support for MySQL ENUM columns in ActiveRecord. More details are available here: http://enum-column.rubyforge.org/