From e781aa31fc52a7c696115302ef4d4e02bfd1533b Mon Sep 17 00:00:00 2001
From: Sean Griffin <sean@thoughtbot.com>
Date: Tue, 20 May 2014 09:15:22 -0700
Subject: Replace `type_cast` case statement with delegation

All subclasses of column were now delegating `type_cast` to their
injected type object. We can remove the overriding methods, and
generalize it on the `Column` class itself. This also enabled us to
remove several column classes completely, as they no longer had any
meaningful behavior of their own.
---
 .../connection_adapters/abstract_mysql_adapter.rb  |  3 +--
 .../active_record/connection_adapters/column.rb    | 26 ++++------------------
 .../connection_adapters/mysql2_adapter.rb          | 12 ----------
 .../connection_adapters/mysql_adapter.rb           | 20 -----------------
 .../connection_adapters/postgresql/column.rb       |  7 ------
 .../connection_adapters/sqlite3_adapter.rb         | 12 +---------
 activerecord/test/cases/column_test.rb             |  2 +-
 7 files changed, 7 insertions(+), 75 deletions(-)

diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
index 852b7105d3..86eb2a38d8 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -249,10 +249,9 @@ module ActiveRecord
         raise NotImplementedError
       end
 
-      # Overridden by the adapters to instantiate their specific Column type.
       def new_column(field, default, sql_type, null, collation, extra = "") # :nodoc:
         cast_type = lookup_cast_type(sql_type)
-        Column.new(field, default, cast_type, sql_type, null, collation, extra)
+        Column.new(field, default, cast_type, sql_type, null, collation, strict_mode?, extra)
       end
 
       # Must return the Mysql error number from the exception, if the exception has an
diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb
index 11b2e728e1..f46f9af239 100644
--- a/activerecord/lib/active_record/connection_adapters/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/column.rb
@@ -94,28 +94,10 @@ module ActiveRecord
 
       # Casts value to an appropriate instance.
       def type_cast(value)
-        return nil if value.nil?
-        return coder.load(value) if encoded?
-
-        klass = self.class
-
-        case type
-        when :string, :text
-          case value
-          when TrueClass; "1"
-          when FalseClass; "0"
-          else
-            value.to_s
-          end
-        when :integer              then klass.value_to_integer(value)
-        when :float                then value.to_f
-        when :decimal              then klass.value_to_decimal(value)
-        when :datetime             then klass.string_to_time(value)
-        when :time                 then klass.string_to_dummy_time(value)
-        when :date                 then klass.value_to_date(value)
-        when :binary               then klass.binary_to_string(value)
-        when :boolean              then klass.value_to_boolean(value)
-        else value
+        if encoded?
+          coder.load(value)
+        else
+          cast_type.type_cast(value)
         end
       end
 
diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
index 2e39168374..0a14cdfe89 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
@@ -29,13 +29,6 @@ module ActiveRecord
 
   module ConnectionAdapters
     class Mysql2Adapter < AbstractMysqlAdapter
-
-      class Column < AbstractMysqlAdapter::Column # :nodoc:
-        def adapter
-          Mysql2Adapter
-        end
-      end
-
       ADAPTER_NAME = 'Mysql2'
 
       def initialize(connection, logger, connection_options, config)
@@ -69,11 +62,6 @@ module ActiveRecord
         end
       end
 
-      def new_column(field, default, sql_type, null, collation, extra = "") # :nodoc:
-        cast_type = lookup_cast_type(sql_type)
-        Column.new(field, default, cast_type, sql_type, null, collation, strict_mode?, extra)
-      end
-
       def error_number(exception)
         exception.error_number if exception.respond_to?(:error_number)
       end
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index fc27e1dd66..af5f24b08f 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -66,21 +66,6 @@ module ActiveRecord
     # * <tt>:sslcipher</tt> - Necessary to use MySQL with an SSL connection.
     #
     class MysqlAdapter < AbstractMysqlAdapter
-
-      class Column < AbstractMysqlAdapter::Column #:nodoc:
-        def type_cast(value)
-          if encoded?
-            super
-          else
-            cast_type.type_cast(value)
-          end
-        end
-
-        def adapter
-          MysqlAdapter
-        end
-      end
-
       ADAPTER_NAME = 'MySQL'
 
       class StatementPool < ConnectionAdapters::StatementPool
@@ -142,11 +127,6 @@ module ActiveRecord
         end
       end
 
-      def new_column(field, default, sql_type, null, collation, extra = "") # :nodoc:
-        cast_type = lookup_cast_type(sql_type)
-        Column.new(field, default, cast_type, sql_type, null, collation, strict_mode?, extra)
-      end
-
       def error_number(exception) # :nodoc:
         exception.errno if exception.respond_to?(:errno)
       end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
index 1dd8acc257..e5118b5427 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
@@ -123,13 +123,6 @@ module ActiveRecord
         end
       end
 
-      def type_cast(value)
-        return if value.nil?
-        return super if encoded?
-
-        @oid_type.type_cast value
-      end
-
       def accessor
         @oid_type.accessor
       end
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
index 7f83891043..a5e2619cb8 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -41,16 +41,6 @@ module ActiveRecord
   end
 
   module ConnectionAdapters #:nodoc:
-    class SQLite3Column < Column #:nodoc:
-      def type_cast(value)
-        if encoded?
-          super
-        else
-          cast_type.type_cast(value)
-        end
-      end
-    end
-
     class SQLite3Binary < Type::Binary # :nodoc:
       def cast_value(value)
         if value.encoding != Encoding::ASCII_8BIT
@@ -403,7 +393,7 @@ module ActiveRecord
 
           sql_type = field['type']
           cast_type = lookup_cast_type(sql_type)
-          SQLite3Column.new(field['name'], field['dflt_value'], cast_type, sql_type, field['notnull'].to_i == 0)
+          Column.new(field['name'], field['dflt_value'], cast_type, sql_type, field['notnull'].to_i == 0)
         end
       end
 
diff --git a/activerecord/test/cases/column_test.rb b/activerecord/test/cases/column_test.rb
index c5d455f59c..15c23ea61d 100644
--- a/activerecord/test/cases/column_test.rb
+++ b/activerecord/test/cases/column_test.rb
@@ -146,7 +146,7 @@ module ActiveRecord
 
       if current_adapter?(:SQLite3Adapter)
         def test_binary_encoding
-          column = SQLite3Column.new("field", nil, SQLite3Binary.new)
+          column = Column.new("field", nil, SQLite3Binary.new)
           utf8_string = "a string".encode(Encoding::UTF_8)
           type_cast = column.type_cast(utf8_string)
 
-- 
cgit v1.2.3