Skip to content

Commit 98c9abc

Browse files
committed
fix bug with dots in filename and add tests
1 parent d6082ca commit 98c9abc

6 files changed

Lines changed: 39 additions & 12 deletions

File tree

app/adapters/gist-file.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ApplicationAdapter from './application';
22

33
export default ApplicationAdapter.extend({
44
generateIdForRecord(store, type, inputProperties) {
5-
return inputProperties.filePath.replace(/\./gi, '\\.').replace(/\//gi, '.');
5+
return inputProperties.fileName ||
6+
inputProperties.filePath.replace(/\./gi, '\\.').replace(/\//gi, '.');
67
}
78
});

app/mixins/resize.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import $ from 'jquery';
55
export default Mixin.create({
66
didInsertElement() {
77
this._super();
8-
$(window).on('resize', this.resizeHandler);
8+
this.boundResizeHandler = this.resizeHandler.bind(this);
9+
$(window).on('resize', this.boundResizeHandler);
910
},
1011

1112
willDestroyElement() {
1213
this._super();
13-
$(window).off('resize', this.resizeHandler);
14+
$(window).off('resize', this.boundResizeHandler);
1415
},
1516

1617
resizeHandler() {
17-
return run.bind(this, 'didResize');
18+
return run.bind(() => this.didResize());
1819
}
1920
});

app/models/gist-file.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,28 @@ export default DS.Model.extend({
1515
*/
1616
filePath: computed('fileName', {
1717
get() {
18-
var fileName = this.fileName || '';
18+
let fileName = this.fileName || '';
19+
let filePath = '';
1920

2021
// If the file name has an escaped `.`, we're using the new version of path
2122
// encoding that supports multiple periods in a path.
2223
// If not, we assume the last period separates the file ending and the rest are `/`s.
2324
// Required for back compat.
2425
// Ref: https://github.com/ember-cli/ember-twiddle/issues/658
25-
var hasEscapedPeriod = /\\\./.test(fileName);
26+
let hasEscapedPeriod = /\\\./.test(fileName);
2627
if (hasEscapedPeriod) {
27-
return fileName.replace(/[^\\]\./gi, '/').replace(/\\\./gi, '.');
28+
filePath = fileName.replace(/([^\\])\./gi, "$1/").replace(/\\\./gi, '.');
2829
}
2930
else {
30-
var parts = fileName.split('.');
31-
return parts.slice(0, -1).join('/') + '.' + parts.slice(-1);
31+
let parts = fileName.split('.');
32+
filePath = parts.slice(0, -1).join('/') + '.' + parts.slice(-1);
3233
}
34+
return filePath;
3335
},
3436

3537
set(key, value) {
36-
this.set('fileName', value.replace(/\./gi, '\\.').replace(/\//gi, '.'));
38+
let fileName = value.replace(/\./gi, '\\.').replace(/\//gi, '.');
39+
this.set('fileName', fileName);
3740
return value;
3841
}
3942
}),

app/services/ember-cli.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,11 @@ export default Service.extend({
194194
// Remove app prefix if present
195195
let name = filePath.replace(/^app\//, '');
196196

197-
return Path.join(twiddleAppName,
197+
let moduleName = Path.join(twiddleAppName,
198198
Path.relative('.', Path.dirname(name)),
199199
Path.basename(name, Path.extname(name)));
200+
201+
return moduleName;
200202
},
201203

202204
/**

app/utils/push-deletion.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,9 @@ export function pushDeletion(store, type, id) {
4040

4141
export function pushDeleteAll(store, type) {
4242
let records = store.peekAll(type);
43-
records.forEach(record => pushDeletion(store, type, record.get('id')));
43+
records.forEach(record => {
44+
if (record.get('id')) {
45+
pushDeletion(store, type, record.get('id'));
46+
}
47+
});
4448
}

tests/unit/models/gist-file-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,20 @@ module('Unit | Model | gist file', function(hooks) {
3636
await run(() => model.deleteRecord());
3737
});
3838
});
39+
40+
test('correctly determines filePath from fileName', function(assert) {
41+
const store = this.owner.lookup('service:store');
42+
let file1 = store.createRecord('gist-file', {
43+
fileName: 'twiddle\\.json'
44+
});
45+
assert.equal(file1.filePath, 'twiddle.json');
46+
let file2 = store.createRecord('gist-file', {
47+
fileName: 'controllers.application\\.js'
48+
});
49+
assert.equal(file2.filePath, 'controllers/application.js');
50+
let file3 = store.createRecord('gist-file', {
51+
fileName: 'controllers.app\\.ication\\.js'
52+
});
53+
assert.equal(file3.filePath, 'controllers/app.ication.js');
54+
});
3955
});

0 commit comments

Comments
 (0)