Module: Delayed::PsychExt::ToRuby

Defined in:
lib/delayed/yaml_extensions.rb

Instance Method Summary collapse

Instance Method Details

#resolve_class(klass_name) ⇒ Object



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

def resolve_class(klass_name)
  return nil if klass_name.blank?

  klass_name.constantize
rescue
  super
end

#visit_Psych_Nodes_Scalar(object) ⇒ Object

rubocop:disable Naming/MethodName



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

def visit_Psych_Nodes_Scalar(object) # rubocop:disable Naming/MethodName
  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