2018-10-10 07:56:23 -04:00
|
|
|
import RestModel from "discourse/models/rest";
|
2015-06-25 17:53:03 -04:00
|
|
|
|
2015-07-02 12:19:30 -04:00
|
|
|
const Query = RestModel.extend({
|
2015-06-30 13:20:22 -04:00
|
|
|
dirty: false,
|
2015-06-30 18:12:12 -04:00
|
|
|
params: {},
|
2015-07-02 12:15:55 -04:00
|
|
|
results: null,
|
2015-06-30 18:12:12 -04:00
|
|
|
|
2015-06-30 22:51:38 -04:00
|
|
|
_init: function() {
|
|
|
|
this._super();
|
2018-10-10 07:56:23 -04:00
|
|
|
this.set("dirty", false);
|
|
|
|
}.on("init"),
|
2015-06-30 22:51:38 -04:00
|
|
|
|
2015-06-30 18:12:12 -04:00
|
|
|
_initParams: function() {
|
|
|
|
this.resetParams();
|
2018-10-10 07:56:23 -04:00
|
|
|
}
|
|
|
|
.on("init")
|
|
|
|
.observes("param_info"),
|
2015-06-30 22:51:38 -04:00
|
|
|
|
2015-06-30 13:20:22 -04:00
|
|
|
markDirty: function() {
|
2018-10-10 07:56:23 -04:00
|
|
|
this.set("dirty", true);
|
|
|
|
}.observes("name", "description", "sql"),
|
2015-06-30 13:20:22 -04:00
|
|
|
|
|
|
|
markNotDirty() {
|
2018-10-10 07:56:23 -04:00
|
|
|
this.set("dirty", false);
|
2015-06-30 13:20:22 -04:00
|
|
|
},
|
|
|
|
|
2015-07-14 19:01:38 -04:00
|
|
|
hasParams: function() {
|
2018-10-10 07:56:23 -04:00
|
|
|
return this.get("param_info.length") > 0;
|
|
|
|
}.property("param_info"),
|
2015-07-14 19:01:38 -04:00
|
|
|
|
2015-06-30 18:12:12 -04:00
|
|
|
resetParams() {
|
2015-07-02 12:19:30 -04:00
|
|
|
const newParams = {};
|
2018-10-10 07:56:23 -04:00
|
|
|
const oldParams = this.get("params");
|
|
|
|
const paramInfo = this.get("param_info") || [];
|
2015-07-14 19:01:38 -04:00
|
|
|
paramInfo.forEach(function(pinfo) {
|
|
|
|
const name = pinfo.identifier;
|
|
|
|
if (oldParams[pinfo.identifier]) {
|
2015-07-02 12:19:30 -04:00
|
|
|
newParams[name] = oldParams[name];
|
2018-10-10 07:56:23 -04:00
|
|
|
} else if (pinfo["default"] !== null) {
|
|
|
|
newParams[name] = pinfo["default"];
|
|
|
|
} else if (pinfo["type"] === "boolean") {
|
|
|
|
newParams[name] = "false";
|
2015-06-30 18:12:12 -04:00
|
|
|
} else {
|
2018-10-10 07:56:23 -04:00
|
|
|
newParams[name] = "";
|
2015-06-30 18:12:12 -04:00
|
|
|
}
|
|
|
|
});
|
2018-10-10 07:56:23 -04:00
|
|
|
this.set("params", newParams);
|
2015-06-30 18:12:12 -04:00
|
|
|
},
|
|
|
|
|
2015-06-30 15:52:17 -04:00
|
|
|
downloadUrl: function() {
|
|
|
|
// TODO - can we change this to use the store/adapter?
|
2018-10-10 07:56:23 -04:00
|
|
|
return Discourse.getURL(
|
|
|
|
"/admin/plugins/explorer/queries/" + this.get("id") + ".json?export=1"
|
|
|
|
);
|
|
|
|
}.property("id"),
|
2015-06-30 15:52:17 -04:00
|
|
|
|
2015-06-25 17:53:03 -04:00
|
|
|
createProperties() {
|
2018-10-10 07:56:23 -04:00
|
|
|
if (this.get("sql")) {
|
2015-06-30 15:52:17 -04:00
|
|
|
// Importing
|
|
|
|
return this.updateProperties();
|
|
|
|
}
|
2015-06-26 12:16:09 -04:00
|
|
|
return this.getProperties("name");
|
|
|
|
},
|
|
|
|
|
|
|
|
updateProperties() {
|
2015-06-30 15:52:17 -04:00
|
|
|
let props = this.getProperties(Query.updatePropertyNames);
|
2018-10-10 07:56:23 -04:00
|
|
|
if (this.get("destroyed")) {
|
|
|
|
props.id = this.get("id");
|
2015-06-30 15:52:17 -04:00
|
|
|
}
|
|
|
|
return props;
|
2015-06-25 17:53:03 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-30 13:20:22 -04:00
|
|
|
Query.reopenClass({
|
2019-01-28 02:00:17 -05:00
|
|
|
updatePropertyNames: ["name", "description", "sql", "created_by", "created_at", "last_run_at"]
|
2015-06-30 13:20:22 -04:00
|
|
|
});
|
|
|
|
|
2015-06-25 17:53:03 -04:00
|
|
|
export default Query;
|