<commit_before>'use strict';

var _ = require('lodash');

module.exports = function(bookshelf) {
  var base = bookshelf.Model;
  var baseExtend = bookshelf.Model.extend;
  // `bookshelf.knex()` was deprecated in knex v0.8.0, use `knex.queryBuilder()` instead if available
  var QueryBuilder = (bookshelf.knex.queryBuilder) ? bookshelf.knex.queryBuilder().constructor : bookshelf.knex().constructor;

  bookshelf.Model.extend = function(protoProps, constructorProps) {
    var model = baseExtend.apply(this, arguments);

    model.prototype.scopes = model.prototype.scopes || {};

    _.defaults(model.prototype.scopes, this.prototype.scopes || {});

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

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

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

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

    _.each(['hasMany', 'hasOne', 'belongsToMany', 'morphOne', 'morphMany',
      'belongsTo', 'through'], function(method) {
      var original = model.prototype[method];
      model.prototype[method] = function() {
        var relationship = original.apply(this, arguments);
        var target = relationship.model || relationship.relatedData.target;
        var originalSelectConstraints = relationship.relatedData.selectConstraints;

        if (target.prototype.scopes) {
          relationship.relatedData.selectConstraints = function(knex, options) {
            originalSelectConstraints.apply(this, arguments);
            if (target.prototype.scopes.default) {
              if (!knex.appliedDefault) {
                knex.appliedDefault = true;
                target.prototype.scopes.default.apply(this, [knex, options]);
              }
            }
          };

          Object.keys(target.prototype.scopes).forEach(function(key) {
            relationship[key] = function() {
              var passedInArguments = _.toArray(arguments);
              var currentSelectConstraints = relationship.relatedData.selectConstraints;
              relationship.relatedData.selectConstraints = function(knex, options) {
                currentSelectConstraints.apply(this, arguments);
                passedInArguments.unshift(knex);
                target.prototype.scopes[key].apply(target.prototype, passedInArguments);
              };
              return relationship;
            };
          });

          relationship.unscoped = function() {
            relationship.relatedData.selectConstraints = function(knex, options) {

              var originalDefaultScope;
              if(target.prototype.scopes && target.prototype.scopes.default) {
                originalDefaultScope = target.prototype.scopes.default;
                delete target.prototype.scopes.default;
              }

              originalSelectConstraints.apply(this, arguments);

              if (originalDefaultScope) {
                target.prototype.scopes.default = originalDefaultScope;
              }
            };
            return relationship;
          };
        }

        return relationship;

      };
    });

    return model;
  };

  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) {
          if (!qb.appliedDefault) {
            qb.appliedDefault = true;
            self.scopes.default.call(self, qb);
          }
        });
      }
    },

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

  bookshelf.Model = Model;
};
<commit_msg>Add Travis CI<commit_after>'use strict';

var _ = require('lodash');

module.exports = function(bookshelf) {
  var base = bookshelf.Model;
  var baseExtend = bookshelf.Model.extend;
  // `bookshelf.knex()` was deprecated in knex v0.8.0, use `knex.queryBuilder()` instead if available
  var QueryBuilder = (bookshelf.knex.queryBuilder) ? bookshelf.knex.queryBuilder().constructor : bookshelf.knex().constructor;

  bookshelf.Model.extend = function(protoProps, constructorProps) {
    var model = baseExtend.apply(this, arguments);

    model.prototype.scopes = model.prototype.scopes || {};

    _.defaults(model.prototype.scopes, this.prototype.scopes || {});

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

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

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

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

    _.each(['hasMany', 'hasOne', 'belongsToMany', 'morphOne', 'morphMany',
      'belongsTo', 'through'], function(method) {
      var original = model.prototype[method];
      model.prototype[method] = function() {
        var relationship = original.apply(this, arguments);
        var target = relationship.model || relationship.relatedData.target;
        var originalSelectConstraints = relationship.relatedData.selectConstraints;

        if (target.prototype.scopes) {
          relationship.relatedData.selectConstraints = function(knex, options) {
            originalSelectConstraints.apply(this, arguments);
            if (target.prototype.scopes.default) {
              if (!knex.appliedDefault) {
                knex.appliedDefault = true;
                target.prototype.scopes.default.apply(this, [knex, options]);
              }
            }
          };

          Object.keys(target.prototype.scopes).forEach(function(key) {
            relationship[key] = function() {
              var passedInArguments = _.toArray(arguments);
              var currentSelectConstraints = relationship.relatedData.selectConstraints;
              relationship.relatedData.selectConstraints = function(knex, options) {
                currentSelectConstraints.apply(this, arguments);
                passedInArguments.unshift(knex);
                target.prototype.scopes[key].apply(target.prototype, passedInArguments);
              };
              return relationship;
            };
          });

          relationship.unscoped = function() {
            relationship.relatedData.selectConstraints = function(knex, options) {

              var originalDefaultScope;
              if(target.prototype.scopes && target.prototype.scopes.default) {
                originalDefaultScope = target.prototype.scopes.default;
                delete target.prototype.scopes.default;
              }

              originalSelectConstraints.apply(this, arguments);

              if (originalDefaultScope) {
                target.prototype.scopes.default = originalDefaultScope;
              }
            };
            return relationship;
          };
        }

        return relationship;

      };
    });

    return model;
  };

  var Model = bookshelf.Model.extend({

    scopes: null,

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

    addScope: function() {
      var self = this;
      if (self.scopes && self.scopes.default) {
        self.query(function(qb) {
          if (!qb.appliedDefault) {
            qb.appliedDefault = true;
            self.scopes.default.call(self, qb);
          }
        });
      }
    },

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

  bookshelf.Model = Model;
};

{'fraction_non_alphanumeric': 0.1170050463560615, 'fraction_numerical': 0.0011735711770918906, 'mean_word_length': 2.261385380788366, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1299743', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2198, 'n_words': 381}