Module: Mongoid::History::Tracker
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid/history/tracker.rb
Instance Method Summary collapse
- #affected ⇒ HashWithIndifferentAccess deprecated Deprecated.
- #redo!(modifier = nil) ⇒ Object
- #redo_attr(modifier) ⇒ Object
- #trackable ⇒ Object
- #trackable_parent ⇒ Object
-
#trackable_parent_class ⇒ Class
Returns the class of the trackable, irrespective of whether the trackable object has been destroyed.
- #trackable_parents ⇒ Object
- #trackable_root ⇒ Object
-
#tracked_changes ⇒ HashWithIndifferentAccess
Outputs a :from, :to hash for each affected field.
-
#tracked_edits ⇒ HashWithIndifferentAccess
Outputs summary of edit actions performed: :add, :modify, :remove, or :array.
- #undo!(modifier = nil) ⇒ Object
- #undo_attr(modifier) ⇒ Object
Instance Method Details
#affected ⇒ HashWithIndifferentAccess
Similar to #tracked_changes, but contains only a single value for each affected field:
- :create and :update return the modified values
- :destroy returns original values
Included for legacy compatibility.
141 142 143 144 145 146 147 |
# File 'lib/mongoid/history/tracker.rb', line 141 def affected target = action.to_sym == :destroy ? :from : :to @affected ||= tracked_changes.inject(HashWithIndifferentAccess.new) { |h, (k, v)| h[k] = v[target] h } end |
#redo!(modifier = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mongoid/history/tracker.rb', line 37 def redo!(modifier = nil) if action.to_sym == :destroy re_destroy elsif action.to_sym == :create re_create elsif Mongoid::History.mongoid3? trackable.update_attributes!(redo_attr(modifier), without_protection: true) else trackable.update_attributes!(redo_attr(modifier)) end end |
#redo_attr(modifier) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/mongoid/history/tracker.rb', line 60 def redo_attr(modifier) redo_hash = affected.easy_unmerge(original) redo_hash.easy_merge!(modified) modifier_field = trackable.[:modifier_field] redo_hash[modifier_field] = modifier localize_keys(redo_hash) end |
#trackable ⇒ Object
72 73 74 |
# File 'lib/mongoid/history/tracker.rb', line 72 def trackable @trackable ||= trackable_parents_and_trackable.last end |
#trackable_parent ⇒ Object
80 81 82 |
# File 'lib/mongoid/history/tracker.rb', line 80 def trackable_parent @trackable_parent ||= trackable_parents_and_trackable[-2] end |
#trackable_parent_class ⇒ Class
Returns the class of the trackable, irrespective of whether the trackable object has been destroyed.
153 154 155 |
# File 'lib/mongoid/history/tracker.rb', line 153 def trackable_parent_class association_chain.first["name"].constantize end |
#trackable_parents ⇒ Object
76 77 78 |
# File 'lib/mongoid/history/tracker.rb', line 76 def trackable_parents @trackable_parents ||= trackable_parents_and_trackable[0, -1] end |
#trackable_root ⇒ Object
68 69 70 |
# File 'lib/mongoid/history/tracker.rb', line 68 def trackable_root @trackable_root ||= trackable_parents_and_trackable.first end |
#tracked_changes ⇒ HashWithIndifferentAccess
Outputs a :from, :to hash for each affected field. Intentionally excludes fields which are not tracked, even if there are tracked values for such fields present in the database.
90 91 92 93 94 95 |
# File 'lib/mongoid/history/tracker.rb', line 90 def tracked_changes @tracked_changes ||= (modified.keys | original.keys).inject(HashWithIndifferentAccess.new) do |h, k| h[k] = { from: original[k], to: modified[k] }.delete_if { |_, vv| vv.nil? } h end.delete_if { |k, v| v.blank? || !trackable_parent_class.tracked_field?(k) } end |
#tracked_edits ⇒ HashWithIndifferentAccess
Outputs summary of edit actions performed: :add, :modify, :remove, or :array. Does deep comparison of arrays. Useful for creating human-readable representations of the history tracker. Considers changing a value to ‘blank’ to be a removal.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/mongoid/history/tracker.rb', line 106 def tracked_edits @tracked_edits ||= tracked_changes.inject(HashWithIndifferentAccess.new) do |h, (k, v)| unless v[:from].blank? && v[:to].blank? if v[:from].blank? h[:add] ||= {} h[:add][k] = v[:to] elsif v[:to].blank? h[:remove] ||= {} h[:remove][k] = v[:from] else if v[:from].is_a?(Array) && v[:to].is_a?(Array) h[:array] ||= {} old_values = v[:from] - v[:to] new_values = v[:to] - v[:from] h[:array][k] = { add: new_values, remove: old_values }.delete_if { |_, vv| vv.blank? } else h[:modify] ||= {} h[:modify][k] = v end end end h end end |
#undo!(modifier = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mongoid/history/tracker.rb', line 25 def undo!(modifier = nil) if action.to_sym == :destroy re_create elsif action.to_sym == :create re_destroy elsif Mongoid::History.mongoid3? trackable.update_attributes!(undo_attr(modifier), without_protection: true) else trackable.update_attributes!(undo_attr(modifier)) end end |
#undo_attr(modifier) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mongoid/history/tracker.rb', line 49 def undo_attr(modifier) undo_hash = affected.easy_unmerge(modified) undo_hash.easy_merge!(original) modifier_field = trackable.[:modifier_field] undo_hash[modifier_field] = modifier (modified.keys - undo_hash.keys).each do |k| undo_hash[k] = nil end localize_keys(undo_hash) end |