Read about our upcoming Code of Conduct on this issue

snippet.rb 4.11 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
30
  validates :title, presence: true, length: { maximum: 255 }
31
  validates :file_name,
32
    length: { maximum: 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
70
71
72
73
74
75
76
  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
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
  def file_name
    super.to_s
  end

101
102
103
104
105
  # alias for compatibility with blobs and highlighting
  def path
    file_name
  end

106
  def name
107
108
109
    file_name
  end

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

114
  def mode
115
    nil
gitlabhq's avatar
gitlabhq committed
116
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
117

118
119
  def visibility_level_field
    visibility_level
120
  end
121

122
123
124
125
  def no_highlighting?
    content.lines.count > 1000
  end

Yorick Peterse's avatar
Yorick Peterse committed
126
  def notes_with_associations
127
    notes.includes(:author)
Yorick Peterse's avatar
Yorick Peterse committed
128
129
  end

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

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

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

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

    def accessible_to(user)
160
161
162
163
164
165
166
167
168
169
      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))
170
171
    end
  end
gitlabhq's avatar
gitlabhq committed
172
end