Invalid history tracker with custom changes_method
it "should allow an alternate method to be specified on object creation" do
class CustomTracker < MyModel
field :key
track_history on: :key, changes_method: :my_changes, track_create: true, track_update: true
def my_changes
changes.merge(key: "Save history-#{key}")
end
end
m = CustomTracker.create(key: "on object creation")
history_track = m.history_tracks.last
expect(history_track.modified[:key]).to eq("Save history-on object creation")
p history_track.inspect
m.update_attributes(key: "object updated")
history_track = m.history_tracks.last
p history_track.inspect
end
The above spec yields following output:
History Tracker on creation:
#<Tracker _id: 5783a04ac5f169421e5905f9, created_at: 2016-07-11 13:34:02 UTC, updated_at: 2016-07-11 13:34:02 UTC, association_chain: [{\"name\"=>\"CustomTracker\", \"id\"=>BSON::ObjectId('5783a04ac5f169421e5905fa')}], modified: {\"key\"=>\"Save history-on object creation\"}, original: {}, version: 1, action: \"create\", scope: \"my_model\", modifier_id: nil>
History Tracker on updation:
"#<Tracker _id: 5783a04ac5f169421e5905fb, created_at: 2016-07-11 13:34:02 UTC, updated_at: 2016-07-11 13:34:02 UTC, association_chain: [{\"name\"=>\"CustomTracker\", \"id\"=>BSON::ObjectId('5783a04ac5f169421e5905fa')}], modified: {}, original: {\"key\"=>\"Save history-object updated\"}, version: 2, action: \"update\", scope: \"my_model\", modifier_id: nil>"
Updation history trackers inserts with empty modified value. Actually it shouldn't empty.
@dblock Just now I checked, since I defined changes.merge(key: "Save history-#{key}") in my model it overrides changes that are there on the object. So, modified value becomes empty. Is this valid case?
But wasn't that the whole point? I guess I don't understand what the original intent was anymore :)
@dblock I think we can't fix this as the user modifies changes happened on the object using changes.merge(key: "Save history-#{key}"). Do you think the same?
Backup. What did you want to happen in your initial example? I mean what was the purpose of changes.merge(key: "Save history-#{key}")?
The purpose of changes.merge(key: "Save history-#{key}")? is that I want to append Save history to the values before inserting into history tracker. But when I do changes.merge(key: "Save history-#{key}")? it inserts empty history tracker with modified value as empty while updating an object.
Ok, I understand. I think this should work. I would track where the modified values are being inserted and see why this is happening, I would expect it to work, too.
Maybe write actual specs first where you do a merge when key_changed? for all the CRUD scenarios? I can try to help fix.