Skip to content

Commit

Permalink
#461 [mongo-support] Auto-detect filter by field name
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Apr 13, 2017
1 parent 03a7427 commit bcd4fde
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ <h3>Output</h3>
$('.set-mongo').on('click', function() {
$('#builder').queryBuilder('setRulesFromMongo', {
"$or": [{
"name": {
"username": {
"$regex": "^(?!Mistic)"
}
}, {
Expand Down
43 changes: 34 additions & 9 deletions src/plugins/mongodb-support/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,7 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {

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

/**
* Returns a filter identifier from the MongoDB field
* @event changer:getMongoDBFieldID
* @memberof module:plugins.MongoDbSupport
* @param {string} field
* @param {*} value
* @returns {string}
*/
var id = self.change('getMongoDBFieldID', field, value);
var id = self.getMongoDBFieldID(field, value);

/**
* Modifies the rule generated from the MongoDB expression
Expand Down Expand Up @@ -320,6 +312,39 @@ QueryBuilder.extend(/** @lends module:plugins.MongoDbSupport.prototype */ {
*/
setRulesFromMongo: function(query) {
this.setRules(this.getRulesFromMongo(query));
},

/**
* Returns a filter identifier from the MongoDB field.
* Automatically use the only one filter with a matching field, fires a changer otherwise.
* @param {string} field
* @param {*} value
* @fires module:plugins.MongoDbSupport:changer:getMongoDBFieldID
* @returns {string}
* @private
*/
getMongoDBFieldID: function(field, value) {
var matchingFilters = this.filters.filter(function(filter) {
return filter.field === field;
});

var id;
if (matchingFilters.length === 1) {
id = matchingFilters[0].id;
}
else {
/**
* Returns a filter identifier from the MongoDB field
* @event changer:getMongoDBFieldID
* @memberof module:plugins.MongoDbSupport
* @param {string} field
* @param {*} value
* @returns {string}
*/
id = this.change('getMongoDBFieldID', field, value);
}

return id;
}
});

Expand Down
42 changes: 42 additions & 0 deletions tests/plugins.mongo-support.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,48 @@ $(function(){
);
});

QUnit.test('Automatically use filter from field', function(assert) {
var rules = {
condition: 'AND',
rules: [
{
id: 'name',
operator: 'equal',
value: 'Mistic'
}
]
};

var mongo = {
$and: [{
username: 'Mistic'
}]
};

$b.queryBuilder({
filters: [
{
id: 'name',
field: 'username',
type: 'string'
},
{
id: 'last_days',
field: 'display_date',
type: 'integer'
}
]
});

$b.queryBuilder('setRulesFromMongo', mongo);

assert.rulesMatch(
$b.queryBuilder('getRules'),
rules,
'Should use "name" filter from "username" field'
);
});


var all_operators_rules = {
condition: 'AND',
Expand Down

0 comments on commit bcd4fde

Please sign in to comment.