snippet.rb 2.5 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
19
  include Linguist::BlobHelper
V
Valery Sizov 已提交
20
  include Gitlab::VisibilityLevel
G
gitlabhq 已提交
21

V
Valery Sizov 已提交
22
  default_value_for :visibility_level, Snippet::PRIVATE
23

24
  belongs_to :author, class_name: "User"
A
Andrew8xx8 已提交
25

26
  has_many :notes, as: :noteable, dependent: :destroy
G
gitlabhq 已提交
27

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

A
Andrey Kumanyaev 已提交
30
  validates :author, presence: true
N
Nihad Abbasov 已提交
31
  validates :title, presence: true, length: { within: 0..255 }
32 33 34
  validates :file_name, presence: true, length: { within: 0..255 },
            format: { with: Gitlab::Regex.path_regex,
                      message: Gitlab::Regex.path_regex_message }
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") }
A
Andrew8xx8 已提交
44
  scope :expired, -> { where(["expires_at IS NOT NULL AND expires_at < ?", Time.current]) }
A
Andrew8xx8 已提交
45
  scope :non_expired, -> { where(["expires_at IS NULL OR expires_at > ?", Time.current]) }
N
Nihad Abbasov 已提交
46

G
gitlabhq 已提交
47
  def self.content_types
N
Nihad Abbasov 已提交
48
    [
G
gitlabhq 已提交
49 50 51 52 53
      ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java",
      ".haml", ".html", ".sass", ".scss", ".xml", ".php", ".erb",
      ".js", ".sh", ".coffee", ".yml", ".md"
    ]
  end
G
gitlabhq 已提交
54

55 56 57 58 59 60 61 62
  def data
    content
  end

  def size
    0
  end

63
  def name
64 65 66
    file_name
  end

67
  def mode
68
    nil
G
gitlabhq 已提交
69
  end
N
Nihad Abbasov 已提交
70 71 72 73

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

V
Valery Sizov 已提交
75 76
  def visibility_level_field
    visibility_level
77
  end
V
Valery Sizov 已提交
78

79 80 81 82 83 84 85 86 87 88
  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 已提交
89
      where('visibility_level IN (?) OR author_id = ?', [Snippet::INTERNAL, Snippet::PUBLIC], user)
90 91
    end
  end
G
gitlabhq 已提交
92
end