Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/delayed/yaml_extensions.rb,
lib/delayed/backend/active_record.rb

Overview

Now, tell YAML how to intelligently load ActiveRecord objects, using the database rather than just serializing their attributes to the YAML. This ensures the object is up to date when we use it in the job.

Direct Known Subclasses

Delayed::Backend::ActiveRecord::Job

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_for_delayed_job(id) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/delayed/backend/active_record.rb', line 2

def self.load_for_delayed_job(id)
  if id
    find(id)
  else
    super
  end
end

.yaml_new(klass, tag, val) ⇒ Object



65
66
67
68
69
# File 'lib/delayed/yaml_extensions.rb', line 65

def self.yaml_new(klass, tag, val)
  klass.find(val)
rescue ActiveRecord::RecordNotFound
  raise Delayed::Backend::RecordNotFound, "Couldn't find #{klass} with id #{val.inspect}"
end

Instance Method Details

#encode_with(coder) ⇒ Object



58
59
60
61
62
63
# File 'lib/delayed/yaml_extensions.rb', line 58

def encode_with(coder)
  if id.nil?
    raise("Can't serialize unsaved ActiveRecord object for delayed job: #{self.inspect}")
  end
  coder.scalar("!ruby/ActiveRecord:#{self.class.name}", id.to_s)
end

#to_yaml(opts = {}) ⇒ Object



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

def to_yaml(opts = {})
  if id.nil?
    raise("Can't serialize unsaved ActiveRecord object for delayed job: #{self.inspect}")
  end
  YAML.quick_emit(self.object_id, opts) do |out|
    out.scalar(taguri, id.to_s)
  end
end