improve search

This commit is contained in:
Alexander Machehin 2014-09-30 18:08:45 +06:00
parent 6339880d49
commit 92db8c7968
2 changed files with 12 additions and 8 deletions

View File

@ -8,6 +8,7 @@ class SearchController < ApplicationController
Search.by_term_and_type( Search.by_term_and_type(
@query, @query,
@type, @type,
current_ability,
{page: params[:page]} {page: params[:page]}
).each do |k, v| ).each do |k, v|
var = :"@#{k}" var = :"@#{k}"

View File

@ -1,13 +1,13 @@
class Search class Search
TYPES = ['projects', 'users', 'groups', 'platforms'] TYPES = ['projects', 'users', 'groups', 'platforms']
def self.by_term_and_type(term, type, paginate_params) def self.by_term_and_type(term, type, ability, paginate_params)
results = {} results = {}
case type case type
when 'all' when 'all'
TYPES.each{ |t| results[t] = find_collection(t, term, paginate_params) } TYPES.each{ |t| results[t] = find_collection(t, term, ability, paginate_params) }
when *TYPES when *TYPES
results[type] = find_collection(type, term, paginate_params) results[type] = find_collection(type, term, ability, paginate_params)
end end
results results
end end
@ -15,9 +15,12 @@ class Search
class << self class << self
protected protected
def find_collection(type, term, paginate_params) def find_collection(type, term, ability, paginate_params)
type.classify.constantize.opened. if type == 'users'
search(term). User.opened
else
type.classify.constantize.accessible_by(ability, :read)
end.search(term).
search_order. search_order.
paginate(paginate_params) paginate(paginate_params)
end end