Class: SnippetsFinder

Inherits:
UnionFinder show all
Includes:
CreatedAtFilter, FinderMethods, Gitlab::Allowable, Gitlab::Utils::StrongMemoize
Defined in:
app/finders/snippets_finder.rb

Overview

Finder for retrieving snippets that a user can see, optionally scoped to a project or snippets author.

Basic usage:

user = User.find(1)

SnippetsFinder.new(user).execute

To scope snippets to a specific organization:

user = User.find(1)
organization = Organization.find(1)

SnippetsFinder.new(user, organization_id: organization.id).execute

To limit the snippets to a specific project, supply the project: option:

user = User.find(1)
project = Project.find(1)
organization_id = project.organization_id # Every project belongs to an organization

SnippetsFinder.new(user, project: project, organization_id: organization_id).execute

Limiting snippets to an author can be done by supplying the author: option:

user = User.find(1)
project = Project.find(1)
organization_id = project.organization_id # Every project belongs to an organization

SnippetsFinder.new(user, author: user, organization_id: organization_id).execute

To filter snippets using a specific visibility level, you can provide the scope: option:

user = User.find(1)
project = Project.find(1)
organization_id = project.organization_id # Every project belongs to an organization

SnippetsFinder.new(user, author: user, scope: :are_public, organization_id: organization_id).execute

Valid scope: values are:

  • :are_private

  • :are_internal

  • :are_public

Any other value will be ignored.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

Methods included from CreatedAtFilter

#by_created_at

Methods included from FinderMethods

#find, #find_by, #find_by!

Methods inherited from UnionFinder

#find_union

Constructor Details

#initialize(current_user = nil, params = {}) ⇒ SnippetsFinder

Returns a new instance of SnippetsFinder.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/finders/snippets_finder.rb', line 59

def initialize(current_user = nil, params = {})
  @current_user = current_user
  @params = params
  @organization_id = params.delete(:organization_id)

  if project && author
    raise(
      ArgumentError,
      'Filtering by both an author and a project is not supported, ' \
        'as this finder is not optimised for this use case'
    )
  end
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



57
58
59
# File 'app/finders/snippets_finder.rb', line 57

def current_user
  @current_user
end

#organization_idObject (readonly)

Returns the value of attribute organization_id.



57
58
59
# File 'app/finders/snippets_finder.rb', line 57

def organization_id
  @organization_id
end

#paramsObject (readonly)

Returns the value of attribute params.



57
58
59
# File 'app/finders/snippets_finder.rb', line 57

def params
  @params
end

Instance Method Details

#executeObject



73
74
75
76
77
78
79
80
81
82
83
# File 'app/finders/snippets_finder.rb', line 73

def execute
  # The snippet query can be expensive, therefore if the
  # author or project params have been passed and they don't
  # exist, or if a Project has been passed and has snippets
  # disabled, it's better to return
  return Snippet.none if author.nil? && params[:author].present?
  return Snippet.none if project.nil? && params[:project].present?
  return Snippet.none if project && !project.feature_available?(:snippets, current_user)

  filter_snippets.order_by(sort_param)
end