Skip to content

Commit c72a282

Browse files
committed
Add ability to sort models by record count
1 parent c8d5714 commit c72a282

5 files changed

Lines changed: 74 additions & 12 deletions

File tree

app/controllers/model-types.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,36 @@ import { get, computed } from '@ember/object';
33
import LocalStorageService from 'ember-inspector/services/storage/local';
44
import { sort } from '@ember/object/computed';
55
import { inject as service } from '@ember/service';
6+
import { HIDE_EMPTY_MODELS_KEY, ORDER_MODELS_BY_COUNT_KEY } from 'ember-inspector/utils/local-storage-keys';
67

78
export default Controller.extend({
89
application: controller(),
910
navWidth: 180,
10-
sortProperties: ['name'],
11+
sortByNameProp: Object.freeze(['name']),
12+
sortByDescCountProp: Object.freeze(['count:desc']),
1113
storage: service(`storage/${LocalStorageService.SUPPORTED ? 'local' : 'memory'}`),
14+
1215
hideEmptyModelTypes: computed({
1316
get() {
14-
return !!this.get('storage').getItem('are-model-types-hidden');
17+
return getStoredPropertyValue(this.get('storage'), HIDE_EMPTY_MODELS_KEY);
1518
},
1619
set(key, value) {
17-
if (!value) {
18-
this.get('storage').removeItem('are-model-types-hidden');
19-
} else {
20-
this.get('storage').setItem('are-model-types-hidden', value);
21-
}
22-
return value;
20+
return handleSettingProperty(this.get('storage'), HIDE_EMPTY_MODELS_KEY, value);
21+
}
22+
}),
23+
24+
orderByRecordCount: computed({
25+
get() {
26+
return getStoredPropertyValue(this.get('storage'), ORDER_MODELS_BY_COUNT_KEY);
27+
},
28+
set(key, value) {
29+
return handleSettingProperty(this.get('storage'), ORDER_MODELS_BY_COUNT_KEY, value);
2330
}
2431
}),
2532

26-
sorted: sort('filtered', 'sortProperties'),
33+
sortByName: sort('filtered', 'sortByNameProp'),
34+
35+
sortByDescCount: sort('filtered', 'sortByDescCountProp'),
2736

2837
filtered: computed('model.@each.count', 'hideEmptyModelTypes', function() {
2938
return this.get('model').filter(item => {
@@ -37,3 +46,22 @@ export default Controller.extend({
3746
});
3847
})
3948
});
49+
50+
/**
51+
* Returns whether or not a given key has been set in storage.
52+
* @param {*} storage
53+
* @param {string} key
54+
* @returns {boolean}
55+
*/
56+
function getStoredPropertyValue(storage, key) {
57+
return !!storage.getItem(key);
58+
}
59+
60+
function handleSettingProperty(storage, key, value) {
61+
if (!value) {
62+
storage.removeItem(key);
63+
} else {
64+
storage.setItem(key, value);
65+
}
66+
return value;
67+
}

app/templates/model-types-toolbar.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@
55
Hide Empty Model Types
66
</label>
77
</div>
8+
<div class="toolbar__checkbox js-filter-order-records-by-count">
9+
<label for="options-orderByRecordCount">
10+
{{input type="checkbox" checked=orderByRecordCount id="options-orderByRecordCount"}}
11+
Order Models By Record Count
12+
</label>
13+
</div>
814
</div>

app/templates/model-types.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{item-types
22
header='Model Types'
33
setIsDragging=(route-action 'setIsDragging')
4-
sorted=(readonly sorted)
4+
sorted=(readonly (if orderByRecordCount sortByDescCount sortByName))
55
type='model'
66
width=navWidth
77
}}

app/utils/local-storage-keys.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Names of keys set in the storage service.
2+
3+
export const HIDE_EMPTY_MODELS_KEY = 'are-model-types-hidden';
4+
5+
export const ORDER_MODELS_BY_COUNT_KEY = 'are-models-ordered-by-record-count';

tests/acceptance/data-test.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { setupApplicationTest } from 'ember-qunit';
66
import { triggerPort } from '../helpers/trigger-port';
77
import wait from 'ember-test-helpers/wait';
88
import LocalStorageService from 'ember-inspector/services/storage/local';
9+
import { HIDE_EMPTY_MODELS_KEY, ORDER_MODELS_BY_COUNT_KEY } from 'ember-inspector/utils/local-storage-keys';
910

1011
let port, name;
1112

@@ -33,10 +34,12 @@ module('Data Tab', function(hooks) {
3334

3435
hooks.afterEach(function() {
3536
name = null;
36-
// This is to ensure the hiding empty models setting does not persist across multiple test runs.
37+
// This is to ensure settings in Storage do not persist across multiple test runs.
3738
let storageServiceToUse = LocalStorageService.SUPPORTED ? 'local' : 'memory';
3839
let storageService = this.owner.lookup(`service:storage/${storageServiceToUse}`);
39-
storageService.removeItem('are-model-types-hidden');
40+
41+
storageService.removeItem(HIDE_EMPTY_MODELS_KEY);
42+
storageService.removeItem(ORDER_MODELS_BY_COUNT_KEY);
4043
});
4144

4245
function modelTypeFactory(options) {
@@ -196,6 +199,26 @@ module('Data Tab', function(hooks) {
196199
assert.dom('.js-model-type').exists({ count: 2 }, 'All models are present again');
197200
});
198201

202+
test('Order by record count', async function(assert) {
203+
assert.expect(6);
204+
205+
await visit('/data/model-types');
206+
207+
assert.dom(findAll('.js-model-type-name')[0]).hasText('App.Comment');
208+
assert.dom(findAll('.js-model-type-name')[1]).hasText('App.Post');
209+
210+
// Order models by record count.
211+
await click('#options-orderByRecordCount');
212+
213+
assert.dom(findAll('.js-model-type-name')[0]).hasText('App.Post');
214+
assert.dom(findAll('.js-model-type-name')[1]).hasText('App.Comment');
215+
216+
// Don't order models by record count.
217+
await click('#options-orderByRecordCount');
218+
assert.dom(findAll('.js-model-type-name')[0]).hasText('App.Comment');
219+
assert.dom(findAll('.js-model-type-name')[1]).hasText('App.Post');
220+
});
221+
199222
test('Filtering records', async function t(assert) {
200223
await visit('/data/model-types');
201224

0 commit comments

Comments
 (0)