<commit_before>// --------------------
// Overlook
// Routing
// Function to create a route
// --------------------

// modules
var Promise = require('bluebird-extra'),
	_ = require('overlook-utils'),
	urlModule = require('url'),
	shimming = require('shimming');

// libraries
var cookies = require('../cookies'),
	transactions = require('../transactions'),
	runRoute = require('./runRoute');

// exports

module.exports = function(method, action, overlook) {
	// create route handler
	var app = overlook.app;
	app[method](action.path, function(req, res, callback) {
		return handleRoute(req, res, method, action, overlook)
		.catch(function(err) {
			// error in route handling - pass it back to express
			callback(err);
		})
		.done();
		// NB does not call callback on success - avoids request being handled by other routes
	});
	
	// set alternative handler for missing trailing slash
	if (_.endsWith(action.path, '/') && action.path != '/') {
		app[method](action.path.slice(0, -1), function(req, res, callback) { // jshint ignore:line
			res.redirect(req.url + '/');
			// NB callback not called
		});
	}
};

// functions

function handleRoute(req, res, method, action, overlook) {
	// get url + route
	var url = req.url;
	var route = action.route;
	
	// redirect if called with extra trailing slash
	if (_.endsWith(url, '/') && !_.endsWith(action.path, '/')) {
		res.redirect(req.url.slice(0, -1));
		return Promise.resolve();
	}
	
	// define 'this' for route function
	
	// base on route
	var obj = _.cloneAll(route);
	
	// add all action params
	_.extendAll(obj, action);
	
	/*
	//xxx delete this
	// add all action params (turning functions into promise-returners bound to this)
	_.forIn(action, function(param, paramName) {
		if (_.isFunction(param)) param = Promise.method(param).bind(this);
		obj[paramName] = param;
	});
	*/
	
	// add other params/methods
	_.extend(obj, {
		// params
		url: url,
		method: method,
		params: req.params,
		query: req.query,
		req: req,
		res: res,
		action: action,
		route: route,
		data: {},
		dataMain: undefined,
		
		// methods
		render: render,
		renderError: renderError,
		redirect: redirect,
		commit: transactions.commit,
		rollback: transactions.rollback,
		
		// state
		user: _.pop(req, 'user'),
		transaction: _.pop(req, 'transaction'),
		
		// app
		overlook: overlook,
		sequelize: overlook.sequelize,
		models: overlook.models
	});
	
	// for post requests, shim act function to record successful actions
	if (method == 'post') shimming.shimOne(obj, 'act', actShim);
	
	// run the route handler
	return runRoute.call(obj);
}

// act/ shims
function actShim(act) {
	// run act
	return Promise.bind(this)
	.then(act)
	.then(function(success) {
		// record success/failure in actResult
		_.defaultValue(this, 'actResult', {});
		this.actResult.success = success;
		
		// if success, record actData to DB
		/*
		if (success && !this.actLocal) {
			//xxx write this
		}
		*/
		
		// done
		return success;
	});
}

// rendering
function render(options, layoutOptions) {
	return renderDo.call(this, this.view, options, layoutOptions);
}

function renderError(view, options, layoutOptions) {
	return renderDo.call(this, '_compiled/_errors/' + view, options, layoutOptions);
}<commit_msg>Code tidy<commit_after>// --------------------
// Overlook
// Routing
// Function to create a route
// --------------------

// modules
var Promise = require('bluebird-extra'),
	_ = require('overlook-utils'),
	urlModule = require('url'),
	shimming = require('shimming');

// libraries
var cookies = require('../cookies'),
	transactions = require('../transactions'),
	runRoute = require('./runRoute');

// exports

module.exports = function(method, action, overlook) {
	// create route handler
	var app = overlook.app;
	app[method](action.path, function(req, res, callback) {
		return handleRoute(req, res, method, action, overlook)
		.catch(function(err) {
			// error in route handling - pass it back to express
			callback(err);
		})
		.done();
		// NB does not call callback on success - avoids request being handled by other routes
	});
	
	// set alternative handler for missing trailing slash
	if (_.endsWith(action.path, '/') && action.path != '/') {
		app[method](action.path.slice(0, -1), function(req, res, callback) { // jshint ignore:line
			res.redirect(req.url + '/');
			// NB callback not called
		});
	}
};

// functions

function handleRoute(req, res, method, action, overlook) {
	// get url + route
	var url = req.url,
		route = action.route;
	
	// redirect if called with extra trailing slash
	if (_.endsWith(url, '/') && !_.endsWith(action.path, '/')) {
		res.redirect(req.url.slice(0, -1));
		return Promise.resolve();
	}
	
	// define 'this' for route function
	
	// base on route
	var obj = _.cloneAll(route);
	
	// add all action params
	_.extendAll(obj, action);
	
	/*
	//xxx delete this
	// add all action params (turning functions into promise-returners bound to this)
	_.forIn(action, function(param, paramName) {
		if (_.isFunction(param)) param = Promise.method(param).bind(this);
		obj[paramName] = param;
	});
	*/
	
	// add other params/methods
	_.extend(obj, {
		// params
		url: url,
		method: method,
		params: req.params,
		query: req.query,
		req: req,
		res: res,
		action: action,
		route: route,
		data: {},
		dataMain: undefined,
		
		// methods
		render: render,
		renderError: renderError,
		redirect: redirect,
		commit: transactions.commit,
		rollback: transactions.rollback,
		
		// state
		user: _.pop(req, 'user'),
		transaction: _.pop(req, 'transaction'),
		
		// app
		overlook: overlook,
		sequelize: overlook.sequelize,
		models: overlook.models
	});
	
	// for post requests, shim act function to record successful actions
	if (method == 'post') shimming.shimOne(obj, 'act', actShim);
	
	// run the route handler
	return runRoute.call(obj);
}

// act/ shims
function actShim(act) {
	// run act
	return Promise.bind(this)
	.then(act)
	.then(function(success) {
		// record success/failure in actResult
		_.defaultValue(this, 'actResult', {});
		this.actResult.success = success;
		
		// if success, record actData to DB
		/*
		if (success && !this.actLocal) {
			//xxx write this
		}
		*/
		
		// done
		return success;
	});
}

// rendering

function render(options, layoutOptions) {
	return renderDo.call(this, this.view, options, layoutOptions);
}

function renderError(view, options, layoutOptions) {
	return renderDo.call(this, '_compiled/_errors/' + view, options, layoutOptions);
}
{'fraction_non_alphanumeric': 0.1617283950617284, 'fraction_numerical': 0.0012345679012345679, 'mean_word_length': 3.227658186562296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6019342', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2034, 'n_words': 574}