From ee1cc2d96d8a89aec1763ab35d28f7b0b8008cad Mon Sep 17 00:00:00 2001 From: Kane York Date: Tue, 30 Jun 2015 10:20:22 -0700 Subject: [PATCH] Tuesday morning, 10AM --- .../controllers/admin-plugins-explorer.js.es6 | 50 ++++++++++--------- .../javascripts/discourse/models/query.js.es6 | 26 +++++++++- .../templates/admin/plugins-explorer-show.hbs | 4 +- .../templates/admin/plugins-explorer.hbs | 22 ++------ assets/stylesheets/tagging.scss | 22 ++++---- plugin.rb | 19 +++---- 6 files changed, 79 insertions(+), 64 deletions(-) diff --git a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 index 8a43665..e307996 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 @@ -2,23 +2,24 @@ import showModal from 'discourse/lib/show-modal'; import Query from 'discourse/plugins/discourse-data-explorer/discourse/models/query'; export default Ember.ArrayController.extend({ - selectedItem: null, + selectedQueryId: null, dirty: false, loading: false, - markDirty: function() { - this.set('dirty', true); - }.observes('selectedItem.name', 'selectedItem.description', 'selectedItem.sql'), + explain: false, + + saveDisabled: Ember.computed.not('selectedItem.dirty'), + runDisabled: Ember.computed.alias('selectedItem.dirty'), + + selectedItem: function() { + const _id = this.get('selectedQueryId'); + const id = parseInt(_id); + return this.get('content').find(function(q) { + return q.get('id') === id; + }); + }.property('selectedQueryId'), actions: { - selectItem(item) { - this.setProperties({ - selectedItem: item, - editName: false - }); - this.set('dirty', false); - }, - dummy() {}, create() { @@ -28,7 +29,7 @@ export default Ember.ArrayController.extend({ newQuery.save().then(function(result) { self.pushObject(result.target); self.set('selectedItem', result.target); - self.set('dirty', false); + self.set('selectedItem.dirty', false); }).finally(function() { self.set('loading', false); }); @@ -39,17 +40,16 @@ export default Ember.ArrayController.extend({ }, editName() { - this.setProperties({ - editName: true, - dirty: true - }); + this.set('editName', true); }, save() { const self = this; this.set('loading', true); - this.get('selectedItem').save().then(function(result) { - debugger; + this.get('selectedItem').save().then(function() { + const query = self.get('selectedItem'); + query.markNotDirty(); + self.set('editName', false); }).finally(function() { self.set('loading', false); }); @@ -58,21 +58,25 @@ export default Ember.ArrayController.extend({ discard() { const self = this; this.set('loading', true); - this.store.find('query', this.selectedItem.id).then(function(result) { - self.set('selectedItem', result); + this.store.find('query', this.get('selectedItem.id')).then(function(result) { + debugger; + const query = self.get('selectedItem'); + query.setProperties(result.getProperties(Query.updatePropertyNames)); + query.markNotDirty(); + self.set('editName', false); }).finally(function() { self.set('loading', false); }); }, run() { - if (this.get('dirty')) { + if (this.get('selectedItem.dirty')) { self.set('results', {errors: [I18n.t('errors.explorer.dirty')]}); return; } const self = this; this.set('loading', true); - Discourse.ajax("/admin/plugins/explorer/query/" + this.get('selectedItem.id') + "/run", { + Discourse.ajax("/admin/plugins/explorer/queries/" + this.get('selectedItem.id') + "/run", { type: "POST", data: { params: JSON.stringify({foo: 34}), diff --git a/assets/javascripts/discourse/models/query.js.es6 b/assets/javascripts/discourse/models/query.js.es6 index ac00458..63d95c2 100644 --- a/assets/javascripts/discourse/models/query.js.es6 +++ b/assets/javascripts/discourse/models/query.js.es6 @@ -1,12 +1,30 @@ import RestModel from 'discourse/models/rest'; -const Query = RestModel.extend({ +let Query; +Query = RestModel.extend({ + dirty: false, + + markDirty: function() { + this.set('dirty', true); + }.observes('name', 'description', 'sql', 'defaults'), + + markNotDirty() { + this.set('dirty', false); + }, + + listName: function() { + if (this.get('dirty')) { + return this.get('name') + " (*)"; + } + return this.get('name'); + }.property('name', 'dirty'), + createProperties() { return this.getProperties("name"); }, updateProperties() { - return this.getProperties("name", "description", "sql", "defaults"); + return this.getProperties(Query.updatePropertyNames); }, run() { @@ -14,4 +32,8 @@ const Query = RestModel.extend({ } }); +Query.reopenClass({ + updatePropertyNames: ["name", "description", "sql", "defaults"] +}); + export default Query; diff --git a/assets/javascripts/discourse/templates/admin/plugins-explorer-show.hbs b/assets/javascripts/discourse/templates/admin/plugins-explorer-show.hbs index 17d39a6..317d31a 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-explorer-show.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-explorer-show.hbs @@ -4,7 +4,7 @@ {{text-field value=selectedItem.name}} {{else}}

{{selectedItem.name}}

- {{d-button action="editName" icon="pencil" class="no-text"}} + {{d-button action="editName" icon="pencil" class="no-text btn-small"}} {{/if}}
@@ -18,6 +18,6 @@ {{textarea value=selectedItem.sql}}
{{d-button action="save" label="explorer.save" disabled=saveDisabled}} - {{d-button action="discard" class="no-text danger" icon="trash" disabled=saveDisabled}} + {{d-button action="discard" class="no-text btn-danger" icon="undo" disabled=saveDisabled}} {{d-button action="run" label="explorer.run" disabled=runDisabled}} {{/if}} diff --git a/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs b/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs index 7258a55..d4b8f1d 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs @@ -2,25 +2,9 @@ {{text-field value=newQueryName placeholderKey="explorer.create_placeholder"}} {{d-button action="create" label="explorer.create" icon="plus"}} {{d-button action="importQuery" label="explorer.import.label" icon="upload" class="import-button"}} -
- - - - - - - - - {{#each content as |query|}} - - - - - {{/each}} - -
NameDescription
{{query.name}}{{query.description}}
-
-
+ +{{combo-box valueAttribute="id" value=selectedQueryId nameProperty="name" content=content castInteger="true"}} +
{{partial "admin/plugins-explorer-show" model=selectedItem}}
diff --git a/assets/stylesheets/tagging.scss b/assets/stylesheets/tagging.scss index ef5871a..8e46739 100644 --- a/assets/stylesheets/tagging.scss +++ b/assets/stylesheets/tagging.scss @@ -4,15 +4,19 @@ max-height: 15em; } -.query-edit .sql textarea { - width: 800px; - height: 100px; - font-family: monospace; - border-color: $tertiary; -} - -.query-edit .desc textarea { - width: 200px; +.query-edit { + .name h2 { + display: inline-block; + } + .desc textarea { + width: 200px; + } + .sql textarea { + width: 800px; + height: 100px; + font-family: monospace; + border-color: $tertiary; + } } .query-list, .query-edit, .query-results { diff --git a/plugin.rb b/plugin.rb index 5b15886..3c5127c 100644 --- a/plugin.rb +++ b/plugin.rb @@ -179,7 +179,9 @@ SQL end def self.find(id) - from_hash DataExplorer.pstore_get("q:#{id}") + hash = DataExplorer.pstore_get("q:#{id}") + raise Discourse::NotFound unless hash + from_hash hash end def save @@ -254,11 +256,11 @@ SQL query = DataExplorer::Query.find(params[:id].to_i) hash = params.require(:query) [:name, :sql, :defaults, :description].each do |sym| - query.send("#{sym}=", params[sym]) if hash[sym] + query.send("#{sym}=", hash[sym]) if hash[sym] end query.save - render_serialized query, DataExplorer::QuerySerializer, root: 'queries' + render_serialized query, DataExplorer::QuerySerializer, root: true end def destroy @@ -323,12 +325,11 @@ SQL root to: "query#index" get 'queries' => "query#index" post 'queries' => "query#create" - # GET /query/:id -> explorer#show - # PUT /query/:id -> explorer#update - # DELETE /query/:id -> explorer#destroy - resources :query - get 'query/parse_params' => "query#parse_params" - post 'query/:id/run' => "query#run" + get 'queries/:id' => "query#show" + put 'queries/:id' => "query#update" + delete 'queries/:id' => "query#destroy" + get 'queries/parse_params' => "query#parse_params" + post 'queries/:id/run' => "query#run" end Discourse::Application.routes.append do