BigW Consortium Gitlab

snippet.rb 4.04 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
12
  include Gitlab::SQL::Pattern
gitlabhq committed
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

blackst0ne committed
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

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

33 34
  belongs_to :author, class_name: 'User'
  belongs_to :project
Andrew8xx8 committed
35

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

38
  delegate :name, :email, to: :author, prefix: true, allow_nil: true
gitlabhq committed
39

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

45
  validates :content, presence: true
46
  validates :visibility_level, inclusion: { in: Gitlab::VisibilityLevel.values }
gitlabhq committed
47

Andrey Kumanyaev committed
48
  # Scopes
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]) }
Andrew8xx8 committed
53
  scope :fresh,   -> { order("created_at DESC") }
54

Yorick Peterse committed
55 56
  participant :author
  participant :notes_with_associations
57

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

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

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

gitlabhq committed
89
  def self.content_types
Nihad Abbasov committed
90
    [
gitlabhq committed
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
gitlabhq committed
96

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

113
  def visibility_level_field
114
    :visibility_level
115
  end
116

Yorick Peterse committed
117
  def notes_with_associations
118
    notes.includes(:author)
Yorick Peterse committed
119 120
  end

121
  def check_for_spam?
122 123
    visibility_level_changed?(to: Snippet::PUBLIC) ||
      (public? && (title_changed? || content_changed?))
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
gitlabhq committed
153
end