Skip to content

Commit bcd4fde

Browse files
committed
#461 [mongo-support] Auto-detect filter by field name
1 parent 03a7427 commit bcd4fde

File tree

3 files changed

+77
-10
lines changed

3 files changed

+77
-10
lines changed

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ <h3>Output</h3>
565565
$('.set-mongo').on('click', function() {
566566
$('#builder').queryBuilder('setRulesFromMongo', {
567567
"$or": [{
568-
"name": {
568+
"username": {
569569
"$regex": "^(?!Mistic)"
570570
}
571571
}, {

src/plugins/mongodb-support/plugin.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,7 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
270270

271271
var opVal = mdbrl.call(self, value);
272272

273-
/**
274-
* Returns a filter identifier from the MongoDB field
275-
* @event changer:getMongoDBFieldID
276-
* @memberof module:plugins.MongoDbSupport
277-
* @param {string} field
278-
* @param {*} value
279-
* @returns {string}
280-
*/
281-
var id = self.change('getMongoDBFieldID', field, value);
273+
var id = self.getMongoDBFieldID(field, value);
282274

283275
/**
284276
* Modifies the rule generated from the MongoDB expression
@@ -320,6 +312,39 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
320312
*/
321313
setRulesFromMongo: function(query) {
322314
this.setRules(this.getRulesFromMongo(query));
315+
},
316+
317+
/**
318+
* Returns a filter identifier from the MongoDB field.
319+
* Automatically use the only one filter with a matching field, fires a changer otherwise.
320+
* @param {string} field
321+
* @param {*} value
322+
* @fires module:plugins.MongoDbSupport:changer:getMongoDBFieldID
323+
* @returns {string}
324+
* @private
325+
*/
326+
getMongoDBFieldID: function(field, value) {
327+
var matchingFilters = this.filters.filter(function(filter) {
328+
return filter.field === field;
329+
});
330+
331+
var id;
332+
if (matchingFilters.length === 1) {
333+
id = matchingFilters[0].id;
334+
}
335+
else {
336+
/**
337+
* Returns a filter identifier from the MongoDB field
338+
* @event changer:getMongoDBFieldID
339+
* @memberof module:plugins.MongoDbSupport
340+
* @param {string} field
341+
* @param {*} value
342+
* @returns {string}
343+
*/
344+
id = this.change('getMongoDBFieldID', field, value);
345+
}
346+
347+
return id;
323348
}
324349
});
325350

tests/plugins.mongo-support.module.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,48 @@ $(function(){
6262
);
6363
});
6464

65+
QUnit.test('Automatically use filter from field', function(assert) {
66+
var rules = {
67+
condition: 'AND',
68+
rules: [
69+
{
70+
id: 'name',
71+
operator: 'equal',
72+
value: 'Mistic'
73+
}
74+
]
75+
};
76+
77+
var mongo = {
78+
$and: [{
79+
username: 'Mistic'
80+
}]
81+
};
82+
83+
$b.queryBuilder({
84+
filters: [
85+
{
86+
id: 'name',
87+
field: 'username',
88+
type: 'string'
89+
},
90+
{
91+
id: 'last_days',
92+
field: 'display_date',
93+
type: 'integer'
94+
}
95+
]
96+
});
97+
98+
$b.queryBuilder('setRulesFromMongo', mongo);
99+
100+
assert.rulesMatch(
101+
$b.queryBuilder('getRules'),
102+
rules,
103+
'Should use "name" filter from "username" field'
104+
);
105+
});
106+
65107

66108
var all_operators_rules = {
67109
condition: 'AND',

0 commit comments

Comments
 (0)