issuable.rb 8.85 KB
Newer Older
1
# == Issuable concern
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
2
#
3
# Contains common functionality shared between Issues and MergeRequests
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
4 5 6
#
# Used by Issue, MergeRequest
#
7
module Issuable
8
  extend ActiveSupport::Concern
9
  include Participable
10
  include Mentionable
11
  include Subscribable
12
  include StripAttribute
13
  include Awardable
14 15

  included do
16 17
    belongs_to :author, class_name: "User"
    belongs_to :assignee, class_name: "User"
18
    belongs_to :updated_by, class_name: "User"
19
    belongs_to :milestone
20 21
    has_many :notes, as: :noteable, dependent: :destroy do
      def authors_loaded?
22
        # We check first if we're loaded to not load unnecessarily.
23 24
        loaded? && to_a.all? { |note| note.association(:author).loaded? }
      end
25 26 27 28 29

      def award_emojis_loaded?
        # We check first if we're loaded to not load unnecessarily.
        loaded? && to_a.all? { |note| note.association(:award_emoji).loaded? }
      end
30
    end
31 32
    has_many :label_links, as: :target, dependent: :destroy
    has_many :labels, through: :label_links
33
    has_many :todos, as: :target, dependent: :destroy
34

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
35 36
    validates :author, presence: true
    validates :title, presence: true, length: { within: 0..255 }
37

38
    scope :authored, ->(user) { where(author_id: user) }
39
    scope :assigned_to, ->(u) { where(assignee_id: u.id)}
40
    scope :recent, -> { reorder(id: :desc) }
41 42
    scope :assigned, -> { where("assignee_id IS NOT NULL") }
    scope :unassigned, -> { where("assignee_id IS NULL") }
43
    scope :of_projects, ->(ids) { where(project_id: ids) }
44
    scope :of_milestones, ->(ids) { where(milestone_id: ids) }
45
    scope :with_milestone, ->(title) { left_joins_milestones.where(milestones: { title: title }) }
46
    scope :opened, -> { with_state(:opened, :reopened) }
47 48
    scope :only_opened, -> { with_state(:opened) }
    scope :only_reopened, -> { with_state(:reopened) }
49
    scope :closed, -> { with_state(:closed) }
50

51
    scope :left_joins_milestones,    -> { joins("LEFT OUTER JOIN milestones ON #{table_name}.milestone_id = milestones.id") }
52 53
    scope :order_milestone_due_desc, -> { left_joins_milestones.reorder('milestones.due_date IS NULL, milestones.id IS NULL, milestones.due_date DESC') }
    scope :order_milestone_due_asc,  -> { left_joins_milestones.reorder('milestones.due_date IS NULL, milestones.id IS NULL, milestones.due_date ASC') }
54

55
    scope :without_label, -> { joins("LEFT OUTER JOIN label_links ON label_links.target_type = '#{name}' AND label_links.target_id = #{table_name}.id").where(label_links: { id: nil }) }
56
    scope :join_project, -> { joins(:project) }
57
    scope :inc_notes_with_associations, -> { includes(notes: [ :project, :author, :award_emoji ]) }
58
    scope :references_project, -> { references(:project) }
59
    scope :non_archived, -> { join_project.where(projects: { archived: false }) }
60 61


62 63
    delegate :name,
             :email,
64 65
             to: :author,
             prefix: true
66 67 68

    delegate :name,
             :email,
69 70 71
             to: :assignee,
             allow_nil: true,
             prefix: true
72

73
    attr_mentionable :title, pipeline: :single_line
Yorick Peterse's avatar
Yorick Peterse committed
74 75 76 77 78 79
    attr_mentionable :description

    participant :author
    participant :assignee
    participant :notes_with_associations

80
    strip_attributes :title
81 82

    acts_as_paranoid
83 84 85 86 87 88

    after_save :update_assignee_cache_counts, if: :assignee_id_changed?

    def update_assignee_cache_counts
      # make sure we flush the cache for both the old *and* new assignee
      User.find(assignee_id_was).update_cache_counts if assignee_id_was
89
      assignee.update_cache_counts if assignee
90
    end
91 92
  end

93
  module ClassMethods
94 95 96 97 98 99 100
    # Searches for records with a matching title.
    #
    # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
    #
    # query - The search query as a String
    #
    # Returns an ActiveRecord::Relation.
101
    def search(query)
102
      where(arel_table[:title].matches("%#{query}%"))
103
    end
104

105 106 107 108 109 110 111
    # Searches for records with a matching title or description.
    #
    # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
    #
    # query - The search query as a String
    #
    # Returns an ActiveRecord::Relation.
112
    def full_search(query)
113 114 115 116
      t = arel_table
      pattern = "%#{query}%"

      where(t[:title].matches(pattern).or(t[:description].matches(pattern)))
117 118
    end

119
    def sort(method, excluded_labels: [])
120 121 122 123 124 125 126 127 128 129 130 131
      sorted = case method.to_s
               when 'milestone_due_asc' then order_milestone_due_asc
               when 'milestone_due_desc' then order_milestone_due_desc
               when 'downvotes_desc' then order_downvotes_desc
               when 'upvotes_desc' then order_upvotes_desc
               when 'priority' then order_labels_priority(excluded_labels: excluded_labels)
               else
                 order_by(method)
               end

      # Break ties with the ID column for pagination
      sorted.order(id: :desc)
132
    end
133

134 135 136 137 138 139
    def order_labels_priority(excluded_labels: [])
      select("#{table_name}.*, (#{highest_label_priority(excluded_labels).to_sql}) AS highest_priority").
        group(arel_table[:id]).
        reorder(Gitlab::Database.nulls_last_order('highest_priority', 'ASC'))
    end

140
    def with_label(title, sort = nil)
141
      if title.is_a?(Array) && title.size > 1
142
        joins(:labels).where(labels: { title: title }).group(*grouping_columns(sort)).having("COUNT(DISTINCT labels.title) = #{title.size}")
143 144 145 146
      else
        joins(:labels).where(labels: { title: title })
      end
    end
147 148 149 150 151

    # Includes table keys in group by clause when sorting
    # preventing errors in postgres
    #
    # Returns an array of arel columns
152 153
    def grouping_columns(sort)
      grouping_columns = [arel_table[:id]]
154 155 156

      if ["milestone_due_desc", "milestone_due_asc"].include?(sort)
        milestone_table = Milestone.arel_table
157 158
        grouping_columns << milestone_table[:id]
        grouping_columns << milestone_table[:due_date]
159 160
      end

161
      grouping_columns
162
    end
163 164 165 166 167 168 169 170 171 172 173 174 175 176

    private

    def highest_label_priority(excluded_labels)
      query = Label.select(Label.arel_table[:priority].minimum).
        joins(:label_links).
        where(label_links: { target_type: name }).
        where("label_links.target_id = #{table_name}.id").
        reorder(nil)

      query.where.not(title: excluded_labels) if excluded_labels.present?

      query
    end
177 178 179 180 181 182 183 184 185
  end

  def today?
    Date.today == created_at.to_date
  end

  def new?
    today? && created_at == updated_at
  end
186 187 188 189 190

  def is_being_reassigned?
    assignee_id_changed?
  end

191 192 193 194
  def open?
    opened? || reopened?
  end

Z.J. van de Weg's avatar
Z.J. van de Weg committed
195
  def user_notes_count
196 197 198 199 200 201 202
    if notes.loaded?
      # Use the in-memory association to select and count to avoid hitting the db
      notes.to_a.count { |note| !note.system? }
    else
      # do the count query
      notes.user.count
    end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
203 204
  end

205 206 207 208
  def subscribed_without_subscriptions?(user)
    participants(user).include?(user)
  end

Kirill Zaitsev's avatar
Kirill Zaitsev committed
209
  def to_hook_data(user)
210
    hook_data = {
211
      object_kind: self.class.name.underscore,
Kirill Zaitsev's avatar
Kirill Zaitsev committed
212
      user: user.hook_attrs,
213 214 215 216
      project: project.hook_attrs,
      object_attributes: hook_attrs,
      # DEPRECATED
      repository: project.hook_attrs.slice(:name, :url, :description, :homepage)
217
    }
218 219 220
    hook_data.merge!(assignee: assignee.hook_attrs) if assignee

    hook_data
221
  end
222

223 224 225 226
  def labels_array
    labels.to_a
  end

227 228 229 230
  def label_names
    labels.order('title ASC').pluck(:title)
  end

231 232 233 234
  def remove_labels
    labels.delete_all
  end

235 236
  def add_labels_by_names(label_names)
    label_names.each do |label_name|
237 238
      label = project.labels.create_with(color: Label::DEFAULT_COLOR).
        find_or_create_by(title: label_name.strip)
239 240 241
      self.labels << label
    end
  end
242

243 244 245 246 247 248 249 250 251 252
  # Convert this Issuable class name to a format usable by Ability definitions
  #
  # Examples:
  #
  #   issuable.class           # => MergeRequest
  #   issuable.to_ability_name # => "merge_request"
  def to_ability_name
    self.class.to_s.underscore
  end

253 254 255 256 257 258 259 260
  # Returns a Hash of attributes to be used for Twitter card metadata
  def card_attributes
    {
      'Author'   => author.try(:name),
      'Assignee' => assignee.try(:name)
    }
  end

261
  def notes_with_associations
262 263 264 265 266 267
    # If A has_many Bs, and B has_many Cs, and you do
    # `A.includes(b: :c).each { |a| a.b.includes(:c) }`, sadly ActiveRecord
    # will do the inclusion again. So, we check if all notes in the relation
    # already have their authors loaded (possibly because the scope
    # `inc_notes_with_associations` was used) and skip the inclusion if that's
    # the case.
268 269 270 271 272 273 274 275
    includes = []
    includes << :author unless notes.authors_loaded?
    includes << :award_emoji unless notes.award_emojis_loaded?
    if includes.any?
      notes.includes(includes)
    else
      notes
    end
276 277
  end

278 279 280
  def updated_tasks
    Taskable.get_updated_tasks(old_content: previous_changes['description'].first,
                               new_content: description)
281
  end
282 283 284 285 286 287 288 289 290

  ##
  # Method that checks if issuable can be moved to another project.
  #
  # Should be overridden if issuable can be moved.
  #
  def can_move?(*)
    false
  end
291
end