Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.1 on 2022-01-26 at 16:00 UTC+1 (a few minutes of down time)

snippet.rb 4.08 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
class Snippet < ActiveRecord::Base
2
  include Gitlab::VisibilityLevel
3
  include Linguist::BlobHelper
4
  include CacheMarkdownField
5
  include Participable
6
7
  include Referable
  include Sortable
8
  include Awardable
9
  include Mentionable
gitlabhq's avatar
gitlabhq committed
10

11
12
13
14
15
16
17
18
19
  cache_markdown_field :title, pipeline: :single_line
  cache_markdown_field :content

  # If file_name changes, it invalidates content
  alias_method :default_content_html_invalidator, :content_html_invalidated?
  def content_html_invalidated?
    default_content_html_invalidator || file_name_changed?
  end

20
  default_value_for :visibility_level, Snippet::PRIVATE
21

Robert Speicher's avatar
Robert Speicher committed
22
23
  belongs_to :author, class_name: 'User'
  belongs_to :project
Andrew8xx8's avatar
Andrew8xx8 committed
24

25
  has_many :notes, as: :noteable, dependent: :destroy
gitlabhq's avatar
gitlabhq committed
26

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
27
  delegate :name, :email, to: :author, prefix: true, allow_nil: true
gitlabhq's avatar
gitlabhq committed
28

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
29
  validates :author, presence: true
Nihad Abbasov's avatar
Nihad Abbasov committed
30
  validates :title, presence: true, length: { within: 0..255 }
31
32
  validates :file_name,
    length: { within: 0..255 },
Douwe Maan's avatar
Douwe Maan committed
33
34
    format: { with: Gitlab::Regex.file_name_regex,
              message: Gitlab::Regex.file_name_regex_message }
35

36
  validates :content, presence: true
37
  validates :visibility_level, inclusion: { in: Gitlab::VisibilityLevel.values }
gitlabhq's avatar
gitlabhq committed
38

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
39
  # Scopes
40
41
42
43
  scope :are_internal,  -> { where(visibility_level: Snippet::INTERNAL) }
  scope :are_private, -> { where(visibility_level: Snippet::PRIVATE) }
  scope :are_public, -> { where(visibility_level: Snippet::PUBLIC) }
  scope :public_and_internal, -> { where(visibility_level: [Snippet::PUBLIC, Snippet::INTERNAL]) }
Andrew8xx8's avatar
Andrew8xx8 committed
44
  scope :fresh,   -> { order("created_at DESC") }
Nihad Abbasov's avatar
Nihad Abbasov committed
45

Yorick Peterse's avatar
Yorick Peterse committed
46
47
  participant :author
  participant :notes_with_associations
48

49
50
51
52
  def self.reference_prefix
    '$'
  end

53
54
55
56
  # Pattern used to extract `$123` snippet references from text
  #
  # This pattern supports cross-project references.
  def self.reference_pattern
57
    @reference_pattern ||= %r{
58
59
      (#{Project.reference_pattern})?
      #{Regexp.escape(reference_prefix)}(?<snippet>\d+)
60
61
62
    }x
  end

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

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

70
71
72
73
    if project.present?
      "#{project.to_reference(from_project)}#{reference}"
    else
      reference
74
75
76
    end
  end

gitlabhq's avatar
gitlabhq committed
77
  def self.content_types
Nihad Abbasov's avatar
Nihad Abbasov committed
78
    [
gitlabhq's avatar
gitlabhq committed
79
80
81
82
83
      ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java",
      ".haml", ".html", ".sass", ".scss", ".xml", ".php", ".erb",
      ".js", ".sh", ".coffee", ".yml", ".md"
    ]
  end
gitlabhq's avatar
gitlabhq committed
84

85
86
87
88
  def data
    content
  end

89
90
91
92
  def hook_attrs
    attributes
  end

93
94
95
96
  def size
    0
  end

97
98
99
100
101
  # alias for compatibility with blobs and highlighting
  def path
    file_name
  end

102
  def name
103
104
105
    file_name
  end

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

110
  def mode
111
    nil
gitlabhq's avatar
gitlabhq committed
112
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
113

114
115
  def visibility_level_field
    visibility_level
116
  end
117

118
119
120
121
  def no_highlighting?
    content.lines.count > 1000
  end

Yorick Peterse's avatar
Yorick Peterse committed
122
  def notes_with_associations
123
    notes.includes(:author)
Yorick Peterse's avatar
Yorick Peterse committed
124
125
  end

126
  class << self
127
128
129
130
131
132
133
    # 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.
134
    def search(query)
135
      t = arel_table
136
137
138
      pattern = "%#{query}%"

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

141
142
143
144
145
146
147
    # 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.
148
    def search_code(query)
149
150
151
152
      table   = Snippet.arel_table
      pattern = "%#{query}%"

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

    def accessible_to(user)
156
157
158
159
160
161
162
163
164
165
      return are_public unless user.present?
      return all if user.admin?

      where(
        'visibility_level IN (:visibility_levels)
         OR author_id = :author_id
         OR project_id IN (:project_ids)',
         visibility_levels: [Snippet::PUBLIC, Snippet::INTERNAL],
         author_id: user.id,
         project_ids: user.authorized_projects.select(:id))
166
167
    end
  end
gitlabhq's avatar
gitlabhq committed
168
end