Commit adbc3780 authored by Mike Greiling's avatar Mike Greiling

refactor duplicate code into a by_scope method

parent eaf92daa
...@@ -29,21 +29,11 @@ class SnippetsFinder ...@@ -29,21 +29,11 @@ class SnippetsFinder
def by_user(current_user, user, scope) def by_user(current_user, user, scope)
snippets = user.snippets.fresh snippets = user.snippets.fresh
return snippets.are_public unless current_user if current_user
include_private = user == current_user
if user == current_user by_scope(snippets, scope, include_private)
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 else
snippets.public_and_internal snippets.are_public
end end
end end
...@@ -51,29 +41,23 @@ class SnippetsFinder ...@@ -51,29 +41,23 @@ class SnippetsFinder
snippets = project.snippets.fresh snippets = project.snippets.fresh
if current_user if current_user
if project.team.member?(current_user) || current_user.admin? include_private = project.team.member?(current_user) || current_user.admin?
case scope by_scope(snippets, scope, include_private)
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
else else
snippets.are_public snippets.are_public
end end
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 end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment