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

Adding List 'track' option (replaces #490) #523

Merged

Conversation

estilles
Copy link

As promised, here's my PR for the List track option, which adds plugin that automatically tracks when and who created and last updated an item. This PR replaces #490.

The feature can be enabled for all tracking fields, with all their default names, by setting track to true.

var List = keystone.List('MyList', { track: true });

I added this as a shortcut to setting track to { createdAt: true, createdBy: true, updatedAt: true, updatedBy: true}, which I thought was a bit verbose and probably the most common use case.

You can selectively enabled each tracking field by setting track an object containing the keys for the desired fields set to true. When using this method, fields will default to false, so any omitted fields will not be enabled.

var List = keystone.List('MyList, { 
    track: { createdAt: true, createdBy: true }
});

You can also selective specify custom field names by setting the desired field keys to a string representing the desired name.

var List = keystone.List('MyList, { 
    track: { 
        createdAt: true,
        createdBy: 'whoCreatedThis',
        updatedAt: true, 
        updatedBy: 'whoUpdatedThis'
    }
});

As we agreed in #490, using List.addPattern('standard meta') enables the track option and maps createdOn and updatedOn to createdAt and updatedAt respectively. It also displays a message warning the developer that the feature is now deprecated.

I've also updated the Keystone docs with documentation for the track option. I included a caveat pointing out that createdBy and updatedBy are only updated when adding/updating items via the Admin UI (which uses the updateHandler()). I further explained that if developers wish to add/modify items from their own apps they must manually store the currently logged on user in the item prior to calling .save(). I used the following example:

var item = new List.model();
item.set({ field1: 'value1', field2: 'value2' });

item._req_user = req.user;

item.save();    

This addresses the issue of ._req_user not being available to pre and post save hooks when using .save().

Finally, I included a unit test for the feature. I refactored some common code into helper functions and updated the test for the ._req_user feature to use them as well.

That's it! Comments, suggestions and modifications are always welcome!

Johnny Estilles added 4 commits August 10, 2014 10:00
changed; "standard meta" pattern now enables list "track" option
added; "standard meta" pattern displays deprecation warning
added; helper functions for commonly used code
@Globegitter
Copy link
Contributor

@JohnnyEstilles Thanks for this PR, that's some genius work. Simple and effective.

@estilles
Copy link
Author

@Globegitter You're quite welcome! I aim to please. :-) I'm not sure about the genius part, but I appreciate the compliment. :-)

@JedWatson
Copy link
Member

This looks great, nice work @JohnnyEstilles!

JedWatson added a commit that referenced this pull request Aug 10, 2014
…-option

Adding List 'track' option (replaces #490)
@JedWatson JedWatson merged commit c9ec3ba into keystonejs:master Aug 10, 2014
@webteckie
Copy link
Contributor

Just a quick comment that I just updated to 0.2.26 and get the following warning (as expected) but the link to the List Options does not explain anything about this (as far as I can tell)--the documentation team should take note of this:


KeystoneJS: Deprecation Warning:

Use of List.addPattern("standard meta") is now deprecated and will be removed
in future versions of KeystoneJS. It has been replaced with the List "track" option.

See http://keystonejs.com/docs/database/#lists-options for more information.

@estilles
Copy link
Author

@webteckie, I submitted changes to the docs in my PR, but it looks like the website is still one build behind (on 0.2.25). Thanks for pointing that out.

@estilles estilles deleted the feature/adding-list-track-option branch August 15, 2014 12:41
@JedWatson
Copy link
Member

@webteckie @JohnnyEstilles sorry about that, I forgot to push the new docs build when I released 0.2.26 to npm. It's up now :)

@estilles
Copy link
Author

@JedWatson, cool! Thanks!

I mean, I already new how to use it, but I guess we should let the rest of the world. :-)

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

Successfully merging this pull request may close these issues.

4 participants