Class: ActiveSnapshot::SnapshotItem

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/active_snapshot/models/snapshot_item.rb

Instance Method Summary collapse

Instance Method Details

#objectObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_snapshot/models/snapshot_item.rb', line 17

def object
  return @object if @object

  if ActiveSnapshot.config.storage_method_serialized_json?
    # for legacy active_snapshot configurations only
    @object = self[:object] ? JSON.parse(self[:object]) : {}
  elsif ActiveSnapshot.config.storage_method_yaml?
    # for legacy active_snapshot configurations only
    yaml_method = YAML.respond_to?(:unsafe_load) ? :unsafe_load : :load

    @object = self[:object] ? YAML.public_send(yaml_method, self[:object]) : {}
  else
    @object = self[:object]
  end
end

#object=(h) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_snapshot/models/snapshot_item.rb', line 33

def object=(h)
  @object = nil

  if ActiveSnapshot.config.storage_method_serialized_json?
    # for legacy active_snapshot configurations only
    self[:object] = h.to_json
  elsif ActiveSnapshot.config.storage_method_yaml?
    # for legacy active_snapshot configurations only
    self[:object] = YAML.dump(h)
  else
    self[:object] = h
  end
end

#restore_item!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/active_snapshot/models/snapshot_item.rb', line 47

def restore_item!
  ### Add any custom logic here

  if !item
    item_klass = item_type.constantize

    self.item = item_klass.new
  end

  object.each do |k,v|
    if item.respond_to?("#{k}=")
      item[k] = v
    else
      # database column was likely dropped since the snapshot was created
    end
  end

  item.save!(validate: false, touch: false)
end