Last active
December 15, 2015 00:39
-
-
Save STRd6/5174638 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
findCondenseOrCreate = (normalizedTag, name, fn) -> | |
query = new Parse.Query("Category") | |
query.equalTo "normalizedTag", normalizedTag | |
query.find | |
success: (results) -> | |
[category, duplicates...] = results | |
# We could try and collect numbers, but we really | |
# don't care since we should be recounting anyway | |
_.each duplicates, (duplicate) -> | |
console.log "Destroying duplicate #{duplicate.get('normalizedTag')}" | |
duplicate.destroy() | |
if category | |
fn(category) | |
else | |
category = new Parse.Object("Category") | |
category.set | |
normalizedTag: normalizedTag | |
name: name | |
fn(category) | |
error: throwWithMessage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var findCondenseOrCreate, | |
__slice = [].slice; | |
findCondenseOrCreate = function(normalizedTag, name, fn) { | |
var query; | |
query = new Parse.Query("Category"); | |
query.equalTo("normalizedTag", normalizedTag); | |
return query.find({ | |
success: function(results) { | |
var category, duplicates; | |
category = results[0], duplicates = 2 <= results.length ? __slice.call(results, 1) : []; | |
_.each(duplicates, function(duplicate) { | |
console.log("Destroying duplicate " + (duplicate.get('normalizedTag'))); | |
return duplicate.destroy(); | |
}); | |
if (category) { | |
return fn(category); | |
} else { | |
category = new Parse.Object("Category"); | |
category.set({ | |
normalizedTag: normalizedTag, | |
name: name | |
}); | |
return fn(category); | |
} | |
}, | |
error: throwWithMessage | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment