Module: Delayed::PsychExt::ToRuby

Defined in:
lib/delayed/yaml_extensions.rb

Instance Method Summary collapse

Instance Method Details

#resolve_class(klass_name) ⇒ Object


52
53
54
55
56
57
# File 'lib/delayed/yaml_extensions.rb', line 52

def resolve_class(klass_name)
  return nil if !klass_name || klass_name.empty?
  klass_name.constantize
rescue
  super
end

#visit_Psych_Nodes_Scalar(object) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/delayed/yaml_extensions.rb', line 34

def visit_Psych_Nodes_Scalar(object)
  case object.tag
  when %r{^!ruby/ActiveRecord:(.+)$}
    begin
      klass = resolve_class(Regexp.last_match[1])
      klass.unscoped.find(object.value)
    rescue ActiveRecord::RecordNotFound
      raise Delayed::Backend::RecordNotFound, "Couldn't find #{klass} with id #{object.value.inspect}"
    end
  when "!ruby/Delayed::Periodic", "!ruby/object:Delayed::Periodic"
    # The 2nd ruby/object tag is no longer generated, but some existing
    # jobs use that tag. We can remove it later once those are all gone.
    Delayed::Periodic.scheduled[object.value] || raise(NameError, "job #{object.value} is no longer scheduled")
  else
    super
  end
end