snippet.rb 1.5 KB
Newer Older
G
gitlabhq 已提交
1
class Snippet < ActiveRecord::Base
2
  include Linguist::BlobHelper
G
gitlabhq 已提交
3

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

G
gitlabhq 已提交
6
  belongs_to :project
7 8
  belongs_to :author, class_name: "User"
  has_many :notes, as: :noteable, dependent: :destroy
G
gitlabhq 已提交
9

N
Nihad Abbasov 已提交
10
  delegate :name, :email, to: :author, prefix: true
G
gitlabhq 已提交
11

N
Nihad Abbasov 已提交
12 13 14 15
  validates_presence_of :author_id, :project_id
  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 }
G
gitlabhq 已提交
16

N
Nihad Abbasov 已提交
17
  scope :fresh, order("created_at DESC")
N
Nihad Abbasov 已提交
18
  scope :non_expired, where(["expires_at IS NULL OR expires_at > ?", Time.current])
D
Dmitriy Zaporozhets 已提交
19
  scope :expired, where(["expires_at IS NOT NULL AND expires_at < ?", Time.current])
N
Nihad Abbasov 已提交
20

G
gitlabhq 已提交
21
  def self.content_types
N
Nihad Abbasov 已提交
22
    [
G
gitlabhq 已提交
23 24 25 26 27
      ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java",
      ".haml", ".html", ".sass", ".scss", ".xml", ".php", ".erb",
      ".js", ".sh", ".coffee", ".yml", ".md"
    ]
  end
G
gitlabhq 已提交
28

29 30 31 32 33 34 35 36
  def data
    content
  end

  def size
    0
  end

37
  def name
38 39 40
    file_name
  end

41
  def mode
42
    nil
G
gitlabhq 已提交
43
  end
N
Nihad Abbasov 已提交
44 45 46 47

  def expired?
    expires_at && expires_at < Time.current
  end
G
gitlabhq 已提交
48
end
N
Nihad Abbasov 已提交
49

G
gitlabhq 已提交
50 51 52 53
# == Schema Information
#
# Table name: snippets
#
N
Nihad Abbasov 已提交
54
#  id         :integer         not null, primary key
G
gitlabhq 已提交
55 56
#  title      :string(255)
#  content    :text
N
Nihad Abbasov 已提交
57 58
#  author_id  :integer         not null
#  project_id :integer         not null
R
randx 已提交
59 60
#  created_at :datetime        not null
#  updated_at :datetime        not null
G
gitlabhq 已提交
61
#  file_name  :string(255)
N
Nihad Abbasov 已提交
62
#  expires_at :datetime
G
gitlabhq 已提交
63
#