<commit_before>import Ember from 'ember';
import ajax from 'ic-ajax';

export default Ember.Route.extend({

    model(params) {
        const requestedVersion = params.version_num;

        const crate = this.modelFor('crate');
        const controller = this.controllerFor(this.routeName);
        const maxVersion = crate.get('max_version');

        // Fall back to the crate's `max_version` property
        if (!requestedVersion) {
            params.version_num = maxVersion;
        }

        controller.set('crate', crate);
        controller.set('requestedVersion', requestedVersion);
        controller.set('fetchingFollowing', true);

        crate.get('keywords')
            .then((keywords) => controller.set('keywords', keywords));

        if (this.session.get('currentUser')) {
            ajax(`/api/v1/crates/${crate.get('name')}/following`)
                .then((d) => controller.set('following', d.following))
                .finally(() => controller.set('fetchingFollowing', false));
        }

        // Find version model
        return crate.get('versions')
            .then(versions => {
                const version = versions.find(version => version.get('num') === params.version_num);
                if (!version) {
                    this.controllerFor('application').set('nextFlashError',
                        `Version '${params.version_num}' of crate '${crate.get('name')}' does not exist`);
                }

                return version ||
                    versions.find(version => version.get('num') === maxVersion) ||
                    versions.objectAt(0);
            });
    },

    // can't do this in setupController because it won't be called
    // when going from "All Versions" to the current version
    afterModel(model) {
        this._super(...arguments);

        const controller = this.controllerFor(this.routeName);
        const context = controller.get('requestedVersion') ? model : this.modelFor('crate');

        context.get('version_downloads').then(downloads => {
            controller.set('downloads', downloads);
        });
    },

    serialize(model) {
        if (!model) {
            return { version_num: '' };
        } else {
            return { version_num: model.get('num') };
        }
    },
});
<commit_msg>routes/crate/version: Simplify serialize() method<commit_after>import Ember from 'ember';
import ajax from 'ic-ajax';

export default Ember.Route.extend({

    model(params) {
        const requestedVersion = params.version_num;

        const crate = this.modelFor('crate');
        const controller = this.controllerFor(this.routeName);
        const maxVersion = crate.get('max_version');

        // Fall back to the crate's `max_version` property
        if (!requestedVersion) {
            params.version_num = maxVersion;
        }

        controller.set('crate', crate);
        controller.set('requestedVersion', requestedVersion);
        controller.set('fetchingFollowing', true);

        crate.get('keywords')
            .then((keywords) => controller.set('keywords', keywords));

        if (this.session.get('currentUser')) {
            ajax(`/api/v1/crates/${crate.get('name')}/following`)
                .then((d) => controller.set('following', d.following))
                .finally(() => controller.set('fetchingFollowing', false));
        }

        // Find version model
        return crate.get('versions')
            .then(versions => {
                const version = versions.find(version => version.get('num') === params.version_num);
                if (!version) {
                    this.controllerFor('application').set('nextFlashError',
                        `Version '${params.version_num}' of crate '${crate.get('name')}' does not exist`);
                }

                return version ||
                    versions.find(version => version.get('num') === maxVersion) ||
                    versions.objectAt(0);
            });
    },

    // can't do this in setupController because it won't be called
    // when going from "All Versions" to the current version
    afterModel(model) {
        this._super(...arguments);

        const controller = this.controllerFor(this.routeName);
        const context = controller.get('requestedVersion') ? model : this.modelFor('crate');

        context.get('version_downloads').then(downloads => {
            controller.set('downloads', downloads);
        });
    },

    serialize(model) {
        let version_num = model ? model.get('num') : '';
        return { version_num };
    },
});

{'fraction_non_alphanumeric': 0.14183006535947712, 'fraction_numerical': 0.0008714596949891067, 'mean_word_length': 2.1727712508638564, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5908613', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1172, 'n_words': 247}