1- $ ( function ( ) {
1+ $ ( function ( ) {
22 var $b = $ ( '#builder' ) ;
33
44 QUnit . module ( 'plugins.sql-support' , {
5- afterEach : function ( ) {
5+ afterEach : function ( ) {
66 $b . queryBuilder ( 'destroy' ) ;
77 }
88 } ) ;
99
10- QUnit . test ( 'Raw SQL' , function ( assert ) {
10+ QUnit . test ( 'Raw SQL' , function ( assert ) {
1111 $b . queryBuilder ( {
1212 filters : basic_filters ,
1313 rules : basic_rules
@@ -30,7 +30,7 @@ $(function () {
3030 ) ;
3131 } ) ;
3232
33- QUnit . test ( 'Placeholder SQL' , function ( assert ) {
33+ QUnit . test ( 'Placeholder SQL' , function ( assert ) {
3434 $b . queryBuilder ( {
3535 filters : basic_filters ,
3636 rules : basic_rules
@@ -53,7 +53,7 @@ $(function () {
5353 ) ;
5454 } ) ;
5555
56- QUnit . test ( 'Numbered SQL' , function ( assert ) {
56+ QUnit . test ( 'Numbered SQL' , function ( assert ) {
5757 $b . queryBuilder ( {
5858 filters : basic_filters ,
5959 rules : basic_rules
@@ -92,7 +92,7 @@ $(function () {
9292 ) ;
9393 } ) ;
9494
95- QUnit . test ( 'Named SQL' , function ( assert ) {
95+ QUnit . test ( 'Named SQL' , function ( assert ) {
9696 $b . queryBuilder ( {
9797 filters : basic_filters ,
9898 rules : basic_rules
@@ -131,7 +131,7 @@ $(function () {
131131 ) ;
132132 } ) ;
133133
134- QUnit . test ( 'All operators' , function ( assert ) {
134+ QUnit . test ( 'All operators' , function ( assert ) {
135135 $b . queryBuilder ( {
136136 filters : basic_filters ,
137137 rules : all_operators_rules
@@ -154,14 +154,14 @@ $(function () {
154154 ) ;
155155 } ) ;
156156
157- QUnit . test ( 'Nested rules' , function ( assert ) {
157+ QUnit . test ( 'Nested rules' , function ( assert ) {
158158
159159 $b . queryBuilder ( {
160160 filters : [
161- { id : 'a' , type : 'integer' } ,
162- { id : 'b' , type : 'integer' } ,
163- { id : 'c' , type : 'integer' } ,
164- { id : 'd' , type : 'integer' }
161+ { id : 'a' , type : 'integer' } ,
162+ { id : 'b' , type : 'integer' } ,
163+ { id : 'c' , type : 'integer' } ,
164+ { id : 'd' , type : 'integer' }
165165 ]
166166 } ) ;
167167
@@ -191,7 +191,7 @@ $(function () {
191191 ) ;
192192 } ) ;
193193
194- QUnit . test ( 'Custom export/parsing' , function ( assert ) {
194+ QUnit . test ( 'Custom export/parsing' , function ( assert ) {
195195 var rules = {
196196 condition : 'AND' ,
197197 rules : [
@@ -225,13 +225,13 @@ $(function () {
225225 ]
226226 } ) ;
227227
228- $b . on ( 'ruleToSQL.queryBuilder.filter' , function ( e , rule , sqlValue , sqlOperator ) {
228+ $b . on ( 'ruleToSQL.queryBuilder.filter' , function ( e , rule , sqlValue , sqlOperator ) {
229229 if ( rule . id === 'last_days' ) {
230230 e . value = rule . field + ' ' + sqlOperator ( 'DATE_SUB(NOW(), INTERVAL ' + sqlValue + ' DAY)' ) ;
231231 }
232232 } ) ;
233233
234- $b . on ( 'parseSQLNode.queryBuilder.filter' , function ( e ) {
234+ $b . on ( 'parseSQLNode.queryBuilder.filter' , function ( e ) {
235235 var data = e . value ;
236236 // left must be the field name and right must be the date_sub function
237237 if ( data . left && data . left . value == 'display_date' && data . operation == '>' && data . right && data . right . name == 'DATE_SUB' ) {
@@ -269,6 +269,44 @@ $(function () {
269269 ) ;
270270 } ) ;
271271
272+ QUnit . test ( 'Automatically use filter from field' , function ( assert ) {
273+ var rules = {
274+ condition : 'AND' ,
275+ rules : [
276+ {
277+ id : 'name' ,
278+ operator : 'equal' ,
279+ value : 'Mistic'
280+ }
281+ ]
282+ } ;
283+
284+ var sql = 'username = \'Mistic\'' ;
285+
286+ $b . queryBuilder ( {
287+ filters : [
288+ {
289+ id : 'name' ,
290+ field : 'username' ,
291+ type : 'string'
292+ } ,
293+ {
294+ id : 'last_days' ,
295+ field : 'display_date' ,
296+ type : 'integer'
297+ }
298+ ]
299+ } ) ;
300+
301+ $b . queryBuilder ( 'setRulesFromSQL' , sql ) ;
302+
303+ assert . rulesMatch (
304+ $b . queryBuilder ( 'getRules' ) ,
305+ rules ,
306+ 'Should use "name" filter from "username" field'
307+ ) ;
308+ } ) ;
309+
272310
273311 var basic_rules_sql_raw = {
274312 sql : 'price < 10.25 AND name IS NULL AND ( category IN(\'mo\', \'mu\') OR id != \'1234-azer-5678\' ) '
0 commit comments