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

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

22
  default_value_for :visibility_level, Snippet::PRIVATE
23

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

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

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

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

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
36
  # Scopes
37 38 39 40
  scope :are_internal,  -> { where(visibility_level: Snippet::INTERNAL) }
  scope :are_private, -> { where(visibility_level: Snippet::PRIVATE) }
  scope :are_public, -> { where(visibility_level: Snippet::PUBLIC) }
  scope :public_and_internal, -> { where(visibility_level: [Snippet::PUBLIC, Snippet::INTERNAL]) }
Andrew8xx8's avatar
Andrew8xx8 committed
41
  scope :fresh,   -> { order("created_at DESC") }
Andrew8xx8's avatar
Andrew8xx8 committed
42
  scope :expired, -> { where(["expires_at IS NOT NULL AND expires_at < ?", Time.current]) }
Andrew8xx8's avatar
Andrew8xx8 committed
43
  scope :non_expired, -> { where(["expires_at IS NULL OR expires_at > ?", Time.current]) }
44

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

53 54 55 56 57 58 59 60
  def data
    content
  end

  def size
    0
  end

61
  def name
62 63 64
    file_name
  end

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

  def expired?
    expires_at && expires_at < Time.current
  end
72

73 74 75 76
  def visibility_level_field
    visibility_level
  end 

77 78 79 80 81 82 83 84 85 86
  class << self
    def search(query)
      where('(title LIKE :query OR file_name LIKE :query)', query: "%#{query}%")
    end

    def search_code(query)
      where('(content LIKE :query)', query: "%#{query}%")
    end

    def accessible_to(user)
87
      where('visibility_level IN (?) OR author_id = ?', [Snippet::INTERNAL, Snippet::PUBLIC], user)
88 89
    end
  end
gitlabhq's avatar
gitlabhq committed
90
end