rosa-build/app/models/search.rb

29 lines
758 B
Ruby
Raw Normal View History

2012-11-21 15:04:41 +00:00
class Search
TYPES = ['projects', 'users', 'groups', 'platforms']
2014-09-30 13:08:45 +01:00
def self.by_term_and_type(term, type, ability, paginate_params)
2012-11-21 15:04:41 +00:00
results = {}
case type
when 'all'
2014-09-30 13:08:45 +01:00
TYPES.each{ |t| results[t] = find_collection(t, term, ability, paginate_params) }
2012-11-21 15:04:41 +00:00
when *TYPES
2014-09-30 13:08:45 +01:00
results[type] = find_collection(type, term, ability, paginate_params)
2012-11-21 15:04:41 +00:00
end
results
end
class << self
protected
2014-09-30 13:08:45 +01:00
def find_collection(type, term, ability, paginate_params)
2014-09-30 16:10:06 +01:00
scope = if type == 'users'
User.opened
else
2014-10-02 14:33:31 +01:00
type.classify.constantize.accessible_by(ability, :show)
2014-09-30 16:10:06 +01:00
end
scope.search(term).
search_order.
paginate(paginate_params)
2012-11-21 15:04:41 +00:00
end
end
end