Commit ba770c43 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'jcunha/refactor-snippets-class-methods' into 'master'

Standardize snippet class methods location

See merge request gitlab-org/gitlab!75383
parents 18c34a49 635e2e5d
...@@ -98,7 +98,27 @@ class Snippet < ApplicationRecord ...@@ -98,7 +98,27 @@ class Snippet < ApplicationRecord
mode: :per_attribute_iv, mode: :per_attribute_iv,
algorithm: 'aes-256-cbc' algorithm: 'aes-256-cbc'
def self.with_optional_visibility(value = nil) 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
def sanitized_file_name(file_name)
file_name.gsub(/[^a-zA-Z0-9_\-\.]+/, '')
end
def with_optional_visibility(value = nil)
if value if value
where(visibility_level: value) where(visibility_level: value)
else else
...@@ -106,21 +126,21 @@ class Snippet < ApplicationRecord ...@@ -106,21 +126,21 @@ class Snippet < ApplicationRecord
end end
end end
def self.only_personal_snippets def only_personal_snippets
where(project_id: nil) where(project_id: nil)
end end
def self.only_project_snippets def only_project_snippets
where.not(project_id: nil) where.not(project_id: nil)
end end
def self.only_include_projects_visible_to(current_user = nil) def only_include_projects_visible_to(current_user = nil)
levels = Gitlab::VisibilityLevel.levels_for_user(current_user) levels = Gitlab::VisibilityLevel.levels_for_user(current_user)
joins(:project).where(projects: { visibility_level: levels }) joins(:project).where(projects: { visibility_level: levels })
end end
def self.only_include_projects_with_snippets_enabled(include_private: false) def only_include_projects_with_snippets_enabled(include_private: false)
column = ProjectFeature.access_level_attribute(:snippets) column = ProjectFeature.access_level_attribute(:snippets)
levels = [ProjectFeature::ENABLED, ProjectFeature::PUBLIC] levels = [ProjectFeature::ENABLED, ProjectFeature::PUBLIC]
...@@ -130,7 +150,7 @@ class Snippet < ApplicationRecord ...@@ -130,7 +150,7 @@ class Snippet < ApplicationRecord
.where(project_features: { column => levels }) .where(project_features: { column => levels })
end end
def self.only_include_authorized_projects(current_user) def only_include_authorized_projects(current_user)
where( where(
'EXISTS (?)', 'EXISTS (?)',
ProjectAuthorization ProjectAuthorization
...@@ -140,7 +160,7 @@ class Snippet < ApplicationRecord ...@@ -140,7 +160,7 @@ class Snippet < ApplicationRecord
) )
end end
def self.for_project_with_user(project, user = nil) def for_project_with_user(project, user = nil)
return none unless project.snippets_visible?(user) return none unless project.snippets_visible?(user)
if user && project.team.member?(user) if user && project.team.member?(user)
...@@ -150,36 +170,37 @@ class Snippet < ApplicationRecord ...@@ -150,36 +170,37 @@ class Snippet < ApplicationRecord
end end
end end
def self.visible_to_or_authored_by(user) def visible_to_or_authored_by(user)
query = where(visibility_level: Gitlab::VisibilityLevel.levels_for_user(user)) query = where(visibility_level: Gitlab::VisibilityLevel.levels_for_user(user))
query.or(where(author_id: user.id)) query.or(where(author_id: user.id))
end end
def self.reference_prefix def reference_prefix
'$' '$'
end end
# Pattern used to extract `$123` snippet references from text # Pattern used to extract `$123` snippet references from text
# #
# This pattern supports cross-project references. # This pattern supports cross-project references.
def self.reference_pattern def reference_pattern
@reference_pattern ||= %r{ @reference_pattern ||= %r{
(#{Project.reference_pattern})? (#{Project.reference_pattern})?
#{Regexp.escape(reference_prefix)}(?<snippet>\d+) #{Regexp.escape(reference_prefix)}(?<snippet>\d+)
}x }x
end end
def self.link_reference_pattern def link_reference_pattern
@link_reference_pattern ||= super("snippets", /(?<snippet>\d+)/) @link_reference_pattern ||= super("snippets", /(?<snippet>\d+)/)
end end
def self.find_by_id_and_project(id:, project:) def find_by_id_and_project(id:, project:)
Snippet.find_by(id: id, project: project) Snippet.find_by(id: id, project: project)
end end
def self.max_file_limit def max_file_limit
MAX_FILE_COUNT MAX_FILE_COUNT
end end
end
def initialize(attributes = {}) def initialize(attributes = {})
# We can't use default_value_for because the database has a default # We can't use default_value_for because the database has a default
...@@ -230,10 +251,6 @@ class Snippet < ApplicationRecord ...@@ -230,10 +251,6 @@ class Snippet < ApplicationRecord
super.to_s super.to_s
end end
def self.sanitized_file_name(file_name)
file_name.gsub(/[^a-zA-Z0-9_\-\.]+/, '')
end
def visibility_level_field def visibility_level_field
:visibility_level :visibility_level
end end
...@@ -371,23 +388,6 @@ class Snippet < ApplicationRecord ...@@ -371,23 +388,6 @@ class Snippet < ApplicationRecord
def multiple_files? def multiple_files?
list_files.size > 1 list_files.size > 1
end 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 end
Snippet.prepend_mod_with('Snippet') Snippet.prepend_mod_with('Snippet')
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