issuable.rb 6.2 KB
Newer Older
1
# == Issuable concern
D
Dmitriy Zaporozhets 已提交
2
#
3
# Contains common functionality shared between Issues and MergeRequests
D
Dmitriy Zaporozhets 已提交
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 14

  included do
15 16
    belongs_to :author, class_name: "User"
    belongs_to :assignee, class_name: "User"
17
    belongs_to :updated_by, class_name: "User"
18
    belongs_to :milestone
19
    has_many :notes, as: :noteable, dependent: :destroy
20 21
    has_many :label_links, as: :target, dependent: :destroy
    has_many :labels, through: :label_links
22
    has_many :todos, as: :target, dependent: :destroy
23

A
Andrey Kumanyaev 已提交
24 25
    validates :author, presence: true
    validates :title, presence: true, length: { within: 0..255 }
26

27
    scope :authored, ->(user) { where(author_id: user) }
28
    scope :assigned_to, ->(u) { where(assignee_id: u.id)}
29
    scope :recent, -> { reorder(id: :desc) }
30 31
    scope :assigned, -> { where("assignee_id IS NOT NULL") }
    scope :unassigned, -> { where("assignee_id IS NULL") }
32
    scope :of_projects, ->(ids) { where(project_id: ids) }
33
    scope :of_milestones, ->(ids) { where(milestone_id: ids) }
34
    scope :opened, -> { with_state(:opened, :reopened) }
35 36
    scope :only_opened, -> { with_state(:opened) }
    scope :only_reopened, -> { with_state(:reopened) }
37
    scope :closed, -> { with_state(:closed) }
38 39
    scope :order_milestone_due_desc, -> { joins(:milestone).reorder('milestones.due_date DESC, milestones.id DESC') }
    scope :order_milestone_due_asc, -> { joins(:milestone).reorder('milestones.due_date ASC, milestones.id ASC') }
R
Rubén Dávila 已提交
40 41
    scope :with_label, ->(title) { joins(:labels).where(labels: { title: title }) }
    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 }) }
42

43 44
    scope :join_project, -> { joins(:project) }
    scope :references_project, -> { references(:project) }
45
    scope :non_archived, -> { join_project.merge(Project.non_archived.only(:where)) }
46

47 48
    delegate :name,
             :email,
49 50
             to: :author,
             prefix: true
51 52 53

    delegate :name,
             :email,
54 55 56
             to: :assignee,
             allow_nil: true,
             prefix: true
57

58
    attr_mentionable :title, pipeline: :single_line
59
    attr_mentionable :description, cache: true
60
    participant :author, :assignee, :notes_with_associations
61
    strip_attributes :title
62 63

    acts_as_paranoid
64 65
  end

66
  module ClassMethods
67 68 69 70 71 72 73
    # 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.
74
    def search(query)
75
      where(arel_table[:title].matches("%#{query}%"))
76
    end
77

78 79 80 81 82 83 84
    # 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.
85
    def full_search(query)
86 87 88 89
      t = arel_table
      pattern = "%#{query}%"

      where(t[:title].matches(pattern).or(t[:description].matches(pattern)))
90 91
    end

92
    def sort(method)
93 94 95
      case method.to_s
      when 'milestone_due_asc' then order_milestone_due_asc
      when 'milestone_due_desc' then order_milestone_due_desc
96 97
      when 'downvotes_desc' then order_downvotes_desc
      when 'upvotes_desc' then order_upvotes_desc
98 99 100
      else
        order_by(method)
      end
101
    end
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

    def order_downvotes_desc
      order_votes_desc('thumbsdown')
    end

    def order_upvotes_desc
      order_votes_desc('thumbsup')
    end

    def order_votes_desc(award_emoji_name)
      issuable_table = self.arel_table
      note_table = Note.arel_table

      join_clause = issuable_table.join(note_table, Arel::Nodes::OuterJoin).on(
        note_table[:noteable_id].eq(issuable_table[:id]).and(
          note_table[:noteable_type].eq(self.name).and(
            note_table[:is_award].eq(true).and(note_table[:note].eq(award_emoji_name))
          )
        )
      ).join_sources

      joins(join_clause).group(issuable_table[:id]).reorder("COUNT(notes.id) DESC")
    end
125 126 127 128 129 130 131 132 133
  end

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

  def new?
    today? && created_at == updated_at
  end
134 135 136 137 138 139 140 141 142

  def is_assigned?
    !!assignee_id
  end

  def is_being_reassigned?
    assignee_id_changed?
  end

143 144 145 146
  def open?
    opened? || reopened?
  end

147
  def downvotes
148
    notes.awards.where(note: "thumbsdown").count
149 150
  end

151
  def upvotes
152
    notes.awards.where(note: "thumbsup").count
153
  end
154

155 156 157 158
  def subscribed_without_subscriptions?(user)
    participants(user).include?(user)
  end

K
Kirill Zaitsev 已提交
159
  def to_hook_data(user)
160
    hook_data = {
161
      object_kind: self.class.name.underscore,
K
Kirill Zaitsev 已提交
162
      user: user.hook_attrs,
163 164 165 166
      project: project.hook_attrs,
      object_attributes: hook_attrs,
      # DEPRECATED
      repository: project.hook_attrs.slice(:name, :url, :description, :homepage)
167
    }
168 169 170
    hook_data.merge!(assignee: assignee.hook_attrs) if assignee

    hook_data
171
  end
172 173 174 175 176

  def label_names
    labels.order('title ASC').pluck(:title)
  end

177 178 179 180
  def remove_labels
    labels.delete_all
  end

181 182
  def add_labels_by_names(label_names)
    label_names.each do |label_name|
183 184
      label = project.labels.create_with(color: Label::DEFAULT_COLOR).
        find_or_create_by(title: label_name.strip)
185 186 187
      self.labels << label
    end
  end
M
Michael Clarke 已提交
188

R
Robert Speicher 已提交
189 190 191 192 193 194 195 196 197 198
  # 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

199 200 201 202 203 204 205 206
  # Returns a Hash of attributes to be used for Twitter card metadata
  def card_attributes
    {
      'Author'   => author.try(:name),
      'Assignee' => assignee.try(:name)
    }
  end

207 208 209 210
  def notes_with_associations
    notes.includes(:author, :project)
  end

211 212 213
  def updated_tasks
    Taskable.get_updated_tasks(old_content: previous_changes['description'].first,
                               new_content: description)
M
Michael Clarke 已提交
214
  end
215 216 217 218 219 220 221 222 223

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