Module: HierarchySnapshot::ActiveRecord::InstanceMethods

Defined in:
lib/hierarchy_snapshot/active_record.rb

Instance Method Summary collapse

Instance Method Details

#hierarchy_snapshot_dataObject



48
49
50
# File 'lib/hierarchy_snapshot/active_record.rb', line 48

def hierarchy_snapshot_data
  hierarchy_snapshot_data_for(self, hierarchy_snapshot_config).to_json
end

#hierarchy_snapshot_data_for(obj, config) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hierarchy_snapshot/active_record.rb', line 52

def hierarchy_snapshot_data_for(obj, config)
  {}.tap do |map|
    config.attrs.each do |sym|
      map[sym] = obj.send(sym)
    end
    config.many(nil).each do |sym, config|
      map[sym] = obj.send(sym).to_a.collect do |child|
        hierarchy_snapshot_data_for(child, config)
      end
    end
  end
end

#hierarchy_snapshot_saveObject



39
40
41
42
43
44
45
46
# File 'lib/hierarchy_snapshot/active_record.rb', line 39

def hierarchy_snapshot_save
  return if Thread.current[:skip_hierarchy_snapshot]

  if hierarchy_snapshot_config.require_user? and snapshot_user.nil?
    raise 'No snapshot user was specified, but one is required'
  end
  Snapshot.create :source => self, :data => hierarchy_snapshot_data, :user => snapshot_user
end

#with_snapshot(user = nil) ⇒ Object



29
30
31
32
33
# File 'lib/hierarchy_snapshot/active_record.rb', line 29

def with_snapshot(user=nil)
  self.snapshot_user = user
  yield
  hierarchy_snapshot_save
end

#without_snapshot(&bl) ⇒ Object



35
36
37
# File 'lib/hierarchy_snapshot/active_record.rb', line 35

def without_snapshot(&bl)
  self.class.without_snapshot(&bl)
end