Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upRefactor saving highlights in AnnotationController #2762
Conversation
seanh
added
the
WIP
label
Dec 3, 2015
seanh
self-assigned this
Dec 3, 2015
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
seanh
Dec 3, 2015
Contributor
Needs tests, and once I have those I can do a couple more little refactors on top of this
|
Needs tests, and once I have those I can do a couple more little refactors on top of this |
seanh
added some commits
Dec 3, 2015
seanh
removed
the
WIP
label
Dec 3, 2015
seanh
removed their assignment
Dec 3, 2015
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
Ready for review |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
judell
Dec 3, 2015
Contributor
"But it ended up being a can of worms." No good deed goes unpunished.
|
"But it ended up being a can of worms." No good deed goes unpunished. |
nickstenning
reviewed
Dec 4, 2015
| // Highlights are always private. | ||
| model.permissions = permissions.private(); | ||
| model.$create().then(function() { | ||
| $rootScope.$emit('annotationCreated', model); |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
nickstenning
Dec 4, 2015
Contributor
So I don't think this should block merging this PR, but this smells really awful to me. AnnotationController shouldn't be responsible for firing these lifecycle events. Indeed, most of the rest of them have moved into annotationMapper, which makes me wonder why AnnotationController isn't using annotationMapper.createAnnotation(object) rather than ngResource.$create...
nickstenning
Dec 4, 2015
Contributor
So I don't think this should block merging this PR, but this smells really awful to me. AnnotationController shouldn't be responsible for firing these lifecycle events. Indeed, most of the rest of them have moved into annotationMapper, which makes me wonder why AnnotationController isn't using annotationMapper.createAnnotation(object) rather than ngResource.$create...
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
robertknight
Dec 4, 2015
Contributor
Indeed, we definitely don't want to have multiple places handling the actual creation of the object on the server if possible.
robertknight
Dec 4, 2015
Contributor
Indeed, we definitely don't want to have multiple places handling the actual creation of the object on the server if possible.
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
robertknight
reviewed
Dec 4, 2015
| assert.notCalled(annotation.$create); | ||
| }); | ||
| it('does not save old highlights on initialization', function() { |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
robertknight
Dec 4, 2015
Contributor
I'd suggest we call these "existing" rather than "old" for consistency with comments elsewhere
robertknight
Dec 4, 2015
Contributor
I'd suggest we call these "existing" rather than "old" for consistency with comments elsewhere
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
robertknight
Dec 4, 2015
Contributor
So overall:
- This is looking much clearer, the comments clarify things especially
- I'd agree with Nick's comments about
annotationMapper- it would be preferable to have the submission of annotations to the server only happen in one place - There is a lot of duplication in the tests (eg. in terms of repeated comments explaining the same thing about the consequences of setting different attributes on a new annotation) which is going to make refactoring harder in future. Extracting these into a helper could simplify that.
eg:
it('...', function () {
var annotation = createAnnotation({
// properties customizing the new annotation, everything
// else is set to a default
});
});|
So overall:
eg: it('...', function () {
var annotation = createAnnotation({
// properties customizing the new annotation, everything
// else is set to a default
});
}); |
seanh
added some commits
Dec 3, 2015
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
seanh
Dec 4, 2015
Contributor
I've factored out the annotation global from the tests to pave the way for removing duplication by adding helper functions for creating different kinds of annotation. Going to lunch now, will finish after..
|
I've factored out the |
seanh
added
the
WIP
label
Dec 4, 2015
seanh
self-assigned this
Dec 4, 2015
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
seanh
Dec 4, 2015
Contributor
@robertknight As long as you're happy that I ignored the old -> existing comment (I can't really be bothered to go through both files and change old -> existing everywhere unless you really want it), I think that's everything responded to
|
@robertknight As long as you're happy that I ignored the old -> existing comment (I can't really be bothered to go through both files and change old -> existing everywhere unless you really want it), I think that's everything responded to |
seanh commentedDec 3, 2015
This started out as me trying to do a small fix to
AnnotationController, I just wanted to move the line of code that automatically saves new highlights to the server on login out of a$watch()function and to remove some duplicated state (highlight,model.$highlight,vm.isHighlight(),vm.hasContent()...). But it ended up being a can of worms.The changes I've ended up with are mostly just refactoring the code for clarity but this does also fix at least one broken behaviour (creating highlights while logged out, then logging in). See list of use-cases below.
Code changes:
Rename local variable
highlighttonewlyCreatedByHighlightButtonand adda docstring - clarify what this piece of state actually means.
Yes I know this is a really long variable name but this state variable has a very particular meaning and is only used in a couple of places - I think the explicitness is justified here for clarity.
Fix
vm.isHighlight(): It now returnsfalsefor new annotations that theuser hasn't entered any text or tags for yet (instead of
true).Move the code for saving new highlights to the server into a
saveNewHighlight()function that's called on AnnotationControllerinstantiation instead of having it in a
$watch()function that's calledevery time the local variable
modelchanges.Also use the presence of
model.idto avoid re-saving highlights that havealready been saved - instead of abusing the
highlight(now
newlyCreatedByHighlightButton) variable and confusing what itrepresents.
Don't automatically open the annotation editor on Annotationcontroller
instantiation if the annotation is a highlight. This fixes this bug:
open on your highlight.
The intention is that highlights are simply saved to the server, not opened
for editing.
Annotations created while logged out, on the other hand, are not saved
to the server on login but are opened for editing on login.
Cases to test (these are all things that are either broken on master, or were broken at one point by the earlier versions of the code changes in this pull request:
P.S. In my opinion most of the complexity here is caused by using one directive / controller for many things (annotations, highlights, replies, page notes).