snippet.rb 3.3 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3 4
# == Schema Information
#
# Table name: snippets
#
V
Valery Sizov 已提交
5 6 7 8 9 10 11 12 13 14 15
#  id               :integer          not null, primary key
#  title            :string(255)
#  content          :text
#  author_id        :integer          not null
#  project_id       :integer
#  created_at       :datetime
#  updated_at       :datetime
#  file_name        :string(255)
#  expires_at       :datetime
#  type             :string(255)
#  visibility_level :integer          default(0), not null
D
Dmitriy Zaporozhets 已提交
16
#
D
Dmitriy Zaporozhets 已提交
17

G
gitlabhq 已提交
18
class Snippet < ActiveRecord::Base
V
Valery Sizov 已提交
19
  include Gitlab::VisibilityLevel
20
  include Linguist::BlobHelper
21
  include Participable
22 23
  include Referable
  include Sortable
G
gitlabhq 已提交
24

V
Valery Sizov 已提交
25
  default_value_for :visibility_level, Snippet::PRIVATE
26

R
Robert Speicher 已提交
27 28
  belongs_to :author, class_name: 'User'
  belongs_to :project
A
Andrew8xx8 已提交
29

30
  has_many :notes, as: :noteable, dependent: :destroy
G
gitlabhq 已提交
31

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

A
Andrey Kumanyaev 已提交
34
  validates :author, presence: true
N
Nihad Abbasov 已提交
35
  validates :title, presence: true, length: { within: 0..255 }
36 37 38
  validates :file_name,
    presence: true,
    length: { within: 0..255 },
D
Douwe Maan 已提交
39 40
    format: { with: Gitlab::Regex.file_name_regex,
              message: Gitlab::Regex.file_name_regex_message }
V
Valeriy Sizov 已提交
41
  validates :content, presence: true
V
Valery Sizov 已提交
42
  validates :visibility_level, inclusion: { in: Gitlab::VisibilityLevel.values }
G
gitlabhq 已提交
43

A
Andrey Kumanyaev 已提交
44
  # Scopes
V
Valery Sizov 已提交
45 46 47 48
  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 已提交
49
  scope :fresh,   -> { order("created_at DESC") }
A
Andrew8xx8 已提交
50
  scope :expired, -> { where(["expires_at IS NOT NULL AND expires_at < ?", Time.current]) }
A
Andrew8xx8 已提交
51
  scope :non_expired, -> { where(["expires_at IS NULL OR expires_at > ?", Time.current]) }
N
Nihad Abbasov 已提交
52

53 54
  participant :author, :notes

55 56 57 58
  def self.reference_prefix
    '$'
  end

59 60 61 62 63
  # Pattern used to extract `$123` snippet references from text
  #
  # This pattern supports cross-project references.
  def self.reference_pattern
    %r{
64
      (#{Project.reference_pattern})?
65 66 67 68
      #{Regexp.escape(reference_prefix)}(?<snippet>\d+)
    }x
  end

69 70 71 72 73 74 75 76 77 78
  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 已提交
79
  def self.content_types
N
Nihad Abbasov 已提交
80
    [
G
gitlabhq 已提交
81 82 83 84 85
      ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java",
      ".haml", ".html", ".sass", ".scss", ".xml", ".php", ".erb",
      ".js", ".sh", ".coffee", ".yml", ".md"
    ]
  end
G
gitlabhq 已提交
86

87 88 89 90
  def data
    content
  end

91 92 93 94
  def hook_attrs
    attributes
  end

95 96 97 98
  def size
    0
  end

99
  def name
100 101 102
    file_name
  end

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

107
  def mode
108
    nil
G
gitlabhq 已提交
109
  end
N
Nihad Abbasov 已提交
110 111 112 113

  def expired?
    expires_at && expires_at < Time.current
  end
114

V
Valery Sizov 已提交
115 116
  def visibility_level_field
    visibility_level
117
  end
V
Valery Sizov 已提交
118

119 120 121 122 123 124 125 126 127 128
  class << self
    def search(query)
      where('(title LIKE :query OR file_name LIKE :query)', query: "%#{query}%")
    end

    def search_code(query)
      where('(content LIKE :query)', query: "%#{query}%")
    end

    def accessible_to(user)
V
Valery Sizov 已提交
129
      where('visibility_level IN (?) OR author_id = ?', [Snippet::INTERNAL, Snippet::PUBLIC], user)
130 131
    end
  end
G
gitlabhq 已提交
132
end