Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid history tracker with custom changes_method #159

Open
sivagollapalli opened this issue Jul 11, 2016 · 6 comments
Open

Invalid history tracker with custom changes_method #159

sivagollapalli opened this issue Jul 11, 2016 · 6 comments
Labels

Comments

@sivagollapalli
Copy link
Contributor

      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 dblock added the bug label Jul 11, 2016
@sivagollapalli
Copy link
Contributor Author

@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?

@dblock
Copy link
Collaborator

dblock commented Jul 26, 2016

But wasn't that the whole point? I guess I don't understand what the original intent was anymore :)

@sivagollapalli
Copy link
Contributor Author

@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?

@dblock
Copy link
Collaborator

dblock commented Jul 31, 2016

Backup. What did you want to happen in your initial example? I mean what was the purpose of changes.merge(key: "Save history-#{key}")?

@sivagollapalli
Copy link
Contributor Author

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.

@dblock
Copy link
Collaborator

dblock commented Aug 1, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants