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

gitlabhq's avatar
gitlabhq committed
18
class Snippet < ActiveRecord::Base
19
  include Linguist::BlobHelper
gitlabhq's avatar
gitlabhq committed
20

21
  attr_accessible :title, :content, :file_name, :expires_at, :private
22

23 24
  default_value_for :private, true

25
  belongs_to :author, class_name: "User"
Andrew8xx8's avatar
Andrew8xx8 committed
26

27
  has_many :notes, as: :noteable, dependent: :destroy
gitlabhq's avatar
gitlabhq committed
28

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
29
  delegate :name, :email, to: :author, prefix: true, allow_nil: true
gitlabhq's avatar
gitlabhq committed
30

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
31
  validates :author, presence: true
Nihad Abbasov's avatar
Nihad Abbasov committed
32 33
  validates :title, presence: true, length: { within: 0..255 }
  validates :file_name, presence: true, length: { within: 0..255 }
34
  validates :content, presence: true
gitlabhq's avatar
gitlabhq committed
35

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
36
  # Scopes
Andrew8xx8's avatar
Andrew8xx8 committed
37 38 39
  scope :public,  -> { where(private: false) }
  scope :private, -> { where(private: true) }
  scope :fresh,   -> { order("created_at DESC") }
Andrew8xx8's avatar
Andrew8xx8 committed
40
  scope :expired, -> { where(["expires_at IS NOT NULL AND expires_at < ?", Time.current]) }
Andrew8xx8's avatar
Andrew8xx8 committed
41
  scope :non_expired, -> { where(["expires_at IS NULL OR expires_at > ?", Time.current]) }
42

gitlabhq's avatar
gitlabhq committed
43
  def self.content_types
Nihad Abbasov's avatar
Nihad Abbasov committed
44
    [
gitlabhq's avatar
gitlabhq committed
45 46 47 48 49
      ".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
50

51 52 53 54 55 56 57 58
  def data
    content
  end

  def size
    0
  end

59
  def name
60 61 62
    file_name
  end

63
  def mode
64
    nil
gitlabhq's avatar
gitlabhq committed
65
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
66 67 68 69

  def expired?
    expires_at && expires_at < Time.current
  end
gitlabhq's avatar
gitlabhq committed
70
end