Skip to content

Instantly share code, notes, and snippets.

Created November 28, 2012 01:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4158475 to your computer and use it in GitHub Desktop.
Save anonymous/4158475 to your computer and use it in GitHub Desktop.
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