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

G
gitlabhq 已提交
18
class Snippet < ActiveRecord::Base
19
  include Linguist::BlobHelper
G
gitlabhq 已提交
20

A
Andrew8xx8 已提交
21
  attr_accessible :title, :content, :file_name, :expires_at, :private
22

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

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

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

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

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

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

49 50 51 52 53 54 55 56
  def data
    content
  end

  def size
    0
  end

57
  def name
58 59 60
    file_name
  end

61
  def mode
62
    nil
G
gitlabhq 已提交
63
  end
N
Nihad Abbasov 已提交
64 65 66 67

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