From fe9fe1c487c78ed44d6c761f272dc3421d36465e Mon Sep 17 00:00:00 2001 From: bmck Date: Fri, 29 Aug 2014 13:45:16 -0500 Subject: [PATCH 1/3] Create abstract_mysql_adapter.rb Suppress rails 4.1.x's conversion of enum columns in output schema.rb files to integers (i.e., preserving enum_column's historical behavior) --- lib/enum/abstract_mysql_adapter.rb | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lib/enum/abstract_mysql_adapter.rb diff --git a/lib/enum/abstract_mysql_adapter.rb b/lib/enum/abstract_mysql_adapter.rb new file mode 100644 index 0000000..1843bd4 --- /dev/null +++ b/lib/enum/abstract_mysql_adapter.rb @@ -0,0 +1,37 @@ +adapter_class = if defined? ActiveRecord::ConnectionAdapters::MysqlAdapter + ActiveRecord::ConnectionAdapters::MysqlAdapter +end + +if adapter_class + adapter_class.module_eval do + protected + + def initialize_type_map(m) # :nodoc: + super + # m.register_type(%r(enum)i) do |sql_type| + # limit = sql_type[/^enum\((.+)\)/i, 1] + # .split(',').map{|enum| enum.strip.length - 2}.max + # Type::String.new(limit: limit) + # end + + m.register_type %r(tinytext)i, Type::Text.new(limit: 255) + m.register_type %r(tinyblob)i, Type::Binary.new(limit: 255) + m.register_type %r(mediumtext)i, Type::Text.new(limit: 16777215) + m.register_type %r(mediumblob)i, Type::Binary.new(limit: 16777215) + m.register_type %r(longtext)i, Type::Text.new(limit: 2147483647) + m.register_type %r(longblob)i, Type::Binary.new(limit: 2147483647) + m.register_type %r(^bigint)i, Type::Integer.new(limit: 8) + m.register_type %r(^int)i, Type::Integer.new(limit: 4) + m.register_type %r(^mediumint)i, Type::Integer.new(limit: 3) + m.register_type %r(^smallint)i, Type::Integer.new(limit: 2) + m.register_type %r(^tinyint)i, Type::Integer.new(limit: 1) + m.register_type %r(^float)i, Type::Float.new(limit: 24) + m.register_type %r(^double)i, Type::Float.new(limit: 53) + + m.alias_type %r(tinyint\(1\))i, 'boolean' if emulate_booleans + m.alias_type %r(set)i, 'varchar' + m.alias_type %r(year)i, 'integer' + m.alias_type %r(bit)i, 'binary' + end + end +end From 28a944ab08ccc7151c763cce50773100e9adcec6 Mon Sep 17 00:00:00 2001 From: bmck Date: Fri, 29 Aug 2014 13:46:35 -0500 Subject: [PATCH 2/3] Add new file enum/abstract_mysql_adapter.rb --- lib/enum_column.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/enum_column.rb b/lib/enum_column.rb index 7d951bb..03618f3 100644 --- a/lib/enum_column.rb +++ b/lib/enum_column.rb @@ -3,6 +3,7 @@ class EnumColumnRailtie < Rails::Railtie initializer 'enum_column.initialize', :after => 'active_record.initialize_database' do |app| ActiveSupport.on_load :active_record do require 'enum/mysql_adapter' + require 'enum/abstract_mysql_adapter' require 'enum/enum_adapter' require 'enum/schema_statements' require 'enum/schema_definitions' @@ -15,4 +16,4 @@ class EnumColumnRailtie < Rails::Railtie end end end -end \ No newline at end of file +end From a6b93fb464a6349f843f1e50f507dc57513243a6 Mon Sep 17 00:00:00 2001 From: Bill McKinnon Date: Thu, 2 Apr 2015 10:20:40 -0500 Subject: [PATCH 3/3] check rails version --- lib/enum/enum_adapter.rb | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/enum/enum_adapter.rb b/lib/enum/enum_adapter.rb index a58b4c4..331990e 100644 --- a/lib/enum/enum_adapter.rb +++ b/lib/enum/enum_adapter.rb @@ -27,24 +27,26 @@ def klass end end - alias __type_cast_enum type_cast - # Convert to a symbol. - def type_cast(value) - if type == :enum - self.class.value_to_symbol(value) - else - __type_cast_enum(value) - end - end - - if respond_to?(:type_cast_code) - alias __type_cast_code_enum type_cast_code - # Code to convert to a symbol. - def type_cast_code(var_name) + if Rails::VERSION::MAJOR < 4 + alias __type_cast_enum type_cast + # Convert to a symbol. + def type_cast(value) if type == :enum - "#{self.class.name}.value_to_symbol(#{var_name})" + self.class.value_to_symbol(value) else - __type_cast_code_enum(var_name) + __type_cast_enum(value) + end + end + + if respond_to?(: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 end @@ -63,7 +65,7 @@ def value_to_symbol(value) end end -private + private alias __simplified_type_enum simplified_type # The enum simple type. def simplified_type(field_type) @@ -73,7 +75,7 @@ def simplified_type(field_type) __simplified_type_enum(field_type) end end - + alias __extract_limit_enum extract_limit def extract_limit(sql_type) if sql_type =~ /^enum/i