Created
November 28, 2012 01:33
-
-
Save anonymous/4158475 to your computer and use it in GitHub Desktop.
delayed_job with custom attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Psych | |
module Visitors | |
class ToRuby | |
def visit_Psych_Nodes_Mapping_with_class_init(object) | |
return revive(Psych.load_tags[object.tag], object) if Psych.load_tags[object.tag] | |
case object.tag | |
when /^!ruby\/ActiveRecord:(.+)$/ | |
klass = resolve_class($1) | |
payload = Hash[*object.children.map { |c| accept c }] | |
id = payload["attributes"][klass.primary_key] | |
begin | |
o = klass.unscoped.find(id) | |
if o.respond_to?(:init_with) | |
o.init_with payload | |
end | |
rescue ActiveRecord::RecordNotFound | |
raise Delayed::DeserializationError | |
end | |
when /^!ruby\/Mongoid:(.+)$/ | |
klass = resolve_class($1) | |
payload = Hash[*object.children.map { |c| accept c }] | |
begin | |
klass.find(payload["attributes"]["_id"]) | |
rescue Mongoid::Errors::DocumentNotFound | |
raise Delayed::DeserializationError | |
end | |
else | |
visit_Psych_Nodes_Mapping_without_class_init(object) | |
end | |
end | |
alias_method_chain :visit_Psych_Nodes_Mapping, :class_init | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment