Read about our upcoming Code of Conduct on this issue

snippet.rb 4.06 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
gitlabhq's avatar
gitlabhq committed
9

10
11
12
13
14
15
16
17
18
  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

19
  default_value_for :visibility_level, Snippet::PRIVATE
20

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

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

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

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

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

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
38
  # Scopes
39
40
41
42
  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
43
  scope :fresh,   -> { order("created_at DESC") }
Nihad Abbasov's avatar
Nihad Abbasov committed
44

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

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

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

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

66
67
68
69
70
71
72
73
74
75
  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

gitlabhq's avatar
gitlabhq committed
76
  def self.content_types
Nihad Abbasov's avatar
Nihad Abbasov committed
77
    [
gitlabhq's avatar
gitlabhq committed
78
79
80
81
82
      ".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
83

84
85
86
87
  def data
    content
  end

88
89
90
91
  def hook_attrs
    attributes
  end

92
93
94
95
  def size
    0
  end

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

101
  def name
102
103
104
    file_name
  end

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

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

113
114
  def visibility_level_field
    visibility_level
115
  end
116

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

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

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

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

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

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

    def accessible_to(user)
155
156
157
158
159
160
161
162
163
164
      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))
165
166
    end
  end
gitlabhq's avatar
gitlabhq committed
167
end