Class: Snippet

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Gitlab::VisibilityLevel, Linguist::BlobHelper, Participable, Referable, Sortable
Defined in:
app/models/snippet.rb

Overview

Schema Information

Table name: snippets

id               :integer          not null, primary key
title            :string
content          :text
author_id        :integer          not null
project_id       :integer
created_at       :datetime
updated_at       :datetime
file_name        :string
type             :string
visibility_level :integer          default(0), not null

Direct Known Subclasses

PersonalSnippet, ProjectSnippet

Constant Summary

Constants included from Gitlab::VisibilityLevel

Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PRIVATE, Gitlab::VisibilityLevel::PUBLIC

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Referable

#reference_link_text

Methods included from Participable

#participants

Methods included from Gitlab::VisibilityLevel

allowed_for?, allowed_level?, #internal?, level_name, non_restricted_level?, options, #private?, #public?, valid_level?, values

Methods included from Gitlab::CurrentSettings

#current_application_settings, #fake_application_settings

Class Method Details

.accessible_to(user) ⇒ Object


148
149
150
# File 'app/models/snippet.rb', line 148

def accessible_to(user)
  where('visibility_level IN (?) OR author_id = ?', [Snippet::INTERNAL, Snippet::PUBLIC], user)
end

.content_typesObject


79
80
81
82
83
84
85
# File 'app/models/snippet.rb', line 79

def self.content_types
  [
    ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java",
    ".haml", ".html", ".sass", ".scss", ".xml", ".php", ".erb",
    ".js", ".sh", ".coffee", ".yml", ".md"
  ]
end

65
66
67
# File 'app/models/snippet.rb', line 65

def self.link_reference_pattern
  @link_reference_pattern ||= super("snippets", /(?<snippet>\d+)/)
end

.reference_patternObject

Pattern used to extract `$123` snippet references from text

This pattern supports cross-project references.


58
59
60
61
62
63
# File 'app/models/snippet.rb', line 58

def self.reference_pattern
  @reference_pattern ||= %r{
    (#{Project.reference_pattern})?
    #{Regexp.escape(reference_prefix)}(?<snippet>\d+)
  }x
end

.reference_prefixObject


51
52
53
# File 'app/models/snippet.rb', line 51

def self.reference_prefix
  '$'
end

.search(query) ⇒ Object

Searches for snippets with a matching title or file name.

This method uses ILIKE on PostgreSQL and LIKE on MySQL.

query - The search query as a String.

Returns an ActiveRecord::Relation.


127
128
129
130
131
132
# File 'app/models/snippet.rb', line 127

def search(query)
  t = arel_table
  pattern = "%#{query}%"

  where(t[:title].matches(pattern).or(t[:file_name].matches(pattern)))
end

.search_code(query) ⇒ Object

Searches for snippets with matching content.

This method uses ILIKE on PostgreSQL and LIKE on MySQL.

query - The search query as a String.

Returns an ActiveRecord::Relation.


141
142
143
144
145
146
# File 'app/models/snippet.rb', line 141

def search_code(query)
  table   = Snippet.arel_table
  pattern = "%#{query}%"

  where(table[:content].matches(pattern))
end

Instance Method Details

#dataObject


87
88
89
# File 'app/models/snippet.rb', line 87

def data
  content
end

#hook_attrsObject


91
92
93
# File 'app/models/snippet.rb', line 91

def hook_attrs
  attributes
end

#modeObject


107
108
109
# File 'app/models/snippet.rb', line 107

def mode
  nil
end

#nameObject


99
100
101
# File 'app/models/snippet.rb', line 99

def name
  file_name
end

#no_highlighting?Boolean

Returns:

  • (Boolean)

115
116
117
# File 'app/models/snippet.rb', line 115

def no_highlighting?
  content.lines.count > 1000
end

#sanitized_file_nameObject


103
104
105
# File 'app/models/snippet.rb', line 103

def sanitized_file_name
  file_name.gsub(/[^a-zA-Z0-9_\-\.]+/, '')
end

#sizeObject


95
96
97
# File 'app/models/snippet.rb', line 95

def size
  0
end

#to_reference(from_project = nil) ⇒ Object


69
70
71
72
73
74
75
76
77
# File 'app/models/snippet.rb', line 69

def to_reference(from_project = nil)
  reference = "#{self.class.reference_prefix}#{id}"

  if cross_project_reference?(from_project)
    reference = project.to_reference + reference
  end

  reference
end

#visibility_level_fieldObject


111
112
113
# File 'app/models/snippet.rb', line 111

def visibility_level_field
  visibility_level
end