From 2dca1ba039eb0d1adad089134749a5093b481666 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Fri, 6 Jun 2014 09:43:09 -0600 Subject: Don't query the database schema when calling `serialize` We need to decorate the types lazily. This is extracted to a separate API, as there are other refactorings that will be able to make use of it, and to allow unit testing the finer points more granularly. --- .../lib/active_record/connection_adapters/column.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters/column.rb') diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index 5e4e00bc64..23434df1fe 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -13,7 +13,7 @@ module ActiveRecord ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/ end - attr_reader :name, :default, :cast_type, :null, :sql_type, :default_function + attr_reader :name, :cast_type, :null, :sql_type, :default_function delegate :type, :precision, :scale, :limit, :klass, :accessor, :text?, :number?, :binary?, :serialized?, :changed?, @@ -35,7 +35,7 @@ module ActiveRecord @cast_type = cast_type @sql_type = sql_type @null = null - @default = extract_default(default) + @original_default = default @default_function = nil end @@ -51,8 +51,15 @@ module ActiveRecord Base.human_attribute_name(@name) end - def extract_default(default) - type_cast(default) + def default + @default ||= type_cast(@original_default) + end + + def with_type(type) + dup.tap do |clone| + clone.instance_variable_set('@default', nil) + clone.instance_variable_set('@cast_type', type) + end end end -- cgit v1.2.3