diff --git a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 index b6d213b..42a774b 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 @@ -21,20 +21,19 @@ export default Ember.Controller.extend({ editing: false, everEditing: false, + createDisabled: function() { + return (this.get('newQueryName') || "").trim().length === 0; + }.property('newQueryName'), + selectedItem: function() { - const _id = this.get('selectedQueryId'); - const id = parseInt(_id); - const item = this.get('content').find(function(q) { - return q.get('id') === id; - }); + const id = parseInt(this.get('selectedQueryId')); + const item = this.get('content').find(q => q.get('id') === id); return item || NoQuery; }.property('selectedQueryId'), othersDirty: function() { const selected = this.get('selectedItem'); - return !!this.get('content').find(function(q) { - return q !== selected && q.get('dirty'); - }); + return !!this.get('content').find(q => q !== selected && q.get('dirty')); }.property('selectedItem', 'selectedItem.dirty'), setEverEditing: function() { @@ -104,15 +103,15 @@ export default Ember.Controller.extend({ }, create() { - const self = this; + const name = this.get("newQueryName").trim(); this.set('loading', true); this.set('showCreate', false); - var newQuery = this.store.createRecord('query', {name: this.get('newQueryName')}); - newQuery.save().then(function(result) { - self.addCreatedRecord(result.target); - }).catch(popupAjaxError).finally(function() { - self.set('loading', false); - }); + this.store + .createRecord('query', { name }) + .save() + .then(result => this.addCreatedRecord(result.target) ) + .catch(popupAjaxError) + .finally(() => this.set('loading', false)); }, discard() { diff --git a/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs b/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs index ee3a4fd..b0847c4 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-explorer.hbs @@ -12,7 +12,7 @@ {{#if showCreate}}
{{text-field value=newQueryName placeholderKey="explorer.create_placeholder"}} - {{d-button action="create" label="explorer.create" icon="plus" class="btn-primary"}} + {{d-button action="create" disabled=createDisabled label="explorer.create" icon="plus" class="btn-primary"}}
{{/if}} diff --git a/plugin.rb b/plugin.rb index e9d11ab..b1c5220 100644 --- a/plugin.rb +++ b/plugin.rb @@ -583,9 +583,7 @@ SQL end def slug - s = Slug.for(name) - s = "query-#{id}" unless s.present? - s + Slug.for(name).presence || "query-#{id}" end def params @@ -619,11 +617,9 @@ SQL def self.from_hash(h) query = DataExplorer::Query.new [:name, :description, :sql].each do |sym| - query.send("#{sym}=", h[sym]) if h[sym] - end - if h[:id] - query.id = h[:id].to_i + query.send("#{sym}=", h[sym].strip) if h[sym] end + query.id = h[:id].to_i if h[:id] query end @@ -637,8 +633,7 @@ SQL end def self.find(id, opts = {}) - hash = DataExplorer.pstore_get("q:#{id}") - unless hash + unless hash = DataExplorer.pstore_get("q:#{id}") return DataExplorer::Query.new if opts[:ignore_deleted] raise Discourse::NotFound end @@ -647,9 +642,7 @@ SQL def save check_params! - unless @id && @id > 0 - @id = self.class.alloc_id - end + @id = self.class.alloc_id unless @id && @id > 0 DataExplorer.pstore_set "q:#{id}", to_hash end