2017-04-28 18:06:27 -04:00
|
|
|
class SnippetsFinder < UnionFinder
|
|
|
|
attr_accessor :current_user, :params
|
|
|
|
|
|
|
|
def initialize(current_user, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
items = init_collection
|
|
|
|
items = by_project(items)
|
|
|
|
items = by_author(items)
|
|
|
|
items = by_visibility(items)
|
|
|
|
|
|
|
|
items.fresh
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
def init_collection
|
|
|
|
items = Snippet.all
|
|
|
|
|
|
|
|
accessible(items)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
def accessible(items)
|
|
|
|
segments = []
|
|
|
|
segments << items.public_to_user(current_user)
|
|
|
|
segments << authorized_to_user(items) if current_user
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
find_union(segments, Snippet)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
def authorized_to_user(items)
|
|
|
|
items.where(
|
|
|
|
'author_id = :author_id
|
|
|
|
OR project_id IN (:project_ids)',
|
|
|
|
author_id: current_user.id,
|
|
|
|
project_ids: current_user.authorized_projects.select(:id))
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
def by_visibility(items)
|
|
|
|
visibility = params[:visibility] || visibility_from_scope
|
|
|
|
|
|
|
|
return items unless visibility
|
|
|
|
|
|
|
|
items.where(visibility_level: visibility)
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_author(items)
|
|
|
|
return items unless params[:author]
|
|
|
|
|
|
|
|
items.where(author_id: params[:author].id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_project(items)
|
|
|
|
return items unless params[:project]
|
|
|
|
|
|
|
|
items.where(project_id: params[:project].id)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
2016-12-09 15:40:48 -05:00
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
def visibility_from_scope
|
|
|
|
case params[:scope].to_s
|
2016-12-09 15:40:48 -05:00
|
|
|
when 'are_private'
|
2017-04-28 18:06:27 -04:00
|
|
|
Snippet::PRIVATE
|
2016-12-09 15:40:48 -05:00
|
|
|
when 'are_internal'
|
2017-04-28 18:06:27 -04:00
|
|
|
Snippet::INTERNAL
|
2016-12-09 15:40:48 -05:00
|
|
|
when 'are_public'
|
2017-04-28 18:06:27 -04:00
|
|
|
Snippet::PUBLIC
|
2016-12-09 15:40:48 -05:00
|
|
|
else
|
2017-04-28 18:06:27 -04:00
|
|
|
nil
|
2016-12-09 15:40:48 -05:00
|
|
|
end
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|