aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/column.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-06-06 09:43:09 -0600
committerSean Griffin <sean@thoughtbot.com>2014-06-07 07:03:42 -0600
commit2dca1ba039eb0d1adad089134749a5093b481666 (patch)
tree7ffd41b412443ed83fc9ea112fe196b48d0b8490 /activerecord/lib/active_record/connection_adapters/column.rb
parentecd4151aa829214c7b10f24bc5eca194089b4319 (diff)
downloadrails-2dca1ba039eb0d1adad089134749a5093b481666.tar.gz
rails-2dca1ba039eb0d1adad089134749a5093b481666.tar.bz2
rails-2dca1ba039eb0d1adad089134749a5093b481666.zip
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.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/column.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/column.rb15
1 files changed, 11 insertions, 4 deletions
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