diff --git a/addon/helpers/storage.js b/addon/helpers/storage.js index 2ddd9b0f..26d13f2b 100644 --- a/addon/helpers/storage.js +++ b/addon/helpers/storage.js @@ -109,6 +109,8 @@ function createStorage(context, key, modelKey, options) { storageKey = modelKey ? `${storageFactory}:${modelKey}` : storageFactory; } + storageKey = _buildKey(context, storageKey); + const initialState = {}, defaultState = { _storageKey: storageKey @@ -145,6 +147,29 @@ function _modelKey(model) { return `${modelName}:${id}`; } +// TODO: v2.0 - Make modulePrefix the default +function _getNamespace(appConfig, addonConfig) { + // For backward compatibility this is a opt-in feature + let namespace = addonConfig.namespace; + + // Shortcut for modulePrefix + if (namespace === true) { + namespace = appConfig.modulePrefix + } + + return namespace; +} + +// TODO: Add migration helper +function _buildKey(context, key) { + let appConfig = getOwner(context).resolveRegistration('config:environment'); + let addonConfig = appConfig && appConfig['ember-local-storage'] || {}; + let namespace = _getNamespace(appConfig, addonConfig); + let delimiter = addonConfig.keyDelimiter || ':'; + + return namespace ? `${namespace}${delimiter}${key}` : key; +} + // Testing helper function _resetStorages() { storages = {}; diff --git a/tests/helpers/storage.js b/tests/helpers/storage.js index 2c51b091..ac23b974 100644 --- a/tests/helpers/storage.js +++ b/tests/helpers/storage.js @@ -8,7 +8,24 @@ function storageDeepEqual(assert, actual, expected, message) { assert.deepEqual(actual, expected, message); } +function registerConfigEnvironment(context) { + let environment = { + modulePrefix: 'my-app', + 'ember-local-storage': {} + }; + + context.register('config:environment', environment, { instantiate: false }); +} + +function setConfigEnvironment(context, key, value) { + let appConfig = context.container.lookup('config:environment'); + let addonConfig = appConfig['ember-local-storage'] || {}; + addonConfig[key] = value; +} + export { storageEqual, - storageDeepEqual + storageDeepEqual, + registerConfigEnvironment, + setConfigEnvironment }; diff --git a/tests/unit/storage-for-test.js b/tests/unit/storage-for-test.js index d756c84a..ecdc2fd0 100644 --- a/tests/unit/storage-for-test.js +++ b/tests/unit/storage-for-test.js @@ -1,7 +1,9 @@ import Ember from 'ember'; import { moduleFor, test } from 'ember-qunit'; import { - storageDeepEqual + storageDeepEqual, + registerConfigEnvironment, + setConfigEnvironment } from '../helpers/storage'; import StorageObject from 'ember-local-storage/local/object'; @@ -10,8 +12,12 @@ import { _resetStorages } from 'ember-local-storage/helpers/storage'; -moduleFor('router:main', 'legacy - config', { +let subject; + +moduleFor('router:main', 'storageFor', { beforeEach() { + registerConfigEnvironment(this); + let mockStorage = StorageObject.extend(); mockStorage.reopenClass({ @@ -24,6 +30,18 @@ moduleFor('router:main', 'legacy - config', { this.register('storage:settings', mockStorage); this.register('storage:options', mockStorage); + + let post = Ember.Object.extend({ + modelName: 'post', + id: '123' + }).create(); + + this.register('object:test', Ember.Object.extend({ + post: post, + settings: storageFor('settings', 'post'), + options: storageFor('options', 'post') + })); + subject = this.container.lookup('object:test'); }, afterEach() { window.localStorage.clear(); @@ -31,21 +49,9 @@ moduleFor('router:main', 'legacy - config', { } }); -test('it has the correct key', function(assert) { +test('it has the correct key (namespace not set)', function(assert) { assert.expect(4); - let post = Ember.Object.extend({ - modelName: 'post', - id: '123' - }).create(); - - this.register('object:test', Ember.Object.extend({ - post: post, - settings: storageFor('settings', 'post'), - options: storageFor('options', 'post') - })); - let subject = this.container.lookup('object:test'); - assert.equal( subject.get('settings._storageKey'), 'storage:settings:post:123' @@ -64,3 +70,95 @@ test('it has the correct key', function(assert) { perPage: 10 }); }); + +test('it has the correct key (namespace: true)', function(assert) { + assert.expect(4); + + setConfigEnvironment(this, 'namespace', true); + + assert.equal( + subject.get('settings._storageKey'), + 'my-app:storage:settings:post:123' + ); + + assert.equal( + subject.get('options._storageKey'), + 'my-app:storage:options:post:123' + ); + + storageDeepEqual(assert, + window.localStorage['my-app:storage:settings:post:123'], + { + perPage: 10 + } + ); + + storageDeepEqual(assert, + window.localStorage['my-app:storage:options:post:123'], + { + perPage: 10 + } + ); +}); + +test('it has the correct key (namespace: "custom")', function(assert) { + assert.expect(4); + + setConfigEnvironment(this, 'namespace', 'custom'); + + assert.equal( + subject.get('settings._storageKey'), + 'custom:storage:settings:post:123' + ); + + assert.equal( + subject.get('options._storageKey'), + 'custom:storage:options:post:123' + ); + + storageDeepEqual(assert, + window.localStorage['custom:storage:settings:post:123'], + { + perPage: 10 + } + ); + + storageDeepEqual(assert, + window.localStorage['custom:storage:options:post:123'], + { + perPage: 10 + } + ); +}); + +test('it has the correct key (keyDelimiter: "/")', function(assert) { + assert.expect(4); + + setConfigEnvironment(this, 'namespace', true); + setConfigEnvironment(this, 'keyDelimiter', '/'); + + assert.equal( + subject.get('settings._storageKey'), + 'my-app/storage:settings:post:123' + ); + + assert.equal( + subject.get('options._storageKey'), + 'my-app/storage:options:post:123' + ); + + storageDeepEqual(assert, + window.localStorage['my-app/storage:settings:post:123'], + { + perPage: 10 + } + ); + + storageDeepEqual(assert, + window.localStorage['my-app/storage:options:post:123'], + { + perPage: 10 + } + ); +}); +