summaryrefslogtreecommitdiff
path: root/app/finders/snippets_finder.rb
diff options
context:
space:
mode:
authorMike Greiling <mike@pixelcog.com>2016-12-09 14:40:48 -0600
committerMike Greiling <mike@pixelcog.com>2016-12-10 00:57:23 -0600
commitadbc37804e49e1d3ba02bf61122696e135666ff3 (patch)
tree7ba7076088f3b3ff5fe48e63a8b0e44752f11004 /app/finders/snippets_finder.rb
parenteaf92daa2ce2601426c991c794aab57c4d0da420 (diff)
downloadgitlab-ce-adbc37804e49e1d3ba02bf61122696e135666ff3.tar.gz
refactor duplicate code into a by_scope method
Diffstat (limited to 'app/finders/snippets_finder.rb')
-rw-r--r--app/finders/snippets_finder.rb54
1 files changed, 19 insertions, 35 deletions
diff --git a/app/finders/snippets_finder.rb b/app/finders/snippets_finder.rb
index 99f1e73c800..31f039b5a70 100644
--- a/app/finders/snippets_finder.rb
+++ b/app/finders/snippets_finder.rb
@@ -29,21 +29,11 @@ class SnippetsFinder
def by_user(current_user, user, scope)
snippets = user.snippets.fresh
- return snippets.are_public unless current_user
-
- if user == current_user
- case scope
- when 'are_internal' then
- snippets.are_internal
- when 'are_private' then
- snippets.are_private
- when 'are_public' then
- snippets.are_public
- else
- snippets
- end
+ if current_user
+ include_private = user == current_user
+ by_scope(snippets, scope, include_private)
else
- snippets.public_and_internal
+ snippets.are_public
end
end
@@ -51,29 +41,23 @@ class SnippetsFinder
snippets = project.snippets.fresh
if current_user
- if project.team.member?(current_user) || current_user.admin?
- case scope
- when 'are_internal' then
- snippets.are_internal
- when 'are_private' then
- snippets.are_private
- when 'are_public' then
- snippets.are_public
- else
- snippets
- end
- else
- case scope
- when 'are_internal' then
- snippets.are_internal
- when 'are_public' then
- snippets.are_public
- else
- snippets.public_and_internal
- end
- end
+ include_private = project.team.member?(current_user) || current_user.admin?
+ by_scope(snippets, scope, include_private)
else
snippets.are_public
end
end
+
+ def by_scope(snippets, scope = nil, include_private = false)
+ case scope.to_s
+ when 'are_private'
+ include_private ? snippets.are_private : nil
+ when 'are_internal'
+ snippets.are_internal
+ when 'are_public'
+ snippets.are_public
+ else
+ include_private ? snippets : snippets.public_and_internal
+ end
+ end
end