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, editing: false,
everEditing: false, everEditing: false,
createDisabled: function() {
return (this.get('newQueryName') || "").trim().length === 0;
}.property('newQueryName'),
selectedItem: function() { selectedItem: function() {
const _id = this.get('selectedQueryId'); const id = parseInt(this.get('selectedQueryId'));
const id = parseInt(_id); const item = this.get('content').find(q => q.get('id') === id);
const item = this.get('content').find(function(q) {
return q.get('id') === id;
});
return item || NoQuery; return item || NoQuery;
}.property('selectedQueryId'), }.property('selectedQueryId'),
othersDirty: function() { othersDirty: function() {
const selected = this.get('selectedItem'); const selected = this.get('selectedItem');
return !!this.get('content').find(function(q) { return !!this.get('content').find(q => q !== selected && q.get('dirty'));
return q !== selected && q.get('dirty');
});
}.property('selectedItem', 'selectedItem.dirty'), }.property('selectedItem', 'selectedItem.dirty'),
setEverEditing: function() { setEverEditing: function() {
@ -104,15 +103,15 @@ export default Ember.Controller.extend({
}, },
create() { create() {
const self = this; const name = this.get("newQueryName").trim();
this.set('loading', true); this.set('loading', true);
this.set('showCreate', false); this.set('showCreate', false);
var newQuery = this.store.createRecord('query', {name: this.get('newQueryName')}); this.store
newQuery.save().then(function(result) { .createRecord('query', { name })
self.addCreatedRecord(result.target); .save()
}).catch(popupAjaxError).finally(function() { .then(result => this.addCreatedRecord(result.target) )
self.set('loading', false); .catch(popupAjaxError)
}); .finally(() => this.set('loading', false));
}, },
discard() { discard() {

View File

@ -12,7 +12,7 @@
{{#if showCreate}} {{#if showCreate}}
<div class="query-create"> <div class="query-create">
{{text-field value=newQueryName placeholderKey="explorer.create_placeholder"}} {{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> </div>
{{/if}} {{/if}}

View File

@ -583,9 +583,7 @@ SQL
end end
def slug def slug
s = Slug.for(name) Slug.for(name).presence || "query-#{id}"
s = "query-#{id}" unless s.present?
s
end end
def params def params
@ -619,11 +617,9 @@ SQL
def self.from_hash(h) def self.from_hash(h)
query = DataExplorer::Query.new query = DataExplorer::Query.new
[:name, :description, :sql].each do |sym| [:name, :description, :sql].each do |sym|
query.send("#{sym}=", h[sym]) if h[sym] query.send("#{sym}=", h[sym].strip) if h[sym]
end
if h[:id]
query.id = h[:id].to_i
end end
query.id = h[:id].to_i if h[:id]
query query
end end
@ -637,8 +633,7 @@ SQL
end end
def self.find(id, opts = {}) def self.find(id, opts = {})
hash = DataExplorer.pstore_get("q:#{id}") unless hash = DataExplorer.pstore_get("q:#{id}")
unless hash
return DataExplorer::Query.new if opts[:ignore_deleted] return DataExplorer::Query.new if opts[:ignore_deleted]
raise Discourse::NotFound raise Discourse::NotFound
end end
@ -647,9 +642,7 @@ SQL
def save def save
check_params! check_params!
unless @id && @id > 0 @id = self.class.alloc_id unless @id && @id > 0
@id = self.class.alloc_id
end
DataExplorer.pstore_set "q:#{id}", to_hash DataExplorer.pstore_set "q:#{id}", to_hash
end end