Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
635e2e5d
Commit
635e2e5d
authored
Nov 25, 2021
by
João Cunha
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Standardize snippet class methods location
parent
a8802520
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
81 deletions
+81
-81
app/models/snippet.rb
app/models/snippet.rb
+81
-81
No files found.
app/models/snippet.rb
View file @
635e2e5d
...
...
@@ -98,87 +98,108 @@ class Snippet < ApplicationRecord
mode: :per_attribute_iv
,
algorithm:
'aes-256-cbc'
def
self
.
with_optional_visibility
(
value
=
nil
)
if
value
where
(
visibility_level:
value
)
else
all
class
<<
self
# Searches for snippets with a matching title, description or file name.
#
# This method uses ILIKE on PostgreSQL.
#
# query - The search query as a String.
#
# Returns an ActiveRecord::Relation.
def
search
(
query
)
fuzzy_search
(
query
,
[
:title
,
:description
,
:file_name
])
end
end
def
self
.
only_personal_snippet
s
where
(
project_id:
nil
)
end
def
parent_clas
s
::
Project
end
def
self
.
only_project_snippets
where
.
not
(
project_id:
nil
)
end
def
sanitized_file_name
(
file_name
)
file_name
.
gsub
(
/[^a-zA-Z0-9_\-\.]+/
,
''
)
end
def
self
.
only_include_projects_visible_to
(
current_user
=
nil
)
levels
=
Gitlab
::
VisibilityLevel
.
levels_for_user
(
current_user
)
def
with_optional_visibility
(
value
=
nil
)
if
value
where
(
visibility_level:
value
)
else
all
end
end
joins
(
:project
).
where
(
projects:
{
visibility_level:
levels
})
end
def
only_personal_snippets
where
(
project_id:
nil
)
end
def
self
.
only_include_projects_with_snippets_enabled
(
include_private:
false
)
column
=
ProjectFeature
.
access_level_attribute
(
:snippets
)
levels
=
[
ProjectFeature
::
ENABLED
,
ProjectFeature
::
PUBLIC
]
def
only_project_snippets
where
.
not
(
project_id:
nil
)
end
levels
<<
ProjectFeature
::
PRIVATE
if
include_private
def
only_include_projects_visible_to
(
current_user
=
nil
)
levels
=
Gitlab
::
VisibilityLevel
.
levels_for_user
(
current_user
)
joins
(
project: :project_feature
)
.
where
(
project_features:
{
column
=>
levels
})
end
joins
(
:project
).
where
(
projects:
{
visibility_level:
levels
})
end
def
self
.
only_include_authorized_projects
(
current_user
)
where
(
'EXISTS (?)'
,
ProjectAuthorization
.
select
(
1
)
.
where
(
'project_id = snippets.project_id'
)
.
where
(
user_id:
current_user
.
id
)
)
end
def
only_include_projects_with_snippets_enabled
(
include_private:
false
)
column
=
ProjectFeature
.
access_level_attribute
(
:snippets
)
levels
=
[
ProjectFeature
::
ENABLED
,
ProjectFeature
::
PUBLIC
]
def
self
.
for_project_with_user
(
project
,
user
=
nil
)
return
none
unless
project
.
snippets_visible?
(
user
)
levels
<<
ProjectFeature
::
PRIVATE
if
include_private
if
user
&&
project
.
team
.
member?
(
user
)
project
.
snippets
else
project
.
snippets
.
public_to_user
(
user
)
joins
(
project: :project_feature
)
.
where
(
project_features:
{
column
=>
levels
})
end
end
def
self
.
visible_to_or_authored_by
(
user
)
query
=
where
(
visibility_level:
Gitlab
::
VisibilityLevel
.
levels_for_user
(
user
))
query
.
or
(
where
(
author_id:
user
.
id
))
end
def
only_include_authorized_projects
(
current_user
)
where
(
'EXISTS (?)'
,
ProjectAuthorization
.
select
(
1
)
.
where
(
'project_id = snippets.project_id'
)
.
where
(
user_id:
current_user
.
id
)
)
end
def
self
.
reference_prefix
'$'
end
def
for_project_with_user
(
project
,
user
=
nil
)
return
none
unless
project
.
snippets_visible?
(
user
)
if
user
&&
project
.
team
.
member?
(
user
)
project
.
snippets
else
project
.
snippets
.
public_to_user
(
user
)
end
end
def
visible_to_or_authored_by
(
user
)
query
=
where
(
visibility_level:
Gitlab
::
VisibilityLevel
.
levels_for_user
(
user
))
query
.
or
(
where
(
author_id:
user
.
id
))
end
def
reference_prefix
'$'
end
# Pattern used to extract `$123` snippet references from text
#
# This pattern supports cross-project references.
def
self
.
reference_pattern
@reference_pattern
||=
%r{
# Pattern used to extract `$123` snippet references from text
#
# This pattern supports cross-project references.
def
reference_pattern
@reference_pattern
||=
%r{
(
#{
Project
.
reference_pattern
}
)?
#{
Regexp
.
escape
(
reference_prefix
)
}
(?<snippet>
\d
+)
}x
end
end
def
self
.
link_reference_pattern
@link_reference_pattern
||=
super
(
"snippets"
,
/(?<snippet>\d+)/
)
end
def
link_reference_pattern
@link_reference_pattern
||=
super
(
"snippets"
,
/(?<snippet>\d+)/
)
end
def
self
.
find_by_id_and_project
(
id
:,
project
:)
Snippet
.
find_by
(
id:
id
,
project:
project
)
end
def
find_by_id_and_project
(
id
:,
project
:)
Snippet
.
find_by
(
id:
id
,
project:
project
)
end
def
self
.
max_file_limit
MAX_FILE_COUNT
def
max_file_limit
MAX_FILE_COUNT
end
end
def
initialize
(
attributes
=
{})
...
...
@@ -230,10 +251,6 @@ class Snippet < ApplicationRecord
super
.
to_s
end
def
self
.
sanitized_file_name
(
file_name
)
file_name
.
gsub
(
/[^a-zA-Z0-9_\-\.]+/
,
''
)
end
def
visibility_level_field
:visibility_level
end
...
...
@@ -371,23 +388,6 @@ class Snippet < ApplicationRecord
def
multiple_files?
list_files
.
size
>
1
end
class
<<
self
# Searches for snippets with a matching title, description or file name.
#
# This method uses ILIKE on PostgreSQL.
#
# query - The search query as a String.
#
# Returns an ActiveRecord::Relation.
def
search
(
query
)
fuzzy_search
(
query
,
[
:title
,
:description
,
:file_name
])
end
def
parent_class
::
Project
end
end
end
Snippet
.
prepend_mod_with
(
'Snippet'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment