BigW Consortium Gitlab

snippet.rb 3.24 KB
Newer Older
1 2 3 4
# == Schema Information
#
# Table name: snippets
#
Valery Sizov committed
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
Dmitriy Zaporozhets committed
16
#
17

gitlabhq committed
18
class Snippet < ActiveRecord::Base
19
  include Gitlab::VisibilityLevel
20
  include Linguist::BlobHelper
21
  include Participable
22 23
  include Referable
  include Sortable
gitlabhq committed
24

25
  default_value_for :visibility_level, Snippet::PRIVATE
26

27 28
  belongs_to :author, class_name: 'User'
  belongs_to :project
Andrew8xx8 committed
29

30
  has_many :notes, as: :noteable, dependent: :destroy
gitlabhq committed
31

32
  delegate :name, :email, to: :author, prefix: true, allow_nil: true
gitlabhq committed
33

Andrey Kumanyaev committed
34
  validates :author, presence: true
35
  validates :title, presence: true, length: { within: 0..255 }
36 37
  validates :file_name,
    length: { within: 0..255 },
38 39
    format: { with: Gitlab::Regex.file_name_regex,
              message: Gitlab::Regex.file_name_regex_message }
40
  validates :content, presence: true
41
  validates :visibility_level, inclusion: { in: Gitlab::VisibilityLevel.values }
gitlabhq committed
42

Andrey Kumanyaev committed
43
  # Scopes
44 45 46 47
  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 committed
48
  scope :fresh,   -> { order("created_at DESC") }
49
  scope :expired, -> { where(["expires_at IS NOT NULL AND expires_at < ?", Time.current]) }
Andrew8xx8 committed
50
  scope :non_expired, -> { where(["expires_at IS NULL OR expires_at > ?", Time.current]) }
51

52 53
  participant :author, :notes

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

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

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

86 87 88 89
  def data
    content
  end

90 91 92 93
  def hook_attrs
    attributes
  end

94 95 96 97
  def size
    0
  end

98
  def name
99 100 101
    file_name
  end

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

106
  def mode
107
    nil
gitlabhq committed
108
  end
109 110 111 112

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

114 115
  def visibility_level_field
    visibility_level
116
  end
117

118 119 120 121 122 123 124 125 126 127
  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)
128
      where('visibility_level IN (?) OR author_id = ?', [Snippet::INTERNAL, Snippet::PUBLIC], user)
129 130
    end
  end
gitlabhq committed
131
end