Skip to content

Instantly share code, notes, and snippets.

@STRd6
Last active December 15, 2015 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save STRd6/5174638 to your computer and use it in GitHub Desktop.
Save STRd6/5174638 to your computer and use it in GitHub Desktop.
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
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