BigW Consortium Gitlab

snippet.rb 4.14 KB
Newer Older
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 committed
12

13
  cache_markdown_field :title, pipeline: :single_line
14
  cache_markdown_field :description
15 16
  cache_markdown_field :content

blackst0ne committed
17 18 19 20 21
  # 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

22 23 24 25 26 27
  # 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

28
  default_value_for(:visibility_level) { current_application_settings.default_snippet_visibility }
29

30 31
  belongs_to :author, class_name: 'User'
  belongs_to :project
Andrew8xx8 committed
32

33
  has_many :notes, as: :noteable, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
gitlabhq committed
34

35
  delegate :name, :email, to: :author, prefix: true, allow_nil: true
gitlabhq committed
36

Andrey Kumanyaev committed
37
  validates :author, presence: true
38
  validates :title, presence: true, length: { maximum: 255 }
39
  validates :file_name,
40
    length: { maximum: 255 }
41

42
  validates :content, presence: true
43
  validates :visibility_level, inclusion: { in: Gitlab::VisibilityLevel.values }
gitlabhq committed
44

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

Yorick Peterse committed
52 53
  participant :author
  participant :notes_with_associations
54

55 56 57
  attr_spammable :title, spam_title: true
  attr_spammable :content, spam_description: true

58 59 60 61
  def self.reference_prefix
    '$'
  end

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

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

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

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

gitlabhq committed
86
  def self.content_types
Nihad Abbasov committed
87
    [
gitlabhq committed
88 89 90 91 92
      ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java",
      ".haml", ".html", ".sass", ".scss", ".xml", ".php", ".erb",
      ".js", ".sh", ".coffee", ".yml", ".md"
    ]
  end
gitlabhq committed
93

94 95
  def blob
    @blob ||= Blob.decorate(SnippetBlob.new(self), nil)
96 97
  end

98 99 100 101
  def hook_attrs
    attributes
  end

102 103 104 105
  def file_name
    super.to_s
  end

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

110
  def visibility_level_field
111
    :visibility_level
112
  end
113

Yorick Peterse committed
114
  def notes_with_associations
115
    notes.includes(:author)
Yorick Peterse committed
116 117
  end

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

  def spammable_entity_type
    'snippet'
  end

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

      where(t[:title].matches(pattern).or(t[:file_name].matches(pattern)))
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 151 152 153
      table   = Snippet.arel_table
      pattern = "%#{query}%"

      where(table[:content].matches(pattern))
154 155
    end
  end
gitlabhq committed
156
end