Module: TrackChanges::Model::ClassMethods

Defined in:
lib/track_changes/model.rb

Instance Method Summary collapse

Instance Method Details

#default_track_changes_fieldsObject



36
37
38
# File 'lib/track_changes/model.rb', line 36

def default_track_changes_fields
  attribute_names - stored_attributes.keys.map(&:to_s) + stored_attributes.values.flatten.map(&:to_s)
end

#persist_tracked_changes(track_changes_by: nil) ⇒ Object

Record a diff and update the snapshot for all records in the scope This can be used to record a diff after an ‘update_all`.



51
52
53
54
55
56
# File 'lib/track_changes/model.rb', line 51

def persist_tracked_changes(track_changes_by: nil)
  find_each do |record|
    record.track_changes_by = track_changes_by
    record.persist_tracked_changes
  end
end

#snapshot_allObject

Create snapshots for all records so that the next changes made are captured This can be used to init track changes on existing records



42
43
44
45
46
47
# File 'lib/track_changes/model.rb', line 42

def snapshot_all
  # Update existing snapshots
  joins(:snapshot).find_each {|record| record.snapshot.update }
  # Create new snapshots
  where.not(:id => joins(:snapshot)).find_each(&:create_snapshot)
end

#track_changes_fieldsObject

Returns the method names to call to fetch the fields tracked for changes



25
26
27
28
29
30
31
32
33
34
# File 'lib/track_changes/model.rb', line 25

def track_changes_fields
  fields =  Array(track_changes_options[:only]).collect(&:to_s).presence || default_track_changes_fields
  fields -= Array(track_changes_options[:except]).collect(&:to_s)
  fields += Array(track_changes_options[:methods]).collect(&:to_s)
  fields -= ['created_at', 'updated_at'] unless track_changes_options[:track_timestamps]
  fields -= [primary_key] unless track_changes_options[:track_primary_key]
  fields -= [locking_column] unless track_changes_options[:track_locking_column]

  return fields.uniq
end