Create a gist now

Instantly share code, notes, and snippets.

delayed_job with custom attributes
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