BigW Consortium Gitlab

snippet.rb 3.71 KB
Newer Older
gitlabhq committed
1
class Snippet < ActiveRecord::Base
2
  include Gitlab::VisibilityLevel
3
  include Linguist::BlobHelper
4
  include Participable
5 6
  include Referable
  include Sortable
gitlabhq committed
7

8
  default_value_for :visibility_level, Snippet::PRIVATE
9

10 11
  belongs_to :author, class_name: 'User'
  belongs_to :project
Andrew8xx8 committed
12

13
  has_many :notes, as: :noteable, dependent: :destroy
gitlabhq committed
14

15
  delegate :name, :email, to: :author, prefix: true, allow_nil: true
gitlabhq committed
16

Andrey Kumanyaev committed
17
  validates :author, presence: true
18
  validates :title, presence: true, length: { within: 0..255 }
19 20
  validates :file_name,
    length: { within: 0..255 },
21 22
    format: { with: Gitlab::Regex.file_name_regex,
              message: Gitlab::Regex.file_name_regex_message }
23

24
  validates :content, presence: true
25
  validates :visibility_level, inclusion: { in: Gitlab::VisibilityLevel.values }
gitlabhq committed
26

Andrey Kumanyaev committed
27
  # Scopes
28 29 30 31
  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 committed
32
  scope :fresh,   -> { order("created_at DESC") }
33

Yorick Peterse committed
34 35
  participant :author
  participant :notes_with_associations
36

37 38 39 40
  def self.reference_prefix
    '$'
  end

41 42 43 44
  # Pattern used to extract `$123` snippet references from text
  #
  # This pattern supports cross-project references.
  def self.reference_pattern
45
    @reference_pattern ||= %r{
46 47
      (#{Project.reference_pattern})?
      #{Regexp.escape(reference_prefix)}(?<snippet>\d+)
48 49 50
    }x
  end

51
  def self.link_reference_pattern
52
    @link_reference_pattern ||= super("snippets", /(?<snippet>\d+)/)
53 54
  end

55 56 57 58 59 60 61 62 63 64
  def to_reference(from_project = nil)
    reference = "#{self.class.reference_prefix}#{id}"

    if cross_project_reference?(from_project)
      reference = project.to_reference + reference
    end

    reference
  end

gitlabhq committed
65
  def self.content_types
Nihad Abbasov committed
66
    [
gitlabhq committed
67 68 69 70 71
      ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java",
      ".haml", ".html", ".sass", ".scss", ".xml", ".php", ".erb",
      ".js", ".sh", ".coffee", ".yml", ".md"
    ]
  end
gitlabhq committed
72

73 74 75 76
  def data
    content
  end

77 78 79 80
  def hook_attrs
    attributes
  end

81 82 83 84
  def size
    0
  end

85 86 87 88 89
  # alias for compatibility with blobs and highlighting
  def path
    file_name
  end

90
  def name
91 92 93
    file_name
  end

94 95 96 97
  def sanitized_file_name
    file_name.gsub(/[^a-zA-Z0-9_\-\.]+/, '')
  end

98
  def mode
99
    nil
gitlabhq committed
100
  end
101

102 103
  def visibility_level_field
    visibility_level
104
  end
105

106 107 108 109
  def no_highlighting?
    content.lines.count > 1000
  end

Yorick Peterse committed
110
  def notes_with_associations
111
    notes.includes(:author)
Yorick Peterse committed
112 113
  end

114
  class << self
115 116 117 118 119 120 121
    # Searches for snippets with a matching title or file name.
    #
    # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
    #
    # query - The search query as a String.
    #
    # Returns an ActiveRecord::Relation.
122
    def search(query)
123
      t = arel_table
124 125 126
      pattern = "%#{query}%"

      where(t[:title].matches(pattern).or(t[:file_name].matches(pattern)))
127 128
    end

129 130 131 132 133 134 135
    # Searches for snippets with matching content.
    #
    # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
    #
    # query - The search query as a String.
    #
    # Returns an ActiveRecord::Relation.
136
    def search_code(query)
137 138 139 140
      table   = Snippet.arel_table
      pattern = "%#{query}%"

      where(table[:content].matches(pattern))
141 142 143
    end

    def accessible_to(user)
144 145 146 147 148 149 150 151 152 153
      return are_public unless user.present?
      return all if user.admin?

      where(
        'visibility_level IN (:visibility_levels)
         OR author_id = :author_id
         OR project_id IN (:project_ids)',
         visibility_levels: [Snippet::PUBLIC, Snippet::INTERNAL],
         author_id: user.id,
         project_ids: user.authorized_projects.select(:id))
154 155
    end
  end
gitlabhq committed
156
end