<commit_before>/*
  Common properties and behaviors shared by all OSF APIv2 models
 */

import Ember from 'ember';
import DS from 'ember-data';

export default DS.Model.extend({
    links: DS.attr('links'),
    embeds: DS.attr('embed'),

    relationshipLinks: Ember.computed.alias('links.relationships'),
    _dirtyRelationships: null,
    dirtyRelationships: Ember.computed('_dirtyRelationships', function() {
        var dirtyRelationships = this.get('_dirtyRelationships');
        return Object.keys(dirtyRelationships).filter(k => dirtyRelationships[k]);
    }).volatile(),
    clearDirtyRelationship: function(relationship) {
        this.set(`_dirtyRelationships.${relationship}`, false);
        // Also clean the inverse relationship
        var relatedMeta = this[relationship].meta();
        // A slight hack to get the related record w/o an API call
        if (relatedMeta.kind === 'hasMany') {
            this._internalModel._relationships.initializedRelationships[relationship].canonicalState.forEach(internalModel => {
                internalModel.record.set(`_dirtyRelationships.${relatedMeta.options.inverse}`, false);
            });
        }
        // else {
        //     // TODO: belongsTo
        // }

    },
    ready() {
        this._super(...arguments);
        this.set('_dirtyRelationships', Ember.Object.create({}));
    },
    save() {
        this.eachRelationship((rel, meta) => {
            if (meta.kind === 'hasMany') {
                var relation = this.hasMany(rel).hasManyRelationship;
                if (relation.record.isLoaded()) {
                    if (
                        (relation.canonicalState.filter(record => record && (Object.keys(record.changedAttributes()).length > 0 || record.isNew())).length > 0) ||
                        relation.canonicalMembers.size !== relation.members.size
                    ){
                        var key = `_dirtyRelationships.${rel}`;
                        this.set(key, true);
                    }
                }
            } else if (meta.kind === 'belongsTo') {
                var relation = this.belongsTo(rel).belongsToRelationship;
                if (relation.record.isLoaded()) {
                    var record = relation.members.list[0]
                    if (
                        (record && (record.isNew() || record.changedAttributes().length > 0)) ||
                        relation.canonicalMembers.size !== relation.members.size
                    ){
                        var key = `_dirtyRelationships.${rel}`;
                        this.set(key, true);
                    }
                }

            }
        });
        this._super(...arguments);
    }
});<commit_msg>fix style<commit_after>/*
  Common properties and behaviors shared by all OSF APIv2 models
 */

import Ember from 'ember';
import DS from 'ember-data';

export default DS.Model.extend({
    links: DS.attr('links'),
    embeds: DS.attr('embed'),

    relationshipLinks: Ember.computed.alias('links.relationships'),
    _dirtyRelationships: null,
    dirtyRelationships: Ember.computed('_dirtyRelationships', function() {
        var dirtyRelationships = this.get('_dirtyRelationships');
        return Object.keys(dirtyRelationships).filter(k => dirtyRelationships[k]);
    }).volatile(),
    clearDirtyRelationship: function(relationship) {
        this.set(`_dirtyRelationships.${relationship}`, false);
        // Also clean the inverse relationship
        var relatedMeta = this[relationship].meta();
        // A slight hack to get the related record w/o an API call
        if (relatedMeta.kind === 'hasMany') {
            this._internalModel._relationships.initializedRelationships[relationship].canonicalState.forEach(internalModel => {
                internalModel.record.set(`_dirtyRelationships.${relatedMeta.options.inverse}`, false);
            });
        }
        // else {
        //     // TODO: belongsTo
        // }

    },
    ready() {
        this._super(...arguments);
        this.set('_dirtyRelationships', Ember.Object.create({}));
    },
    save() {
        this.eachRelationship((rel, meta) => {
            if (meta.kind === 'hasMany') {
                var relation = this.hasMany(rel).hasManyRelationship;
                if (relation.record.isLoaded()) {
                    if (
                        (relation.canonicalState.filter(record => record && (Object.keys(record.changedAttributes()).length > 0 || record.isNew())).length > 0) ||
                        relation.canonicalMembers.size !== relation.members.size
                    ) {
                        var key = `_dirtyRelationships.${rel}`;
                        this.set(key, true);
                    }
                }
            } else if (meta.kind === 'belongsTo') {
                var relation = this.belongsTo(rel).belongsToRelationship;
                if (relation.record.isLoaded()) {
                    var record = relation.members.list[0];
                    if (
                        (record && (record.isNew() || record.changedAttributes().length > 0)) ||
                        relation.canonicalMembers.size !== relation.members.size
                    ) {
                        var key = `_dirtyRelationships.${rel}`;
                        this.set(key, true);
                    }
                }

            }
        });
        this._super(...arguments);
    }
});
{'fraction_non_alphanumeric': 0.1297148114075437, 'fraction_numerical': 0.0018399264029438822, 'mean_word_length': 1.8020618556701031, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '7556023', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1339, 'n_words': 255}