snippet.rb 4.1 KB
Newer Older
G
gitlabhq 已提交
1
class Snippet < ActiveRecord::Base
V
Valery Sizov 已提交
2
  include Gitlab::VisibilityLevel
3
  include Linguist::BlobHelper
4
  include CacheMarkdownField
5
  include Participable
6 7
  include Referable
  include Sortable
8
  include Awardable
G
gitlabhq 已提交
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

V
Valery Sizov 已提交
19
  default_value_for :visibility_level, Snippet::PRIVATE
20

R
Robert Speicher 已提交
21 22
  belongs_to :author, class_name: 'User'
  belongs_to :project
A
Andrew8xx8 已提交
23

24
  has_many :notes, as: :noteable, dependent: :destroy
G
gitlabhq 已提交
25

D
Dmitriy Zaporozhets 已提交
26
  delegate :name, :email, to: :author, prefix: true, allow_nil: true
G
gitlabhq 已提交
27

A
Andrey Kumanyaev 已提交
28
  validates :author, presence: true
N
Nihad Abbasov 已提交
29
  validates :title, presence: true, length: { within: 0..255 }
30 31
  validates :file_name,
    length: { within: 0..255 },
D
Douwe Maan 已提交
32 33
    format: { with: Gitlab::Regex.file_name_regex,
              message: Gitlab::Regex.file_name_regex_message }
34

V
Valeriy Sizov 已提交
35
  validates :content, presence: true
V
Valery Sizov 已提交
36
  validates :visibility_level, inclusion: { in: Gitlab::VisibilityLevel.values }
G
gitlabhq 已提交
37

A
Andrey Kumanyaev 已提交
38
  # Scopes
V
Valery Sizov 已提交
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]) }
A
Andrew8xx8 已提交
43
  scope :fresh,   -> { order("created_at DESC") }
N
Nihad Abbasov 已提交
44

Y
Yorick Peterse 已提交
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

G
gitlabhq 已提交
76
  def self.content_types
N
Nihad Abbasov 已提交
77
    [
G
gitlabhq 已提交
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
G
gitlabhq 已提交
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
G
gitlabhq 已提交
111
  end
N
Nihad Abbasov 已提交
112

V
Valery Sizov 已提交
113 114
  def visibility_level_field
    visibility_level
115
  end
V
Valery Sizov 已提交
116

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

Y
Yorick Peterse 已提交
121
  def notes_with_associations
122
    notes.includes(:author)
Y
Yorick Peterse 已提交
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
G
gitlabhq 已提交
167
end