all files / ember-custom-actions/utils/ url-builder.js

91.67% Statements 11/12
75% Branches 6/8
100% Functions 5/5
91.67% Lines 11/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36          12× 12×       12×       12× 12×   12× 12×       12×       12×     12×          
import Ember from 'ember';
 
const { assert, computed, Object } = Ember;
 
export default Object.extend({
  modelName: computed('model', function() {
    let { constructor } = this.get('model');
    return constructor.modelName || constructor.typeKey;
  }),
 
  snapshot: computed('model', function() {
    return this.get('model')._createSnapshot();
  }),
 
  build() {
    assert('You must provide a path for model action!', this.get('path'));
    assert('Model has to be persisted!', !(this.get('instance') && !this.get('model.id')));
 
    let id = this.get('instance') ? this.get('model.id') : null;
    return this._makeUrl(this._buildUrl(id));
  },
 
  _buildUrl(id) {
    return this.get('adapter').buildURL(this.get('modelName'), id, this.get('snapshot'), this.get('urlType'));
  },
 
  _makeUrl(url) {
    Iif (url.charAt(url.length - 1) === '/') {
      return `${url}${this.get('path')}`;
    } else {
      return `${url}/${this.get('path')}`;
    }
  }
});