Class: SnippetsFinder
- Inherits:
-
UnionFinder
- Object
- UnionFinder
- SnippetsFinder
- 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
-
#current_user ⇒ Object
readonly
Returns the value of attribute current_user.
-
#organization_id ⇒ Object
readonly
Returns the value of attribute organization_id.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(current_user = nil, params = {}) ⇒ SnippetsFinder
constructor
A new instance of SnippetsFinder.
Methods included from Gitlab::Allowable
Methods included from CreatedAtFilter
Methods included from FinderMethods
Methods inherited from UnionFinder
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 && 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_user ⇒ Object (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_id ⇒ Object (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 |
#params ⇒ Object (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
#execute ⇒ Object
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 .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 |