2017-08-17 22:00:37 +05:30
|
|
|
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
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def init_collection
|
|
|
|
items = Snippet.all
|
|
|
|
|
|
|
|
accessible(items)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def accessible(items)
|
|
|
|
segments = []
|
|
|
|
segments << items.public_to_user(current_user)
|
|
|
|
segments << authorized_to_user(items) if current_user
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
find_union(segments, Snippet)
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
def by_visibility(items)
|
|
|
|
visibility = params[:visibility] || visibility_from_scope
|
|
|
|
|
|
|
|
return items unless visibility
|
|
|
|
|
|
|
|
items.where(visibility_level: visibility)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
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)
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def visibility_from_scope
|
|
|
|
case params[:scope].to_s
|
|
|
|
when 'are_private'
|
|
|
|
Snippet::PRIVATE
|
|
|
|
when 'are_internal'
|
|
|
|
Snippet::INTERNAL
|
|
|
|
when 'are_public'
|
|
|
|
Snippet::PUBLIC
|
2015-04-26 12:48:37 +05:30
|
|
|
else
|
2017-08-17 22:00:37 +05:30
|
|
|
nil
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|