2014-10-08 09:44:25 -04:00
|
|
|
class SnippetsFinder
|
|
|
|
def execute(current_user, params = {})
|
|
|
|
filter = params[:filter]
|
2016-11-26 10:37:26 -05:00
|
|
|
user = params.fetch(:user, current_user)
|
2014-10-08 09:44:25 -04:00
|
|
|
|
|
|
|
case filter
|
|
|
|
when :all then
|
2016-03-05 17:17:49 -05:00
|
|
|
snippets(current_user).fresh
|
2016-11-26 10:37:26 -05:00
|
|
|
when :public then
|
|
|
|
Snippet.are_public.fresh
|
2014-10-08 09:44:25 -04:00
|
|
|
when :by_user then
|
2016-11-26 10:37:26 -05:00
|
|
|
by_user(current_user, user, params[:scope])
|
2014-10-08 09:44:25 -04:00
|
|
|
when :by_project
|
2016-12-07 17:35:53 -05:00
|
|
|
by_project(current_user, params[:project], params[:scope])
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def snippets(current_user)
|
|
|
|
if current_user
|
|
|
|
Snippet.public_and_internal
|
|
|
|
else
|
|
|
|
# Not authenticated
|
|
|
|
#
|
|
|
|
# Return only:
|
|
|
|
# public snippets
|
|
|
|
Snippet.are_public
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_user(current_user, user, scope)
|
2016-03-05 17:17:49 -05:00
|
|
|
snippets = user.snippets.fresh
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2016-12-09 15:40:48 -05:00
|
|
|
if current_user
|
|
|
|
include_private = user == current_user
|
|
|
|
by_scope(snippets, scope, include_private)
|
2014-10-08 09:44:25 -04:00
|
|
|
else
|
2016-12-09 15:40:48 -05:00
|
|
|
snippets.are_public
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-07 17:35:53 -05:00
|
|
|
def by_project(current_user, project, scope)
|
2016-03-05 17:17:49 -05:00
|
|
|
snippets = project.snippets.fresh
|
2014-10-08 09:44:25 -04:00
|
|
|
|
|
|
|
if current_user
|
2016-12-09 15:40:48 -05:00
|
|
|
include_private = project.team.member?(current_user) || current_user.admin?
|
|
|
|
by_scope(snippets, scope, include_private)
|
2014-10-08 09:44:25 -04:00
|
|
|
else
|
|
|
|
snippets.are_public
|
|
|
|
end
|
|
|
|
end
|
2016-12-09 15:40:48 -05:00
|
|
|
|
|
|
|
def by_scope(snippets, scope = nil, include_private = false)
|
|
|
|
case scope.to_s
|
|
|
|
when 'are_private'
|
2016-12-09 16:43:02 -05:00
|
|
|
include_private ? snippets.are_private : Snippet.none
|
2016-12-09 15:40:48 -05:00
|
|
|
when 'are_internal'
|
|
|
|
snippets.are_internal
|
|
|
|
when 'are_public'
|
|
|
|
snippets.are_public
|
|
|
|
else
|
|
|
|
include_private ? snippets : snippets.public_and_internal
|
|
|
|
end
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|