Created
June 26, 2012 20:59
-
-
Save romanmt/2998966 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| Promotion.pre('save', true, function(next, done) { | |
| next(); | |
| var self = this; | |
| short.generate(settings.baseURL + '/promotion/' + this._id + '/show', function(err, url) { | |
| self.url = settings.baseURL + '/' + url.hash; | |
| done(); | |
| }) | |
| }) | |
| Promotion.pre('save', true, function(next, done) { | |
| next() | |
| Release.update({_id: this._release}, {'$push': {_promotions: this._id}}, | |
| function(err, count) { | |
| if(err) throw err; | |
| done(); | |
| } | |
| ); | |
| }) | |
| Promotion.pre('save', true, function(next, done) { | |
| next() | |
| User.update({_id: this._promoter}, {'$push': {_promotions: this._id}}, | |
| function(err, count) { | |
| if(err) throw err; | |
| done(); | |
| } | |
| ); | |
| }) |
This file contains hidden or 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
| Promotion.statics.createFor = function createFor(promoter, release, callback) { | |
| var promotion = new this({ | |
| _promoter: promoter, | |
| _release: release | |
| }); | |
| short.generate(settings.baseURL + '/promotion/' + promotion._id + '/show', function(err, url) { | |
| promotion.url = settings.baseURL + '/' + url.hash | |
| promotion.save(function(err) { | |
| var fields = {'$push': {_promotions: promotion._id}}; | |
| async.series([function(cb) { | |
| User.update({_id: promoter}, fields, cb) | |
| }, function(cb) { | |
| Release.update({_id: release}, fields, cb) | |
| }], | |
| callback(err, promotion)) | |
| }); | |
| }) | |
| } |
This file contains hidden or 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 app = require('test_helper') | |
| describe('Promotion', function() { | |
| var user = {} | |
| , release = {} | |
| var fixtures = { | |
| user: function(cb) { | |
| User.create({displayName: 'Bob Barker'}, cb) | |
| }, | |
| release: function(cb) { | |
| Release.create({title: 'Golden Oldies'}, cb) | |
| } | |
| } | |
| before(function(done) { | |
| async.series(fixtures, function(err, results) { | |
| if(err) throw err; | |
| user = results.user; | |
| release = results.release; | |
| done(); | |
| }) | |
| }) | |
| //after(function(done) { | |
| //mongoose.connection.db.executeDbCommand( {dropDatabase:1}, | |
| //function(err, result) { | |
| //if(err) throw err; | |
| //done(); | |
| //}) | |
| //}) | |
| describe('#save', function() { | |
| it("updates the user object", function(done) { | |
| var promotion = new Promotion({_promoter: user._id, _release: release._id}) | |
| promotion.save(function(err) { throw err; }) | |
| User.findById(user._id, function(err, u) { | |
| if(err) throw err; | |
| _.last(u._promotions).should.eql(promotion._id) | |
| done(); | |
| }) | |
| }) | |
| it("updates the release object", function(done) { | |
| var promotion = new Promotion({_promoter: user._id, _release: release._id}) | |
| promotion.save(function(err) { throw err; }) | |
| Release.findById(release._id, function(err, r) { | |
| if(err) throw err; | |
| _.last(r._promotions).should.eql(promotion._id) | |
| done(); | |
| }) | |
| }) | |
| it("creates a short url", function(done) { | |
| var promotion = new Promotion({_promoter: user._id, _release: release._id}) | |
| var longURL = 'http://localhost:3001/promotion/'+promotion._id+'/show' | |
| promotion.save(function(err) { | |
| inspect(err) | |
| throw err; | |
| }); | |
| Promotion.findOne(promotion, function(err, p) { | |
| if(err) throw err; | |
| short.retrieve(_.strRightBack(p.url, '/'), function(err, url) { | |
| done() | |
| }) | |
| }) | |
| }) | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment