Permalink
Browse files

Autoload missing classes for serialized columns.

Fixes rails#1585.
  • Loading branch information...
alisdair committed Jan 26, 2013
1 parent 0c58848 commit 83805568af9c7b1250fed1a4ccf69f622de8e784
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
* Attempt to autoload missing classes when loading a YAML serialized
column. Fix #1585.
*Alisdair McDiarmid*
* Fix handling of dirty time zone aware attributes
Previously, when `time_zone_aware_attributes` were enabled, after
@@ -34,6 +34,12 @@ def load(yaml)
obj ||= object_class.new if object_class != Object
obj
rescue ArgumentError => e
# Invoke the autoloader and try again if object's class is undefined
if e.message =~ /undefined class\/module (.*)$/
$1.constantize rescue return yaml
end
return load(yaml)
rescue *RESCUE_ERRORS
yaml
end
@@ -234,4 +234,37 @@ def test_serialize_attribute_can_be_serialized_in_an_integer_column
person = person.reload
assert_equal(insures, person.insures)
end
def test_serialize_autoloading
with_autoloaded_models do
Topic.serialize(:content, Array)
topic = topics(:third)
assert_equal(Array, topic.content.class)
end
end
def test_serialize_autoloading_with_missing_class
with_autoloaded_models do
Topic.serialize(:content, Array)
topic = topics(:fourth)
assert_equal(String, topic.content.class)
end
end
private
def with_autoloaded_models
path = File.join(File.dirname(__FILE__), "../models")
if ActiveSupport::Dependencies.autoload_paths.include?(path)
yield
else
begin
ActiveSupport::Dependencies.autoload_paths << path
yield
ensure
ActiveSupport::Dependencies.autoload_paths.reject! {|p| p == path}
ActiveSupport::Dependencies.clear
end
end
end
end
@@ -26,7 +26,7 @@ third:
title: The Third Topic of the day
author_name: Carl
written_on: 2012-08-12t20:24:22.129346+00:00
content: I'm a troll
content: "---\n- !ruby/object:AutoLoadMe\n name: 'Testing'\n"
approved: true
replies_count: 1
@@ -35,8 +35,7 @@ fourth:
title: The Fourth Topic of the day
author_name: Carl
written_on: 2006-07-15t15:28:00.0099+01:00
content: Why not?
content: "---\n- !ruby/object:ThisIsAnUnlikelyClassName\n name: 'Testing'\n"
approved: true
type: Reply
parent_id: 3
@@ -0,0 +1,3 @@
class AutoLoadMe
attr_accessor :name
end

0 comments on commit 8380556

Please sign in to comment.