const _ = require('underscore');
const Backbone = require('backbone');
const UserModel = require('dashboard/data/user-model');
const checkAndBuildOpts = require('builder/helpers/required-opts');

const REQUIRED_OPTS = [
  'organization',
  'userModel'
];

// helper to manage organization users
module.exports = Backbone.Collection.extend({
  sync: require('dashboard/data/backbone/sync-abort'),

  model: UserModel,

  _DEFAULT_EXCLUDE_CURRENT_USER: true,

  url: function () {
    return `${this._userModel.get('base_url')}/api/v1/organization/${this._organization.id}/users`;
  },

  initialize: function (models, options) {
    checkAndBuildOpts(options, REQUIRED_OPTS, this);

    this.currentUserId = options.currentUserId;
    this._excludeCurrentUser = this._DEFAULT_EXCLUDE_CURRENT_USER;
  },

  comparator: function (mdl) {
    return mdl.get('username');
  },

  excludeCurrentUser: function (exclude) {
    exclude = !!exclude;
    this._excludeCurrentUser = exclude;
    if (exclude && !this.currentUserId) {
      console.error('set excludeCurrentUser to true, but there is no current user id set to exclude!');
    }
  },

  restoreExcludeCurrentUser: function () {
    this.excludeCurrentUser(this._DEFAULT_EXCLUDE_CURRENT_USER);
  },

  parse: function (r) {
    this.total_entries = r.total_entries;
    this.total_user_entries = r.total_user_entries;

    return _.reduce(r.users, function (memo, user) {
      if (this._excludeCurrentUser && user.id === this.currentUserId) {
        this.total_user_entries--;
        this.total_entries--;
      } else {
        memo.push(user);
      }
      return memo;
    }, [], this);
  },

  // @return {Number, undefined} may be undefined until a first fetch is done
  totalCount: function () {
    return this.total_user_entries;
  }
});

@@ -233,24 +233,9 @@
 ion'
-,%0A  'userModel'
 %0A
+
 %5D;%0A%0A
@@ -323,64 +323,8 @@
 d(%7B%0A
-  sync: require('dashboard/data/backbone/sync-abort'),%0A%0A
   mo
@@ -416,43 +416,9 @@
 urn 
-%60$%7Bthis._userModel.get('base_url')%7D
+'
 /api
@@ -438,16 +438,17 @@
 ion/
-$%7B
+' + 
 this.
-_
 orga
@@ -462,16 +462,19 @@
 n.id
-%7D
+ + '
 /users
-%60
+'
 ;%0A  
@@ -574,16 +574,60 @@
 this);%0A%0A
+    this.organization = this._organization;%0A
     this
@@ -764,17 +764,19 @@
 ction (m
-d
+ode
 l) %7B%0A   
@@ -784,17 +784,19 @@
 return m
-d
+ode
 l.get('u

Fix url in organization-users-collection