snippet.rb 4.17 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
class Snippet < ActiveRecord::Base
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
10
  include Spammable
11
  include Editable
gitlabhq's avatar
gitlabhq committed
12

13 14
  extend Gitlab::CurrentSettings

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

blackst0ne's avatar
blackst0ne committed
19 20 21 22 23
  # 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

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

30
  default_value_for(:visibility_level) { current_application_settings.default_snippet_visibility }
31

32 33
  belongs_to :author, class_name: 'User'
  belongs_to :project
Andrew8xx8's avatar
Andrew8xx8 committed
34

35
  has_many :notes, as: :noteable, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
gitlabhq's avatar
gitlabhq committed
36

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

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
39
  validates :author, presence: true
40
  validates :title, presence: true, length: { maximum: 255 }
41
  validates :file_name,
42
    length: { maximum: 255 }
43

44
  validates :content, presence: true
45
  validates :visibility_level, inclusion: { in: Gitlab::VisibilityLevel.values }
gitlabhq's avatar
gitlabhq committed
46

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
47
  # Scopes
48 49 50 51
  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
52
  scope :fresh,   -> { order("created_at DESC") }
53

Yorick Peterse's avatar
Yorick Peterse committed
54 55
  participant :author
  participant :notes_with_associations
56

57 58 59
  attr_spammable :title, spam_title: true
  attr_spammable :content, spam_description: true

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

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

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

78
  def to_reference(from_project = nil, full: false)
79 80
    reference = "#{self.class.reference_prefix}#{id}"

81
    if project.present?
82
      "#{project.to_reference(from_project, full: full)}#{reference}"
83 84
    else
      reference
85 86 87
    end
  end

gitlabhq's avatar
gitlabhq committed
88
  def self.content_types
Nihad Abbasov's avatar
Nihad Abbasov committed
89
    [
gitlabhq's avatar
gitlabhq committed
90 91 92 93 94
      ".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
95

Douwe Maan's avatar
Douwe Maan committed
96 97
  def blob
    @blob ||= Blob.decorate(SnippetBlob.new(self), nil)
98 99
  end

100 101 102 103
  def hook_attrs
    attributes
  end

104 105 106 107
  def file_name
    super.to_s
  end

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

112
  def visibility_level_field
113
    :visibility_level
114
  end
115

Yorick Peterse's avatar
Yorick Peterse committed
116
  def notes_with_associations
117
    notes.includes(:author)
Yorick Peterse's avatar
Yorick Peterse committed
118 119
  end

120
  def check_for_spam?
121 122
    visibility_level_changed?(to: Snippet::PUBLIC) ||
      (public? && (title_changed? || content_changed?))
123 124 125 126 127 128
  end

  def spammable_entity_type
    'snippet'
  end

129
  class << self
130 131 132 133 134 135 136
    # 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.
137
    def search(query)
138
      t = arel_table
139 140 141
      pattern = "%#{query}%"

      where(t[:title].matches(pattern).or(t[:file_name].matches(pattern)))
142 143
    end

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

      where(table[:content].matches(pattern))
156 157
    end
  end
gitlabhq's avatar
gitlabhq committed
158
end