snippet.rb 4.49 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
class Snippet < ActiveRecord::Base
2
  include Gitlab::VisibilityLevel
3
  include Linguist::BlobHelper
4
  include CacheMarkdownField
5
  include Noteable
6
  include Participable
7 8
  include Referable
  include Sortable
9
  include Awardable
10
  include Mentionable
11
  include Spammable
gitlabhq's avatar
gitlabhq committed
12

13 14 15 16 17 18 19 20 21
  cache_markdown_field :title, pipeline: :single_line
  cache_markdown_field :content

  # If file_name changes, it invalidates content
  alias_method :default_content_html_invalidator, :content_html_invalidated?
  def content_html_invalidated?
    default_content_html_invalidator || file_name_changed?
  end

22
  default_value_for(:visibility_level) { current_application_settings.default_snippet_visibility }
23

24 25
  belongs_to :author, class_name: 'User'
  belongs_to :project
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
32
  validates :title, presence: true, length: { maximum: 255 }
33
  validates :file_name,
34
    length: { maximum: 255 },
Douwe Maan's avatar
Douwe Maan committed
35 36
    format: { with: Gitlab::Regex.file_name_regex,
              message: Gitlab::Regex.file_name_regex_message }
37

38
  validates :content, presence: true
39
  validates :visibility_level, inclusion: { in: Gitlab::VisibilityLevel.values }
gitlabhq's avatar
gitlabhq committed
40

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
41
  # Scopes
42 43 44 45
  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
46
  scope :fresh,   -> { order("created_at DESC") }
47

Yorick Peterse's avatar
Yorick Peterse committed
48 49
  participant :author
  participant :notes_with_associations
50

51 52 53
  attr_spammable :title, spam_title: true
  attr_spammable :content, spam_description: true

54 55 56 57
  def self.reference_prefix
    '$'
  end

58 59 60 61
  # Pattern used to extract `$123` snippet references from text
  #
  # This pattern supports cross-project references.
  def self.reference_pattern
62
    @reference_pattern ||= %r{
63 64
      (#{Project.reference_pattern})?
      #{Regexp.escape(reference_prefix)}(?<snippet>\d+)
65 66 67
    }x
  end

68
  def self.link_reference_pattern
69
    @link_reference_pattern ||= super("snippets", /(?<snippet>\d+)/)
70 71
  end

72
  def to_reference(from_project = nil, full: false)
73 74
    reference = "#{self.class.reference_prefix}#{id}"

75
    if project.present?
76
      "#{project.to_reference(from_project, full: full)}#{reference}"
77 78
    else
      reference
79 80 81
    end
  end

gitlabhq's avatar
gitlabhq committed
82
  def self.content_types
Nihad Abbasov's avatar
Nihad Abbasov committed
83
    [
gitlabhq's avatar
gitlabhq committed
84 85 86 87 88
      ".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
89

90 91 92 93
  def data
    content
  end

94 95 96 97
  def hook_attrs
    attributes
  end

98 99 100 101
  def size
    0
  end

102 103 104 105
  def file_name
    super.to_s
  end

106 107 108 109 110
  # alias for compatibility with blobs and highlighting
  def path
    file_name
  end

111
  def name
112 113 114
    file_name
  end

115 116 117 118
  def sanitized_file_name
    file_name.gsub(/[^a-zA-Z0-9_\-\.]+/, '')
  end

119
  def mode
120
    nil
gitlabhq's avatar
gitlabhq committed
121
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
122

123
  def visibility_level_field
124
    :visibility_level
125
  end
126

127 128 129 130
  def no_highlighting?
    content.lines.count > 1000
  end

Yorick Peterse's avatar
Yorick Peterse committed
131
  def notes_with_associations
132
    notes.includes(:author)
Yorick Peterse's avatar
Yorick Peterse committed
133 134
  end

135
  def check_for_spam?
136 137
    visibility_level_changed?(to: Snippet::PUBLIC) ||
      (public? && (title_changed? || content_changed?))
138 139 140 141 142 143
  end

  def spammable_entity_type
    'snippet'
  end

144
  class << self
145 146 147 148 149 150 151
    # 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.
152
    def search(query)
153
      t = arel_table
154 155 156
      pattern = "%#{query}%"

      where(t[:title].matches(pattern).or(t[:file_name].matches(pattern)))
157 158
    end

159 160 161 162 163 164 165
    # 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.
166
    def search_code(query)
167 168 169 170
      table   = Snippet.arel_table
      pattern = "%#{query}%"

      where(table[:content].matches(pattern))
171 172 173
    end

    def accessible_to(user)
174 175 176 177 178 179 180 181 182 183
      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))
184 185
    end
  end
gitlabhq's avatar
gitlabhq committed
186
end