Read about our upcoming Code of Conduct on this issue

snippet.rb 1.52 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
class Snippet < ActiveRecord::Base
2
  include Linguist::BlobHelper
gitlabhq's avatar
gitlabhq committed
3

4
5
  attr_accessible :title, :content, :file_name, :expires_at

gitlabhq's avatar
gitlabhq committed
6
  belongs_to :project
7
8
  belongs_to :author, class_name: "User"
  has_many :notes, as: :noteable, dependent: :destroy
gitlabhq's avatar
gitlabhq committed
9

Nihad Abbasov's avatar
Nihad Abbasov committed
10
  delegate :name, :email, to: :author, prefix: true
gitlabhq's avatar
gitlabhq committed
11

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
12
13
  validates :author, presence: true
  validates :project, presence: true
Nihad Abbasov's avatar
Nihad Abbasov committed
14
15
16
  validates :title, presence: true, length: { within: 0..255 }
  validates :file_name, presence: true, length: { within: 0..255 }
  validates :content, presence: true, length: { within: 0..10000 }
gitlabhq's avatar
gitlabhq committed
17

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
18
  # Scopes
Nihad Abbasov's avatar
Nihad Abbasov committed
19
  scope :fresh, order("created_at DESC")
Nihad Abbasov's avatar
Nihad Abbasov committed
20
  scope :non_expired, where(["expires_at IS NULL OR expires_at > ?", Time.current])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
21
  scope :expired, where(["expires_at IS NOT NULL AND expires_at < ?", Time.current])
Nihad Abbasov's avatar
Nihad Abbasov committed
22

gitlabhq's avatar
gitlabhq committed
23
  def self.content_types
Nihad Abbasov's avatar
Nihad Abbasov committed
24
    [
gitlabhq's avatar
gitlabhq committed
25
26
27
28
29
      ".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
30

31
32
33
34
35
36
37
38
  def data
    content
  end

  def size
    0
  end

39
  def name
40
41
42
    file_name
  end

43
  def mode
44
    nil
gitlabhq's avatar
gitlabhq committed
45
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
46
47
48
49

  def expired?
    expires_at && expires_at < Time.current
  end
gitlabhq's avatar
gitlabhq committed
50
end
Nihad Abbasov's avatar
Nihad Abbasov committed
51

gitlabhq's avatar
gitlabhq committed
52
53
54
55
# == Schema Information
#
# Table name: snippets
#
Nihad Abbasov's avatar
Nihad Abbasov committed
56
#  id         :integer         not null, primary key
gitlabhq's avatar
gitlabhq committed
57
58
#  title      :string(255)
#  content    :text
Nihad Abbasov's avatar
Nihad Abbasov committed
59
60
#  author_id  :integer         not null
#  project_id :integer         not null
randx's avatar
randx committed
61
62
#  created_at :datetime        not null
#  updated_at :datetime        not null
gitlabhq's avatar
gitlabhq committed
63
#  file_name  :string(255)
Nihad Abbasov's avatar
Nihad Abbasov committed
64
#  expires_at :datetime
gitlabhq's avatar
gitlabhq committed
65
#