Skip to content

Commit

Permalink
fixed #44
Browse files Browse the repository at this point in the history
fixed small jqgrid multiple sort
moved the securityProvider to EasygridConfig
  • Loading branch information
tudor-malene committed Mar 11, 2014
1 parent 0a13547 commit b1eda57
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 42 deletions.
18 changes: 0 additions & 18 deletions grails-app/conf/DefaultEasygridConfig.groovy
Expand Up @@ -115,24 +115,6 @@ easygrid {
pageSize = 10
}

// default security provider
securityProvider = { grid, oper ->
return true
/*
if (!grid.roles) {
return true
}
def grantedRoles
if (Map.isAssignableFrom(grid.roles.getClass())) {
grantedRoles = grid.roles.findAll { op, role -> oper == op }.collect { op, role -> role }
} else if (List.isAssignableFrom(grid.roles.getClass())) {
grantedRoles = grid.roles
} else {
grantedRoles = [grid.roles]
}
SpringSecurityUtils.ifAllGranted(grantedRoles.inject('') { roles, role -> "${roles},${role}" })
*/
}

//default autocomplete settings
autocomplete {
Expand Down
Expand Up @@ -6,6 +6,7 @@ import org.codehaus.groovy.grails.commons.GrailsDomainClass
import org.codehaus.groovy.grails.commons.GrailsDomainClassProperty
import org.codehaus.groovy.grails.scaffolding.DomainClassPropertyComparator
import org.grails.datastore.mapping.query.Query
import org.grails.datastore.mapping.query.api.Criteria
import org.grails.plugin.easygrid.ColumnConfig
import org.grails.plugin.easygrid.Filter
import org.grails.plugin.easygrid.GridConfig
Expand Down Expand Up @@ -197,30 +198,28 @@ class GormDatasourceService {
* @param filters - map of filter closures
* @return
*/
DetachedCriteria createWhereQuery(gridConfig, filters) {
def initial = new DetachedCriteria(gridConfig.domainClass)
DetachedCriteria result = filters.inject(gridConfig.initialCriteria ? initial.build(gridConfig.initialCriteria) : initial) { DetachedCriteria criteria, Filter filter ->
def filterCriteria = getCriteria(filter);
if (filterCriteria instanceof Closure) {
criteria.and(filterCriteria)
} else {
//todo filterCriteria
filterCriteria.criteria.criteria.each { Query.Criterion criterion -> criteria.add(criterion) }
criteria
}
Criteria createWhereQuery(GridConfig gridConfig, List<Filter> filters) {
DetachedCriteria baseCriteria = new DetachedCriteria(gridConfig.domainClass)
if (gridConfig.initialCriteria) {
baseCriteria = baseCriteria.build(gridConfig.initialCriteria)
}

filters.collect { getCriteria(it) }.each { Closure filterCriteria ->
filterCriteria.resolveStrategy = Closure.DELEGATE_FIRST
filterCriteria.delegate = baseCriteria
filterCriteria(baseCriteria)
}

// add the filterpane stuff -if supported
/*
if (filterPaneService) {
filterPaneService.addFiltersToCriteria(result, params, gridConfig.domainClass)
}
*/
result
return baseCriteria
}


//todo - move to filter
def getCriteria(Filter filter) {
assert filter.searchFilter instanceof Closure
assert filter.searchFilter.parameterTypes.size() == 1
Expand All @@ -229,7 +228,7 @@ class GormDatasourceService {
filter.searchFilter.curry(filter.global ? params : filter)
}

DetachedCriteria addOrderBy(DetachedCriteria criteria, List orderBy) {
Criteria addOrderBy(Criteria criteria, List orderBy) {
orderBy.each {
criteria.order(it.sort, it.order)
}
Expand Down
Expand Up @@ -51,12 +51,14 @@ class JqueryGridService {
if (gridConfig.jqgrid?.multiSort) {
//In case when the data is obtained from the server the sidx parameter contain the order clause. It is a comma separated string in format field1 asc, field2 desc …, fieldN. Note that the last field does not not have asc or desc. It should be obtained from sord parameter
// When the option is true the behavior is a s follow
result.multiSort = params.sidx?.split(',')?.collect { String token ->
String[] tokens = token.trim().split(' ')
if (tokens.size() == 2) {
return [sort: tokens[0], order: tokens[1]]
} else {
return [sort: tokens[0], order: params.sord]
if(params.sidx){
result.multiSort = params.sidx.split(',')?.collect { String token ->
String[] tokens = token.trim().split(' ')
if (tokens.size() == 2) {
return [sort: tokens[0], order: tokens[1]]
} else {
return [sort: tokens[0], order: params.sord]
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions scripts/EasygridSetup.groovy
Expand Up @@ -7,7 +7,28 @@ target(easygridSetup: "The description of the script goes here!") {
configFile.createNewFile()
configFile << """
easygrid{
defaults{
//un-comment if you use spring security or implement your own with your framework
securityProvider = { grid, oper ->
return true
/*
if (!grid.roles) {
return true
}
def grantedRoles
if (Map.isAssignableFrom(grid.roles.getClass())) {
grantedRoles = grid.roles.findAll { op, role -> oper == op }.collect { op, role -> role }
} else if (List.isAssignableFrom(grid.roles.getClass())) {
grantedRoles = grid.roles
} else {
grantedRoles = [grid.roles]
}
SpringSecurityUtils.ifAllGranted(grantedRoles.inject('') { roles, role -> "\${roles},\${role}" })
*/
}
}
}
""".stripIndent()
}
Expand Down
Expand Up @@ -103,7 +103,9 @@ class GormDatasourceServiceSpec extends Specification {
//test criteria search
when:
domainRows = service.list(criteriaGridConfig, [maxRows: 10, rowOffset: 0],
[new Filter({ between("testIntProperty", 31, 80) }, '1')]
[new Filter(
{ between("testIntProperty", 31, 80) }
, '1')]
)
then:
10 == domainRows.size()
Expand Down Expand Up @@ -133,7 +135,13 @@ class GormDatasourceServiceSpec extends Specification {

//test criteria search
when:
domainRows = service.list(domainGridConfig, [maxRows: 10, rowOffset: 10], [new Filter({ filter -> between("testIntProperty", 31, 80) })])
domainRows = service.list(domainGridConfig, [maxRows: 10, rowOffset: 10],
[new Filter(
{ filter ->
between("testIntProperty", 31, 80)
}
)]
)
then:
10 == domainRows.size()
41 == domainRows[0].testIntProperty
Expand Down Expand Up @@ -318,7 +326,7 @@ class GormDatasourceServiceSpec extends Specification {

when:

def query = service.createWhereQuery([domainClass: PetTest], [
def query = service.createWhereQuery([domainClass: PetTest] as GridConfig, [
new Filter(
{ Filter filter ->
if (filter.paramValue.size() > 2) {
Expand Down Expand Up @@ -348,7 +356,7 @@ class GormDatasourceServiceSpec extends Specification {

when:

def query = service.createWhereQuery([domainClass: OwnerTest], [
def query = service.createWhereQuery([domainClass: OwnerTest] as GridConfig, [
new Filter(
{ Filter filter ->
if (filter.paramValue.size() == 2) {
Expand Down Expand Up @@ -398,6 +406,11 @@ class GormDatasourceServiceSpec extends Specification {
// name 'o.name'
// property 'owner.name'
enableFilter true
filterClosure { Filter filter ->
owner {
eq('name', 'John')
}
}
jqgrid {
}
}
Expand All @@ -420,6 +433,19 @@ class GormDatasourceServiceSpec extends Specification {
then:
5 == data.size()
'Bonkers' == data[0].name

when:
data = service.list(petsGridConfig, [:], [new Filter(petsGridConfig.columns['owner.name'])])
then:
2 == data.size()
'Bonkers' == data[0].name
'tommy' == data[1].name

when:
int cnt = service.countRows(petsGridConfig, [new Filter(petsGridConfig.columns['owner.name'])])
then:
2 == cnt

}

def "test order by multiple fields"() {
Expand Down

0 comments on commit b1eda57

Please sign in to comment.