public
Created

delayed_job with custom attributes

  • Download Gist
delayed_job custom attributes.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.