2015-04-01 22:34:14 +01:00
|
|
|
class Search < Struct.new(:query, :user, :paginate_params)
|
2014-11-17 21:17:03 +00:00
|
|
|
include ActiveModel::Conversion
|
|
|
|
extend ActiveModel::Naming
|
2012-11-21 15:04:41 +00:00
|
|
|
|
2014-11-17 21:17:03 +00:00
|
|
|
TYPES = %w(projects users groups platforms)
|
|
|
|
|
|
|
|
|
|
|
|
TYPES.each do |type|
|
|
|
|
define_method type do
|
|
|
|
find_collection(type)
|
2012-11-21 15:04:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-17 21:17:03 +00:00
|
|
|
private
|
2012-11-21 15:04:41 +00:00
|
|
|
|
2014-11-17 21:17:03 +00:00
|
|
|
def find_collection(type)
|
|
|
|
scope =
|
|
|
|
if type == 'users'
|
|
|
|
User.opened
|
|
|
|
else
|
2015-04-01 22:34:14 +01:00
|
|
|
klass = type.classify.constantize
|
2015-04-14 21:21:37 +01:00
|
|
|
"#{klass}Policy::Scope".constantize.new(user, klass).show
|
2014-11-17 21:17:03 +00:00
|
|
|
end
|
|
|
|
scope.search(query).
|
|
|
|
search_order.
|
|
|
|
paginate(paginate_params)
|
2012-11-21 15:04:41 +00:00
|
|
|
end
|
2014-11-17 21:17:03 +00:00
|
|
|
|
2015-04-01 22:34:14 +01:00
|
|
|
end
|