<commit_before>angular.module("admin.indexUtils").factory "pendingChanges", ($q, resources, StatusMessage) ->
  new class pendingChanges
    pendingChanges: {}
    errors: []

    add: (id, attr, change) =>
      @pendingChanges["#{id}"] = {} unless @pendingChanges.hasOwnProperty("#{id}")
      @pendingChanges["#{id}"]["#{attr}"] = change
      StatusMessage.display('notice', t('admin.unsaved_changes'))

    removeAll: =>
      @pendingChanges = {}
      StatusMessage.clear()


    remove: (id, attr) =>
      if @pendingChanges.hasOwnProperty("#{id}")
        delete @pendingChanges["#{id}"]["#{attr}"]
        delete @pendingChanges["#{id}"] if @changeCount( @pendingChanges["#{id}"] ) < 1

    submitAll: (form=null) =>
      all = []
      @errors = []
      StatusMessage.display('progress', t('js.saving'))
      for id, objectChanges of @pendingChanges
        for attrName, change of objectChanges
          all.push @submit(change)
      $q.all(all).then =>
        if @errors.length == 0
          StatusMessage.display('success', t('js.all_changes_saved_successfully'))
          form.$setPristine() if form?
        else
          StatusMessage.display('failure', t('js.oh_no'))
      all

    submit: (change) ->
      resources.update(change).$promise.then (data) =>
        @remove change.object.id, change.attr
        change.scope.reset( data["#{change.attr}"] )
        change.scope.success()
      , (error) =>
        @errors.push error
        change.scope.error()

    unsavedCount: ->
      Object.keys(@pendingChanges).length

    changeCount: (objectChanges) ->
      Object.keys(objectChanges).length<commit_msg>Avoid console error by catching potential fails<commit_after>angular.module("admin.indexUtils").factory "pendingChanges", ($q, resources, StatusMessage) ->
  new class pendingChanges
    pendingChanges: {}
    errors: []

    add: (id, attr, change) =>
      @pendingChanges["#{id}"] = {} unless @pendingChanges.hasOwnProperty("#{id}")
      @pendingChanges["#{id}"]["#{attr}"] = change
      StatusMessage.display('notice', t('admin.unsaved_changes'))

    removeAll: =>
      @pendingChanges = {}
      StatusMessage.clear()


    remove: (id, attr) =>
      if @pendingChanges.hasOwnProperty("#{id}")
        delete @pendingChanges["#{id}"]["#{attr}"]
        delete @pendingChanges["#{id}"] if @changeCount( @pendingChanges["#{id}"] ) < 1

    submitAll: (form=null) =>
      all = []
      @errors = []
      StatusMessage.display('progress', t('js.saving'))
      for id, objectChanges of @pendingChanges
        for attrName, change of objectChanges
          all.push @submit(change)
      $q.all(all).then =>
        if @errors.length == 0
          StatusMessage.display('success', t('js.all_changes_saved_successfully'))
          form.$setPristine() if form?
        else
          StatusMessage.display('failure', t('js.oh_no'))
      .catch ->
        StatusMessage.display('failure', t('js.oh_no'))
      all

    submit: (change) ->
      resources.update(change).$promise.then (data) =>
        @remove change.object.id, change.attr
        change.scope.reset( data["#{change.attr}"] )
        change.scope.success()
      , (error) =>
        @errors.push error
        change.scope.error()

    unsavedCount: ->
      Object.keys(@pendingChanges).length

    changeCount: (objectChanges) ->
      Object.keys(objectChanges).length
{'fraction_non_alphanumeric': 0.1650943396226415, 'fraction_numerical': 0.0011792452830188679, 'mean_word_length': 2.940766550522648, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, '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': '7061747', 'n_tokens_mistral': 990, 'n_tokens_neox': 993, 'n_words': 182}