snippet.rb 1.53 KB
Newer Older
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
#

gitlabhq's avatar
gitlabhq committed
16
class Snippet < ActiveRecord::Base
17
  include Linguist::BlobHelper
gitlabhq's avatar
gitlabhq committed
18

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

gitlabhq's avatar
gitlabhq committed
21
  belongs_to :project
22 23
  belongs_to :author, class_name: "User"
  has_many :notes, as: :noteable, dependent: :destroy
gitlabhq's avatar
gitlabhq committed
24

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

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
27 28
  validates :author, presence: true
  validates :project, presence: true
Nihad Abbasov's avatar
Nihad Abbasov committed
29 30
  validates :title, presence: true, length: { within: 0..255 }
  validates :file_name, presence: true, length: { within: 0..255 }
31
  validates :content, presence: true
gitlabhq's avatar
gitlabhq committed
32

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
33
  # Scopes
Andrew8xx8's avatar
Andrew8xx8 committed
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]) }
37

gitlabhq's avatar
gitlabhq committed
38
  def self.content_types
Nihad Abbasov's avatar
Nihad Abbasov committed
39
    [
gitlabhq's avatar
gitlabhq committed
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
gitlabhq's avatar
gitlabhq committed
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
gitlabhq's avatar
gitlabhq committed
60
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
61 62 63 64

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