'use strict';

var _ = require('lodash');

module.exports = function(bookshelf) {
  var baseExtend = bookshelf.Model.extend;
  var QueryBuilder = bookshelf.knex().constructor;

  bookshelf.Model.extend = function(protoProps) {
    var self = this;
    self.scopes = _.extend({}, self.scopes || {}, protoProps.scopes || {});

    Object.keys(self.scopes).forEach(function(property) {
      self.prototype[property] = function() {
        var _this = this;
        var passedInArguments = _.toArray(arguments);

        if (passedInArguments.length > 0 && passedInArguments[0] instanceof QueryBuilder) {
          self.scopes[property].apply(this, passedInArguments);

          return self;
        } else {
          return this.query(function(qb) {
            passedInArguments.unshift(qb);
            self.scopes[property].apply(_this, passedInArguments);
          });
        }
      };

      self[property] = function() {
        var model = this.forge();
        return model[property].apply(model, arguments);
      };
    });

    return baseExtend.apply(self, arguments);
  };

  var Model = bookshelf.Model.extend({

    scopes: null,

    initialize: function() {
      this.addScope();
    },

    addScope: function() {
      var self = this;
      if (self.scopes && self.scopes.default) {
        self.query(function(qb) {
          var args = [];
          args.push(qb);
          self.scopes.default.apply(self, args);
        });
      }
    },

    unscoped: function() {
      return this.resetQuery();
    }
  }, {
    unscoped: function() {
      return this.forge().unscoped();
    }
  });

  bookshelf.Model = Model;
};

@@ -124,26 +124,204 @@
 ;%0A  
-var QueryBuilder =
+// %60bookshelf.knex()%60 was deprecated in knex v0.8.0, use %60knex.queryBuilder()%60 instead if available%0A  var QueryBuilder = (bookshelf.knex.queryBuilder) ? bookshelf.knex.queryBuilder().constructor :
  boo

Support knex.queryBuilder() over deprecated knex()