FIX: prevent creation of query with an empty name

This commit is contained in:
Régis Hanol 2018-05-09 22:22:39 +02:00
parent aca7b188f6
commit 7c94f98436
3 changed files with 20 additions and 28 deletions

View File

@ -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() {

View File

@ -12,7 +12,7 @@
{{#if showCreate}}
<div class="query-create">
{{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"}}
</div>
{{/if}}

View File

@ -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