BigW Consortium Gitlab

snippet.rb 1.65 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 committed
7
#  content    :text(2147483647)
8
#  author_id  :integer          not null
Dmitriy Zaporozhets committed
9
#  project_id :integer
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 committed
14
#  private    :boolean          default(TRUE), not null
15
#  type       :string(255)
Dmitriy Zaporozhets committed
16
#
17

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

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

23
  belongs_to :author, class_name: "User"
Andrew8xx8 committed
24

25
  has_many :notes, as: :noteable, dependent: :destroy
gitlabhq committed
26

27
  delegate :name, :email, to: :author, prefix: true, allow_nil: true
gitlabhq committed
28

Andrey Kumanyaev committed
29
  validates :author, presence: true
30 31
  validates :title, presence: true, length: { within: 0..255 }
  validates :file_name, presence: true, length: { within: 0..255 }
32
  validates :content, presence: true
gitlabhq committed
33

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

gitlabhq committed
41
  def self.content_types
Nihad Abbasov committed
42
    [
gitlabhq committed
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
gitlabhq committed
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
gitlabhq committed
63
  end
64 65 66 67

  def expired?
    expires_at && expires_at < Time.current
  end
gitlabhq committed
68
end