Permalink
Browse files

column types are passed from the result set to the instantiated AR ob…

…ject
  • Loading branch information...
1 parent 9c07e38 commit 9bcb9cd55f92fb0252b3ab93d068e0b336727af0 @tenderlove tenderlove committed Feb 1, 2012
@@ -165,6 +165,7 @@ def relation #:nodoc:
# User.new({ :first_name => 'Jamie', :is_admin => true }, :without_protection => true)
def initialize(attributes = nil, options = {})
@attributes = self.class.initialize_attributes(self.class.column_defaults.dup)
+ @columns_hash = self.class.columns_hash.dup
init_internals
@@ -190,6 +191,8 @@ def initialize(attributes = nil, options = {})
# post.title # => 'hello world'
def init_with(coder)
@attributes = self.class.initialize_attributes(coder['attributes'])
+ @columns_hash = self.class.columns_hash.merge(coder['column_types'] || {})
+
init_internals
@@ -341,8 +344,6 @@ def init_internals
@attributes[pk] = nil unless @attributes.key?(pk)
- @columns_hash = self.class.columns_hash.dup
-
@relation = nil
@aggregation_cache = {}
@association_cache = {}
@@ -62,7 +62,7 @@ def sti_name
# Finder methods must instantiate through this method to work with the
# single-table inheritance model that makes it possible to create
# objects of different types from the same table.
- def instantiate(record)
+ def instantiate(record, column_types = {})
sti_class = find_sti_class(record[inheritance_column])
record_id = sti_class.primary_key && record[sti_class.primary_key]
@@ -77,7 +77,8 @@ def instantiate(record)
IdentityMap.add(instance)
end
else
- instance = sti_class.allocate.init_with('attributes' => record)
+ instance = sti_class.allocate.init_with('attributes' => record,
+ 'column_types' => column_types)
end
instance
@@ -1,4 +1,5 @@
require 'active_support/core_ext/module/delegation'
+require 'active_support/deprecation'
module ActiveRecord
module Querying
@@ -36,7 +37,15 @@ module Querying
def find_by_sql(sql, binds = [])
logging_query_plan do
result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds)
- result_set.map { |record| instantiate(record) }
+ column_types = {}
+
+ if result_set.respond_to? :column_types
+ column_types = result_set.column_types
+ else
+ ActiveSupport::Deprecation.warn "the object returned from `select_all` must respond to `column_types`"
+ end
+
+ result_set.map { |record| instantiate(record, column_types) }
end
end
@@ -49,6 +49,10 @@ def initialize_copy(other)
@hash_rows = nil
end
+ def column_types
+ {}
+ end
+
private
def hash_rows
@hash_rows ||= @rows.map { |row|
@@ -1959,4 +1959,8 @@ def test_active_record_super
def test_table_name_with_2_abstract_subclasses
assert_equal "photos", Photo.table_name
end
+
+ def test_rawr
+ assert_equal 10, Topic.select('10 as tenderlove').first.tenderlove
+ end
end

0 comments on commit 9bcb9cd

Please sign in to comment.