Skip to content

Instantly share code, notes, and snippets.

@nileshtrivedi
Created June 29, 2011 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nileshtrivedi/1054826 to your computer and use it in GitHub Desktop.
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
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
@nileshtrivedi
Copy link
Author

The plug-in provides support for MySQL ENUM columns in ActiveRecord. More details are available here: http://enum-column.rubyforge.org/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment