<commit_before>import Ember from 'ember';
import DS from 'ember-data';

const { computed, inject, Mixin, ObjectProxy, RSVP } = Ember;
const { all, Promise } = RSVP;
const { service } = inject;
const { PromiseArray, PromiseObject } = DS;

export default Mixin.create({
  store: service(),

  i18n: service(),

  cohort: null,
  topLevelGroup: null,
  members: [],
  showMoveToCohortOption: false,
  showMoveToTopLevelGroupOption: false,
  overrideCurrentGroupDisplay: false,
  saving: false,

  learnerGroupOptions: computed('cohort', 'topLevelGroup', 'topLevelGroup.allTreeGroups.[]', 'showMoveToCohortOption', 'showMoveToTopLevelGroupOption', function() {
    let defer = RSVP.defer();

    this.get('cohort').then((cohort) => {
      let options = [];

      if (this.get('showMoveToCohortOption')) {
        options.pushObject(Ember.Object.create({
          id: -1,
          title: this.get('i18n').t('learnerGroups.removeLearnerToCohort', {cohort: cohort.get('displayTitle')})
        }));
      }

      this.get('topLevelGroup').then((topLevelGroup) => {
        if (this.get('showMoveToTopLevelGroupOption')) {
          options.pushObject(Ember.Object.create({
            id: topLevelGroup.get('id'),
            title: this.get('i18n').t('learnerGroups.switchLearnerToGroup', {group: topLevelGroup.get('allParentsTitle') + ' ' + topLevelGroup.get('title')})
          }));
        }

        topLevelGroup.get('allDescendants').then((groups) => {
          let objs = groups.map((group) => {
            return Ember.Object.create({
              id: group.get('id'),
              title: this.get('i18n').t('learnerGroups.switchLearnerToGroup', {group: group.get('allParentsTitle') + ' ' + group.get('title')}),
              sortTitle: group.get('sortTitle')
            });
          }).sortBy('sortTitle');

          options.pushObjects(objs);
          defer.resolve(options);
        });
      });
    });

    return PromiseArray.create({
      promise: defer.promise
    });
  }),

  proxiedMembers: computed('members.[]', 'topLevelGroup', 'topLevelGroup.allDescendants.[]', function() {
    let defer = RSVP.defer();

    let userProxy = ObjectProxy.extend({
      treeGroups: [],

      overrideCurrentGroupDisplay: false,

      lowestGroupInTree: computed('content.learnerGroups.[]', 'treeGroups.[]', function(){
        let promise = new Promise((resolve) => {
          this.get('content.learnerGroups').then((userGroups) => {
            let treeGroups = userGroups.filter(group => this.get('treeGroups').contains(group));
            let deepestGroup, promises = [];

            treeGroups.forEach((group) => {
              let promise = group.get('children').then((children) => {
                let matchingChildren = children.filter(childGroup => treeGroups.contains(childGroup));

                if (matchingChildren.length === 0) {
                  deepestGroup = group;
                }
              });

              promises.pushObject(promise);
            });

            all(promises).then(()=>{
              resolve(deepestGroup);
            });
          });
        });

        return PromiseObject.create({ promise });
      }),

      groupDisplayValueString: computed('lowestGroupInTree.title', 'lowestGroupInTree.allParentsTitle', 'overrideCurrentGroupDisplay', function(){
        if (this.get('overrideCurrentGroupDisplay')) {
          return this.get('overrideCurrentGroupDisplay');
        }

        let group = this.get('lowestGroupInTree');

        return `${group.get('allParentsTitle')}${group.get('title')}`;
      })
    });

    let topLevelGroup = this.get('topLevelGroup');

    if (topLevelGroup) {
      topLevelGroup.then((topLevelGroup) => {
        let treeGroups = [];

        treeGroups.pushObject(topLevelGroup);

        topLevelGroup.get('allDescendants').then((all) => {
          treeGroups.pushObjects(all);

          let proxiedUsers = this.get('members').map((user) => {
            return userProxy.create({
              content: user,
              treeGroups: treeGroups,
              overrideCurrentGroupDisplay: this.get('overrideCurrentGroupDisplay')
            });
          });

          defer.resolve(proxiedUsers);
        });
      });
    } else {
      defer.resolve([]);
    }

    return PromiseArray.create({
      promise: defer.promise
    });
  }),

  sendPutRequests(groupIdString, userId) {
    this.set('saving', true);
    let groupId = parseInt(groupIdString);
    let toSave = [];
    let component = this;

    this.get('store').find('user', userId).then((user) => {
      toSave.pushObject(user);
      component.get('topLevelGroup').then((topLevelGroup) => {
        topLevelGroup.removeUserFromGroupAndAllDescendants(user).then((groups) => {
          toSave.pushObjects(groups);
          if (groupId === -1) {
            all(toSave.uniq().invoke('save')).finally(() => {
              component.set('saving', false);
            });
          } else {
            component.get('store').find('learnerGroup', groupId).then(learnerGroup => {
              learnerGroup.addUserToGroupAndAllParents(user).then(groups =>{
                toSave.pushObjects(groups);
                all(toSave.uniq().invoke('save')).finally(() => {
                  component.set('saving', false);
                });
              });
            });
          }
        });
      });
    });
  }
});<commit_msg>don't save user/group association on user.<commit_after>import Ember from 'ember';
import DS from 'ember-data';

const { computed, inject, Mixin, ObjectProxy, RSVP } = Ember;
const { all, Promise } = RSVP;
const { service } = inject;
const { PromiseArray, PromiseObject } = DS;

export default Mixin.create({
  store: service(),

  i18n: service(),

  cohort: null,
  topLevelGroup: null,
  members: [],
  showMoveToCohortOption: false,
  showMoveToTopLevelGroupOption: false,
  overrideCurrentGroupDisplay: false,
  saving: false,

  learnerGroupOptions: computed('cohort', 'topLevelGroup', 'topLevelGroup.allTreeGroups.[]', 'showMoveToCohortOption', 'showMoveToTopLevelGroupOption', function() {
    let defer = RSVP.defer();

    this.get('cohort').then((cohort) => {
      let options = [];

      if (this.get('showMoveToCohortOption')) {
        options.pushObject(Ember.Object.create({
          id: -1,
          title: this.get('i18n').t('learnerGroups.removeLearnerToCohort', {cohort: cohort.get('displayTitle')})
        }));
      }

      this.get('topLevelGroup').then((topLevelGroup) => {
        if (this.get('showMoveToTopLevelGroupOption')) {
          options.pushObject(Ember.Object.create({
            id: topLevelGroup.get('id'),
            title: this.get('i18n').t('learnerGroups.switchLearnerToGroup', {group: topLevelGroup.get('allParentsTitle') + ' ' + topLevelGroup.get('title')})
          }));
        }

        topLevelGroup.get('allDescendants').then((groups) => {
          let objs = groups.map((group) => {
            return Ember.Object.create({
              id: group.get('id'),
              title: this.get('i18n').t('learnerGroups.switchLearnerToGroup', {group: group.get('allParentsTitle') + ' ' + group.get('title')}),
              sortTitle: group.get('sortTitle')
            });
          }).sortBy('sortTitle');

          options.pushObjects(objs);
          defer.resolve(options);
        });
      });
    });

    return PromiseArray.create({
      promise: defer.promise
    });
  }),

  proxiedMembers: computed('members.[]', 'topLevelGroup', 'topLevelGroup.allDescendants.[]', function() {
    let defer = RSVP.defer();

    let userProxy = ObjectProxy.extend({
      treeGroups: [],

      overrideCurrentGroupDisplay: false,

      lowestGroupInTree: computed('content.learnerGroups.[]', 'treeGroups.[]', function(){
        let promise = new Promise((resolve) => {
          this.get('content.learnerGroups').then((userGroups) => {
            let treeGroups = userGroups.filter(group => this.get('treeGroups').contains(group));
            let deepestGroup, promises = [];

            treeGroups.forEach((group) => {
              let promise = group.get('children').then((children) => {
                let matchingChildren = children.filter(childGroup => treeGroups.contains(childGroup));

                if (matchingChildren.length === 0) {
                  deepestGroup = group;
                }
              });

              promises.pushObject(promise);
            });

            all(promises).then(()=>{
              resolve(deepestGroup);
            });
          });
        });

        return PromiseObject.create({ promise });
      }),

      groupDisplayValueString: computed('lowestGroupInTree.title', 'lowestGroupInTree.allParentsTitle', 'overrideCurrentGroupDisplay', function(){
        if (this.get('overrideCurrentGroupDisplay')) {
          return this.get('overrideCurrentGroupDisplay');
        }

        let group = this.get('lowestGroupInTree');

        return `${group.get('allParentsTitle')}${group.get('title')}`;
      })
    });

    let topLevelGroup = this.get('topLevelGroup');

    if (topLevelGroup) {
      topLevelGroup.then((topLevelGroup) => {
        let treeGroups = [];

        treeGroups.pushObject(topLevelGroup);

        topLevelGroup.get('allDescendants').then((all) => {
          treeGroups.pushObjects(all);

          let proxiedUsers = this.get('members').map((user) => {
            return userProxy.create({
              content: user,
              treeGroups: treeGroups,
              overrideCurrentGroupDisplay: this.get('overrideCurrentGroupDisplay')
            });
          });

          defer.resolve(proxiedUsers);
        });
      });
    } else {
      defer.resolve([]);
    }

    return PromiseArray.create({
      promise: defer.promise
    });
  }),

  sendPutRequests(groupIdString, userId) {
    this.set('saving', true);
    let groupId = parseInt(groupIdString);
    let toSave = [];
    let component = this;

    this.get('store').find('user', userId).then((user) => {
      component.get('topLevelGroup').then((topLevelGroup) => {
        topLevelGroup.removeUserFromGroupAndAllDescendants(user).then((groups) => {
          toSave.pushObjects(groups);
          if (groupId === -1) {
            all(toSave.uniq().invoke('save')).finally(() => {
              component.set('saving', false);
            });
          } else {
            component.get('store').find('learnerGroup', groupId).then(learnerGroup => {
              learnerGroup.addUserToGroupAndAllParents(user).then(groups =>{
                toSave.pushObjects(groups);
                all(toSave.uniq().invoke('save')).finally(() => {
                  component.set('saving', false);
                });
              });
            });
          }
        });
      });
    });
  }
});
{'fraction_non_alphanumeric': 0.1551310446659284, 'fraction_numerical': 0.002030269472129937, 'mean_word_length': 2.4381345177664975, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, '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': '4783986', 'n_tokens_mistral': 3144, 'n_tokens_neox': 3018, 'n_words': 454}