diff --git a/src/internal-packages/app/lib/stores/collection-store.js b/src/internal-packages/app/lib/stores/collection-store.js index 8cea113768a..9b1e6c0cd7f 100644 --- a/src/internal-packages/app/lib/stores/collection-store.js +++ b/src/internal-packages/app/lib/stores/collection-store.js @@ -1,3 +1,4 @@ +const app = require('ampersand-app'); const Reflux = require('reflux'); const { NamespaceStore } = require('hadron-reflux-store'); @@ -10,8 +11,7 @@ const CollectionStore = Reflux.createStore({ * Initialize the store. */ init() { - this.ns = null; - this.readonly = null; + this.collection = {}; }, /** @@ -20,11 +20,37 @@ const CollectionStore = Reflux.createStore({ * @param {Object} collection - The collection info. */ setCollection(collection) { - this.ns = collection._id; - this.readonly = collection.readonly; + this.collection = collection; if (collection._id) { - NamespaceStore.ns = this.ns; + NamespaceStore.ns = collection._id; } + }, + + /** + * Get the collection ns. + * + * @returns {String} The collection ns. + */ + ns() { + return this.collection._id; + }, + + /** + * Is the collection readonly? + * + * @returns {Boolean} If the collection is readonly. + */ + isReadonly() { + return this.collection.readonly; + }, + + /** + * Is the collection writable? + * + * @returns {Boolean} If the collection is writable. + */ + isWritable() { + return !this.isReadonly() && app.dataService.isWritable(); } }); diff --git a/src/internal-packages/collection-stats/lib/store/index.js b/src/internal-packages/collection-stats/lib/store/index.js index 9311efef09d..213b1363236 100644 --- a/src/internal-packages/collection-stats/lib/store/index.js +++ b/src/internal-packages/collection-stats/lib/store/index.js @@ -30,7 +30,7 @@ const CollectionStatsStore = Reflux.createStore({ */ loadCollectionStats: function(ns) { if (toNS(ns || '').collection) { - if (this.CollectionStore.readonly) { + if (this.CollectionStore.isReadonly()) { this.trigger(); } else { app.dataService.collection(ns, { readPreference: READ }, (err, result) => { diff --git a/src/internal-packages/crud/lib/component/document-list.jsx b/src/internal-packages/crud/lib/component/document-list.jsx index 2cc07a7072c..ad7f26cdd27 100644 --- a/src/internal-packages/crud/lib/component/document-list.jsx +++ b/src/internal-packages/crud/lib/component/document-list.jsx @@ -221,11 +221,10 @@ class DocumentList extends React.Component { * @return {Array} The document list item components. */ renderDocuments(docs) { - const editable = app.dataService.isWritable() && !this.CollectionStore.readonly; return _.map(docs, (doc) => { return (
  • - +
  • ); }); diff --git a/src/internal-packages/database/lib/components/collections-table.jsx b/src/internal-packages/database/lib/components/collections-table.jsx index e514e5cf338..f542b9751d7 100644 --- a/src/internal-packages/database/lib/components/collections-table.jsx +++ b/src/internal-packages/database/lib/components/collections-table.jsx @@ -38,13 +38,16 @@ class CollectionsTable extends React.Component { }); }); + const writable = app.dataService.isWritable(); + return (
    - + {writable ? + : null}
    diff --git a/src/internal-packages/explain/lib/components/compass-explain.jsx b/src/internal-packages/explain/lib/components/compass-explain.jsx index b4d99f0061e..95eea149317 100644 --- a/src/internal-packages/explain/lib/components/compass-explain.jsx +++ b/src/internal-packages/explain/lib/components/compass-explain.jsx @@ -73,7 +73,7 @@ class CompassExplain extends React.Component { return (
    - {this.CollectionStore.readonly ? this.renderReadonly() : this.renderComponent()} + {this.CollectionStore.isReadonly() ? this.renderReadonly() : this.renderComponent()}
    ); } diff --git a/src/internal-packages/explain/lib/stores/index.js b/src/internal-packages/explain/lib/stores/index.js index 22d25724a07..02490696852 100644 --- a/src/internal-packages/explain/lib/stores/index.js +++ b/src/internal-packages/explain/lib/stores/index.js @@ -123,7 +123,7 @@ const CompassExplainStore = Reflux.createStore({ if (!ns.database || !ns.collection) { return; } - if (this.CollectionStore.readonly) { + if (this.CollectionStore.isReadonly()) { this.setState(this.getInitialState()); } else { app.dataService.explain(ns.ns, filter, options, (err, explain) => { diff --git a/src/internal-packages/indexes/lib/component/index-header.jsx b/src/internal-packages/indexes/lib/component/index-header.jsx index 192ec354e97..5bb176ed068 100644 --- a/src/internal-packages/indexes/lib/component/index-header.jsx +++ b/src/internal-packages/indexes/lib/component/index-header.jsx @@ -18,6 +18,7 @@ class IndexHeader extends React.Component { constructor(props) { super(props); this.state = { sortOrder: ASC }; + this.CollectionStore = app.appRegistry.getStore('App.CollectionStore'); } /** @@ -59,7 +60,7 @@ class IndexHeader extends React.Component { - {app.dataService.isWritable() ? + {this.CollectionStore.isWritable() ? : null} diff --git a/src/internal-packages/indexes/lib/component/index.jsx b/src/internal-packages/indexes/lib/component/index.jsx index bddd5237f3b..43566dc557c 100644 --- a/src/internal-packages/indexes/lib/component/index.jsx +++ b/src/internal-packages/indexes/lib/component/index.jsx @@ -12,6 +12,11 @@ const app = require('ampersand-app'); */ class Index extends React.Component { + constructor(props) { + super(props); + this.CollectionStore = app.appRegistry.getStore('App.CollectionStore'); + } + /** * Render the index. * @@ -27,7 +32,7 @@ class Index extends React.Component { relativeSize={this.props.index.relativeSize} /> - {app.dataService.isWritable() ? + {this.CollectionStore.isWritable() ? : null} diff --git a/src/internal-packages/indexes/lib/component/indexes.jsx b/src/internal-packages/indexes/lib/component/indexes.jsx index b4e25591368..279ffb369a6 100644 --- a/src/internal-packages/indexes/lib/component/indexes.jsx +++ b/src/internal-packages/indexes/lib/component/indexes.jsx @@ -36,8 +36,10 @@ class Indexes extends React.Component { } determineState() { - const writable = app.dataService.isWritable() && !this.CollectionStore.readonly; - return { writable: writable, readonly: this.CollectionStore.readonly }; + return { + writable: this.CollectionStore.isWritable(), + readonly: this.CollectionStore.isReadonly() + }; } handleLoad() { diff --git a/src/internal-packages/indexes/lib/store/load-indexes-store.js b/src/internal-packages/indexes/lib/store/load-indexes-store.js index 97fc8ccf83b..ad63d91c8ff 100644 --- a/src/internal-packages/indexes/lib/store/load-indexes-store.js +++ b/src/internal-packages/indexes/lib/store/load-indexes-store.js @@ -29,7 +29,7 @@ const LoadIndexesStore = Reflux.createStore({ */ loadIndexes: function() { if (NamespaceStore.ns) { - if (this.CollectionStore.readonly) { + if (this.CollectionStore.isReadonly()) { this.trigger([]); } else { app.dataService.indexes(NamespaceStore.ns, { readPreference: READ }, (err, indexes) => { diff --git a/src/internal-packages/query/lib/component/sampling-message.jsx b/src/internal-packages/query/lib/component/sampling-message.jsx index 4b1218726bc..90c83cba02f 100644 --- a/src/internal-packages/query/lib/component/sampling-message.jsx +++ b/src/internal-packages/query/lib/component/sampling-message.jsx @@ -140,7 +140,7 @@ class SamplingMessage extends React.Component {
    Query returned {this.state.count} {noun}.  {this._loadedMessage()} - {!this.CollectionStore.readonly ? + {this.CollectionStore.isWritable() ?
    - + {writable ? + : null}
    diff --git a/src/internal-packages/validation/lib/components/validation.jsx b/src/internal-packages/validation/lib/components/validation.jsx index 81c65acb23b..3745f1158b7 100644 --- a/src/internal-packages/validation/lib/components/validation.jsx +++ b/src/internal-packages/validation/lib/components/validation.jsx @@ -94,7 +94,7 @@ class Validation extends React.Component { render() { return (
    - {this.CollectionStore.readonly ? this.renderReadonly() : this.renderComponent()} + {this.CollectionStore.isReadonly() ? this.renderReadonly() : this.renderComponent()}
    ); } diff --git a/src/internal-packages/validation/lib/stores/index.js b/src/internal-packages/validation/lib/stores/index.js index 180067b1b00..620bdf9202a 100644 --- a/src/internal-packages/validation/lib/stores/index.js +++ b/src/internal-packages/validation/lib/stores/index.js @@ -33,7 +33,6 @@ const ValidationStore = Reflux.createStore({ */ init() { this.lastFetchedValidatorDoc = {}; - this.CollectionStore = app.appRegistry.getStore('App.CollectionStore'); NamespaceStore.listen((ns) => { if (ns && toNS(ns).collection) { ValidationActions.fetchValidationRules(); @@ -238,8 +237,31 @@ const ValidationStore = Reflux.createStore({ }); }, + /** + * Determine if the collection is readonly. + * + * @note Durran: The wacky logic here is because the ampersand app is not + * loaded in the unit test environment and the validation tests fail since + * not app registry is found. Once we get rid of the ampersand app we can + * put the store set back into the init once we've sorted out the proper + * test strategy. + * + * @returns {Boolean} If the collection is readonly. + */ + _isCollectionReadonly() { + if (this.CollectionStore) { + return this.CollectionStore.isReadonly(); + } + const registry = app.appRegistry; + if (registry) { + this.CollectionStore = registry.getStore('App.CollectionStore'); + return this.CollectionStore.isReadonly(); + } + return false; + }, + fetchValidationRules() { - if (this.CollectionStore.readonly) { + if (this._isCollectionReadonly()) { this.setState(this.getInitialState()); } else { this.setState({