snippet.rb 1.5 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# == Schema Information
#
# Table name: snippets
#
#  id         :integer          not null, primary key
#  title      :string(255)
#  content    :text
#  author_id  :integer          not null
#  project_id :integer          not null
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  file_name  :string(255)
#  expires_at :datetime
#

G
gitlabhq 已提交
16
class Snippet < ActiveRecord::Base
17
  include Linguist::BlobHelper
G
gitlabhq 已提交
18

19 20
  attr_accessible :title, :content, :file_name, :expires_at

G
gitlabhq 已提交
21
  belongs_to :project
22 23
  belongs_to :author, class_name: "User"
  has_many :notes, as: :noteable, dependent: :destroy
G
gitlabhq 已提交
24

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

A
Andrey Kumanyaev 已提交
27 28
  validates :author, presence: true
  validates :project, presence: true
N
Nihad Abbasov 已提交
29 30
  validates :title, presence: true, length: { within: 0..255 }
  validates :file_name, presence: true, length: { within: 0..255 }
V
Valeriy Sizov 已提交
31
  validates :content, presence: true
G
gitlabhq 已提交
32

A
Andrey Kumanyaev 已提交
33
  # Scopes
A
Andrew8xx8 已提交
34 35 36
  scope :fresh, -> { order("created_at DESC") }
  scope :non_expired, -> { where(["expires_at IS NULL OR expires_at > ?", Time.current]) }
  scope :expired, -> { where(["expires_at IS NOT NULL AND expires_at < ?", Time.current]) }
N
Nihad Abbasov 已提交
37

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

46 47 48 49 50 51 52 53
  def data
    content
  end

  def size
    0
  end

54
  def name
55 56 57
    file_name
  end

58
  def mode
59
    nil
G
gitlabhq 已提交
60
  end
N
Nihad Abbasov 已提交
61 62 63 64

  def expired?
    expires_at && expires_at < Time.current
  end
G
gitlabhq 已提交
65
end