snippet.rb 1.6 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

10 11
  delegate :name,
           :email,
12 13
           to: :author,
           prefix: true
G
gitlabhq 已提交
14 15 16 17 18

  validates_presence_of :project_id
  validates_presence_of :author_id

  validates :title,
19 20
            presence: true,
            length: { within: 0..255 }
N
Nihad Abbasov 已提交
21

G
gitlabhq 已提交
22
  validates :file_name,
23 24
            presence: true,
            length: { within: 0..255 }
G
gitlabhq 已提交
25 26

  validates :content,
27 28
            presence: true,
            length: { within: 0..10000 }
G
gitlabhq 已提交
29

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

G
gitlabhq 已提交
34
  def self.content_types
N
Nihad Abbasov 已提交
35
    [
G
gitlabhq 已提交
36 37 38 39 40
      ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java",
      ".haml", ".html", ".sass", ".scss", ".xml", ".php", ".erb",
      ".js", ".sh", ".coffee", ".yml", ".md"
    ]
  end
G
gitlabhq 已提交
41

42 43 44 45 46 47 48 49
  def data
    content
  end

  def size
    0
  end

50
  def name
51 52 53
    file_name
  end

54
  def mode
55
    nil
G
gitlabhq 已提交
56
  end
N
Nihad Abbasov 已提交
57 58 59 60

  def expired?
    expires_at && expires_at < Time.current
  end
G
gitlabhq 已提交
61
end
G
gitlabhq 已提交
62 63 64 65
# == Schema Information
#
# Table name: snippets
#
N
Nihad Abbasov 已提交
66
#  id         :integer         not null, primary key
G
gitlabhq 已提交
67 68
#  title      :string(255)
#  content    :text
N
Nihad Abbasov 已提交
69 70
#  author_id  :integer         not null
#  project_id :integer         not null
R
randx 已提交
71 72
#  created_at :datetime        not null
#  updated_at :datetime        not null
G
gitlabhq 已提交
73
#  file_name  :string(255)
N
Nihad Abbasov 已提交
74
#  expires_at :datetime
G
gitlabhq 已提交
75 76
#