/**
 * @fileOverview CRUD controller.
 */
var fs = require('fs');

var __ = require('lodash');
var jade = require('jade');

var CrudCmd = require('./controller-crud-base');
var tplHelpers = require('./tpl-helpers.js');

/**
 * The CRUD Controller
 *
 * @param {Entity} Entity An Entity instance or ctor.
 * @param {string} baseUrl The base url.
 * @param {Object=} optOpts Optionally define options.
 * @contructor
 * @extends {crude.CrudCmd}
 */
var CrudCtrl = module.exports = CrudCmd.extend(function(Entity, baseUrl, optOpts) {
  /** @type {Object} define default options */
  this.opts = {
    baseUrl: baseUrl,
    urlField: 'localUrl',
    nameField: 'name',
    idField: 'id',
    // A jade view
    layoutView: null,
    // The edit / create view.
    editView: null,
    // show the doc id
    showId: false,
    // show full path for netsted paths
    expandPaths: false,
    // paths to exclude when displaying
    viewExcludePaths: [],
  };
  /** @type {?string} The base url for the resource */
  this.baseUrl = baseUrl;

  if (__.isObject(optOpts)) {
    this.opts = __.defaults(optOpts, this.opts);
  }

  // set default view template locations
  this.views = {
    add: __dirname + '/../views/add.jade',
    view: __dirname + '/../views/view.jade',
    list: __dirname + '/../views/list.jade',
    edit: __dirname + '/../views/edit.jade',
  };

  this.compiled = {};
  var self = this;
  __.forOwn(this.views, function(tpl, key) {
    fs.readFile(tpl, function(err, data){
      if (err) {
        console.error('Error Reading file "' + tpl + '", error:', err);
        return;
      }
      self.compiled[key] = jade.compile(data, {
        filename: tpl
      });

    });
  });
});

/**
 * Mark the proto to check for proper inheritance of methods,
 * we do this so we can lax comparison by not relying 100% on "instanceof".
 *
 * @type {boolean}
 * @protected
 */
CrudCtrl.prototype._isCrude = true;

/**
 * Insert at the beginning of all routes the provided middleware.
 *
 * @param  {Function} middleware The middleware to insert.
 */
CrudCtrl.prototype.unshiftAllRoutes = function(middleware) {
  this.create.unshift(middleware);
  this.createView.unshift(middleware);
  this.readOne.unshift(middleware);
  this.readList.unshift(middleware);
  this.update.unshift(middleware);
  this.updateView.unshift(middleware);
  this.delete.unshift(middleware);
};

/**
 * Insert at the end of all routes the provided middleware.
 *
 * @param  {Function} middleware The middleware to insert.
 */
CrudCtrl.prototype.pushAllRoutes = function(middleware) {
  this.create.push(middleware);
  this.createView.push(middleware);
  this.readOne.push(middleware);
  this.readList.push(middleware);
  this.update.push(middleware);
  this.updateView.push(middleware);
  this.delete.push(middleware);
};

/**
 * Getter for baseUrl variable, overwrite if custom routing is required.
 *
 * @param {Object} req The request Object.
 * @return {string} The baseUrl.
 */
CrudCtrl.prototype.getBaseUrl = function(/* req */) {
  return this.baseUrl;
};

/**
 * Prepare the response object for each request, an internal middleware.
 *
 * @param {Object} req The request Object.
 * @param {Object} res The response Object.
 * @param {Function} next callback.
 * @protected
 */
CrudCtrl.prototype._prepResponse = function(req, res, next) {
  res.locals.opts = this.opts;
  res.locals.schema = this.entity.getSchema();
  res.locals.currentUser = req.user;
  // all template functions
  res.locals.fn = {};
  __.extend(res.locals.fn, tplHelpers);
  next();
};

/**
 * Process incoming POST vars, we will:
 *  * Remove all vars starting with underscore ( _ ).
 *
 *
 * This is not a validation step, just forbid "private" keys from passing.
 *
 * @param  {Object} params Hash with key / value pairs
 * @return {Object} a processed object.
 */
CrudCtrl.prototype.process = function(params) {
  var outObj = {};
  __.forOwn(params, function(value, key){
    if ('_' !== key.charAt(0)) {
      outObj[key] = value;
    }
  });

  return outObj;
};

@@ -301,49 +301,8 @@
 or.%0A
- * @param %7Bstring%7D baseUrl The base url.%0A
  * @
@@ -462,25 +462,16 @@
 (Entity,
- baseUrl,
  optOpts
@@ -550,23 +550,23 @@
 aseUrl: 
-baseUrl
+'crude'
 ,%0A    ur
@@ -899,89 +899,8 @@
   %7D;
-%0A  /** @type %7B?string%7D The base url for the resource */%0A  this.baseUrl = baseUrl;
 %0A%0A  
@@ -2895,16 +2895,180 @@
 rn this.
+opts.baseUrl;%0A%7D;%0A%0A/**%0A * Set the baseUrl.%0A *%0A * @param %7Bstring%7D baseUrl The base url.%0A */%0ACrudCtrl.prototype.setBaseUrl = function(baseUrl) %7B%0A  this.opts.baseUrl = 
 baseUrl;

change ctor signature