diff --git a/lib/delayed/backend/base.rb b/lib/delayed/backend/base.rb index f701a8e2b..f9db00226 100644 --- a/lib/delayed/backend/base.rb +++ b/lib/delayed/backend/base.rb @@ -68,7 +68,7 @@ def name def payload_object=(object) @payload_object = object - self.handler = object.to_yaml + self.handler = object.to_delayed_yaml end def payload_object diff --git a/lib/delayed/backend/shared_spec.rb b/lib/delayed/backend/shared_spec.rb index 229d8ba96..48eef1372 100644 --- a/lib/delayed/backend/shared_spec.rb +++ b/lib/delayed/backend/shared_spec.rb @@ -296,7 +296,7 @@ def create_job(opts = {}) describe "yaml serialization" do it "should reload changed attributes" do job = described_class.enqueue SimpleJob.new - yaml = job.to_yaml + yaml = job.to_delayed_yaml job.priority = 99 job.save YAML.load(yaml).priority.should == 99 @@ -304,7 +304,7 @@ def create_job(opts = {}) it "should raise deserialization error for destroyed records" do job = described_class.enqueue SimpleJob.new - yaml = job.to_yaml + yaml = job.to_delayed_yaml job.destroy lambda { YAML.load(yaml) }.should raise_error(Delayed::DeserializationError) end diff --git a/lib/delayed/serialization/active_record.rb b/lib/delayed/serialization/active_record.rb index d3affc4a4..b4976e79e 100644 --- a/lib/delayed/serialization/active_record.rb +++ b/lib/delayed/serialization/active_record.rb @@ -1,13 +1,18 @@ class ActiveRecord::Base - yaml_as "tag:ruby.yaml.org,2002:ActiveRecord" + def to_delayed_yaml( opts = {} ) + YAML::quick_emit( self, opts ) do |out| + out.map("tag:delayed_job.com,2010:ActiveRecord:#{self.class.name}", to_yaml_style) do |map| + map.add('id', id) + end + end + end +end - def self.yaml_new(klass, tag, val) - klass.find(val['attributes']['id']) +YAML.add_domain_type('delayed_job.com,2010', 'ActiveRecord') do |tag, value| + begin + type, model = YAML.read_type_class(tag, Kernel) + model.find(value['id']) rescue ActiveRecord::RecordNotFound raise Delayed::DeserializationError end - - def to_yaml_properties - ['@attributes'] - end end diff --git a/lib/delayed/yaml_ext.rb b/lib/delayed/yaml_ext.rb index 8d38d5b30..458267390 100644 --- a/lib/delayed/yaml_ext.rb +++ b/lib/delayed/yaml_ext.rb @@ -15,14 +15,14 @@ def to_yaml( opts = {} ) out.scalar(taguri, self.name, :plain) } end - + def yaml_tag_read_class(name) # Constantize the object so that ActiveSupport can attempt # its auto loading magic. Will raise LoadError if not successful. name.constantize name end - + end class Class @@ -38,3 +38,9 @@ def self.yaml_tag_read_class(name) "Struct::#{ name }" end end + +class Object + def to_delayed_yaml + to_yaml + end +end