<commit_before>import DS from 'ember-data';
import RelationshipTracker from "ember-data-relationship-tracker";
import { inject as service } from '@ember/service';
import { computed, defineProperty, get } from '@ember/object';
import { readOnly, or } from '@ember/object/computed';
import { capitalize } from '@ember/string';

export default DS.Model.extend(RelationshipTracker, {
  resourceMetadata: service(),

  relationshipTrackerVersionKey: 'version',

  init() {
    this._super();
    let dirtyTrackingProperties = [];
    this._relationshipsByName.forEach((relation) => {
      let { kind, meta } = relation;
      let { owned } = meta.options;
      if (owned) {
        let propertyName = createHasDirtyForRelationship(this, meta.name, kind);
        dirtyTrackingProperties.push(propertyName);
      }
    });
    if (dirtyTrackingProperties.length > 0) {
      createHasDirtyOwned(this, dirtyTrackingProperties);
    }
  },

  version: computed(function() {
    let meta = this.get('resourceMetadata').read(this);
    return meta && meta.version;
  }),

  async save() {
    // this._super is not safe to use asynchronously
    // see https://github.com/ember-cli/ember-cli/issues/6282
    let modelSave = this._super.bind(this);
    await this.saveRelated();
    await modelSave();
  },

  async saveRelated() {
    //TODO: Go through owned relationships and save the ones which `hasDirtyFields`
    // and then recurse down to save their `hasDirtyFields` owned relations
    // Save children first.
    let relatedSaves = this.dirtyRelationships.map(field => {
      let { kind } = this._relationshipsByName.get(field);
      let relatedRecords = kind === 'hasMany' ? this[field] : [ this.field ];
      let dirtyRecords = relatedRecords.filter(record => record.hasDirtyFields);
      return dirtyRecords.invoke('save');
    });
    return Promise.all(flatten(relatedSaves));
  },

  _relationshipsByName() {
    return get(this.constructor, 'relationshipsByName');
  }
});

function createHasDirtyForRelationship(model, name, kind) {
  let propertyName = `hasDirty${capitalize(name)}`;
  if (kind === 'hasMany') {
    defineProperty(model, propertyName, computed(`${name}.@each.hasDirtyAttributes`, function() {
      return model.get(name).toArray().some((related) => related.hasDirtyAttributes);<commit_msg>Fix function call<commit_after>import DS from 'ember-data';
import RelationshipTracker from "ember-data-relationship-tracker";
import { inject as service } from '@ember/service';
import { computed, defineProperty, get } from '@ember/object';
import { readOnly, or } from '@ember/object/computed';
import { capitalize } from '@ember/string';

export default DS.Model.extend(RelationshipTracker, {
  resourceMetadata: service(),

  relationshipTrackerVersionKey: 'relationshipTrackerVersion',

  init() {
    this._super();
    let dirtyTrackingProperties = [];
    this._relationshipsByName().forEach((relation) => {
      let { kind, meta } = relation;
      let { owned } = meta.options;
      if (owned) {
        let propertyName = createHasDirtyForRelationship(this, meta.name, kind);
        dirtyTrackingProperties.push(propertyName);
      }
    });
    if (dirtyTrackingProperties.length > 0) {
      createHasDirtyOwned(this, dirtyTrackingProperties);
    }
  },

  relationshipTrackerVersion: computed(function() {
    let meta = this.get('resourceMetadata').read(this);
    return meta && meta.version;
  }),

  async save() {
    // this._super is not safe to use asynchronously
    // see https://github.com/ember-cli/ember-cli/issues/6282
    let modelSave = this._super.bind(this);
    await this.saveRelated();
    await modelSave();
  },

  async saveRelated() {
    //TODO: Go through owned relationships and save the ones which `hasDirtyFields`
    // and then recurse down to save their `hasDirtyFields` owned relations
    // Save children first.
    let relatedSaves = this.dirtyRelationships.map(field => {
      let { kind } = this._relationshipsByName().get(field);
      let relatedRecords = kind === 'hasMany' ? this[field] : [ this.field ];
      let dirtyRecords = relatedRecords.filter(record => record.hasDirtyFields);
      return dirtyRecords.invoke('save');
    });
    return Promise.all(flatten(relatedSaves));
  },

  _relationshipsByName() {
    return get(this.constructor, 'relationshipsByName');
  }
});

function createHasDirtyForRelationship(model, name, kind) {
  let propertyName = `hasDirty${capitalize(name)}`;
  if (kind === 'hasMany') {
    defineProperty(model, propertyName, computed(`${name}.@each.hasDirtyAttributes`, function() {
      return model.get(name).toArray().some((related) => related.hasDirtyAttributes);
{'fraction_non_alphanumeric': 0.13213981244671782, 'fraction_numerical': 0.002131287297527707, 'mean_word_length': 3.9244491080797483, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '100197', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1328, 'n_words': 309}