Fixed issue #1966: Paginated search results on Organizations can never be empty for queries that have at least one result (on an earlier page).
This commit is contained in:
parent
fd96ac58e6
commit
74dd1b21ec
1 changed files with 20 additions and 16 deletions
|
@ -77,24 +77,28 @@ returns
|
||||||
'name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"
|
'name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"
|
||||||
).order('name').offset(offset).limit(limit).to_a
|
).order('name').offset(offset).limit(limit).to_a
|
||||||
|
|
||||||
# if only a few organizations are found, search for names of users
|
# use result independent of size if an explicit offset is given
|
||||||
if organizations.length <= 3
|
# this is the case for e.g. paginated searches
|
||||||
organizations_by_user = Organization.select('DISTINCT(organizations.id), organizations.name').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where(
|
return organizations if params[:offset].present?
|
||||||
'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%"
|
return organizations if organizations.length > 3
|
||||||
).order('organizations.name').limit(limit)
|
|
||||||
organizations_by_user.each do |organization_by_user|
|
|
||||||
organization_exists = false
|
|
||||||
organizations.each do |organization|
|
|
||||||
if organization.id == organization_by_user.id
|
|
||||||
organization_exists = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# get model with full data
|
# if only a few organizations are found, search for names of users
|
||||||
if !organization_exists
|
organizations_by_user = Organization.select('DISTINCT(organizations.id), organizations.name').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where(
|
||||||
organizations.push Organization.find(organization_by_user.id)
|
'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%"
|
||||||
end
|
).order('organizations.name').limit(limit)
|
||||||
|
|
||||||
|
organizations_by_user.each do |organization_by_user|
|
||||||
|
|
||||||
|
organization_exists = false
|
||||||
|
organizations.each do |organization|
|
||||||
|
next if organization.id != organization_by_user.id
|
||||||
|
organization_exists = true
|
||||||
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# get model with full data
|
||||||
|
next if organization_exists
|
||||||
|
organizations.push Organization.find(organization_by_user.id)
|
||||||
end
|
end
|
||||||
organizations
|
organizations
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue