snippet.rb 4.0 KB
Newer Older
G
gitlabhq 已提交
1
class Snippet < ActiveRecord::Base
V
Valery Sizov 已提交
2
  include Gitlab::VisibilityLevel
3
  include CacheMarkdownField
4
  include Noteable
5
  include Participable
6 7
  include Referable
  include Sortable
8
  include Awardable
9
  include Mentionable
S
Sean McGivern 已提交
10
  include Spammable
11
  include Editable
12
  include Gitlab::SQL::Pattern
G
gitlabhq 已提交
13

14 15
  extend Gitlab::CurrentSettings

16
  cache_markdown_field :title, pipeline: :single_line
17
  cache_markdown_field :description
18 19
  cache_markdown_field :content

B
blackst0ne 已提交
20 21 22 23 24
  # Aliases to make application_helper#edited_time_ago_with_tooltip helper work properly with snippets.
  # See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/10392/diffs#note_28719102
  alias_attribute :last_edited_at, :updated_at
  alias_attribute :last_edited_by, :updated_by

25 26 27 28 29 30
  # 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

S
Sean McGivern 已提交
31
  default_value_for(:visibility_level) { current_application_settings.default_snippet_visibility }
32

R
Robert Speicher 已提交
33 34
  belongs_to :author, class_name: 'User'
  belongs_to :project
A
Andrew8xx8 已提交
35

36
  has_many :notes, as: :noteable, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
G
gitlabhq 已提交
37

D
Dmitriy Zaporozhets 已提交
38
  delegate :name, :email, to: :author, prefix: true, allow_nil: true
G
gitlabhq 已提交
39

A
Andrey Kumanyaev 已提交
40
  validates :author, presence: true
41
  validates :title, presence: true, length: { maximum: 255 }
42
  validates :file_name,
43
    length: { maximum: 255 }
44

V
Valeriy Sizov 已提交
45
  validates :content, presence: true
V
Valery Sizov 已提交
46
  validates :visibility_level, inclusion: { in: Gitlab::VisibilityLevel.values }
G
gitlabhq 已提交
47

A
Andrey Kumanyaev 已提交
48
  # Scopes
V
Valery Sizov 已提交
49 50 51 52
  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]) }
A
Andrew8xx8 已提交
53
  scope :fresh,   -> { order("created_at DESC") }
N
Nihad Abbasov 已提交
54

Y
Yorick Peterse 已提交
55 56
  participant :author
  participant :notes_with_associations
57

S
Sean McGivern 已提交
58 59 60
  attr_spammable :title, spam_title: true
  attr_spammable :content, spam_description: true

61 62 63 64
  def self.reference_prefix
    '$'
  end

65 66 67 68
  # Pattern used to extract `$123` snippet references from text
  #
  # This pattern supports cross-project references.
  def self.reference_pattern
69
    @reference_pattern ||= %r{
70 71
      (#{Project.reference_pattern})?
      #{Regexp.escape(reference_prefix)}(?<snippet>\d+)
72 73 74
    }x
  end

75
  def self.link_reference_pattern
76
    @link_reference_pattern ||= super("snippets", /(?<snippet>\d+)/)
77 78
  end

J
Jarka Kadlecova 已提交
79
  def to_reference(from = nil, full: false)
80 81
    reference = "#{self.class.reference_prefix}#{id}"

82
    if project.present?
J
Jarka Kadlecova 已提交
83
      "#{project.to_reference(from, full: full)}#{reference}"
84 85
    else
      reference
86 87 88
    end
  end

G
gitlabhq 已提交
89
  def self.content_types
N
Nihad Abbasov 已提交
90
    [
G
gitlabhq 已提交
91 92 93 94 95
      ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java",
      ".haml", ".html", ".sass", ".scss", ".xml", ".php", ".erb",
      ".js", ".sh", ".coffee", ".yml", ".md"
    ]
  end
G
gitlabhq 已提交
96

D
Douwe Maan 已提交
97 98
  def blob
    @blob ||= Blob.decorate(SnippetBlob.new(self), nil)
99 100
  end

101 102 103 104
  def hook_attrs
    attributes
  end

105 106 107 108
  def file_name
    super.to_s
  end

109 110 111 112
  def sanitized_file_name
    file_name.gsub(/[^a-zA-Z0-9_\-\.]+/, '')
  end

V
Valery Sizov 已提交
113
  def visibility_level_field
114
    :visibility_level
115
  end
V
Valery Sizov 已提交
116

Y
Yorick Peterse 已提交
117
  def notes_with_associations
118
    notes.includes(:author)
Y
Yorick Peterse 已提交
119 120
  end

S
Sean McGivern 已提交
121
  def check_for_spam?
122 123
    visibility_level_changed?(to: Snippet::PUBLIC) ||
      (public? && (title_changed? || content_changed?))
S
Sean McGivern 已提交
124 125 126 127 128 129
  end

  def spammable_entity_type
    'snippet'
  end

130
  class << self
131 132 133 134 135 136 137
    # 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.
138
    def search(query)
139
      fuzzy_search(query, [:title, :file_name])
140 141
    end

142 143 144 145 146 147 148
    # 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.
149
    def search_code(query)
150
      fuzzy_search(query, [:content])
151 152
    end
  end
G
gitlabhq 已提交
153
end