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

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

    validates :author, presence: true
    validates :title, presence: true, length: { within: 0..255 }

26
    scope :authored, ->(user) { where(author_id: user) }
27
    scope :assigned_to, ->(u) { where(assignee_id: u.id)}
28
    scope :recent, -> { reorder(id: :desc) }
29
30
    scope :assigned, -> { where("assignee_id IS NOT NULL") }
    scope :unassigned, -> { where("assignee_id IS NULL") }
31
    scope :of_projects, ->(ids) { where(project_id: ids) }
32
    scope :of_milestones, ->(ids) { where(milestone_id: ids) }
33
    scope :opened, -> { with_state(:opened, :reopened) }
34
35
    scope :only_opened, -> { with_state(:opened) }
    scope :only_reopened, -> { with_state(:reopened) }
36
    scope :closed, -> { with_state(:closed) }
37
38
    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') }
39
40
    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 }) }
41

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

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

    delegate :name,
             :email,
             to: :assignee,
             allow_nil: true,
             prefix: true

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

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

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

      where(t[:title].matches(pattern).or(t[:description].matches(pattern)))
87
88
    end

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

    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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  end

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

  def new?
    today? && created_at == updated_at
  end

  def is_assigned?
    !!assignee_id
  end

  def is_being_reassigned?
    assignee_id_changed?
  end

140
141
142
143
  def open?
    opened? || reopened?
  end

144
  def downvotes
145
    notes.awards.where(note: "thumbsdown").count
146
147
148
  end

  def upvotes
149
    notes.awards.where(note: "thumbsup").count
150
151
  end

152
153
154
155
  def subscribed_without_subscriptions?(user)
    participants(user).include?(user)
  end

Kirill Zaitsev's avatar
Kirill Zaitsev committed
156
  def to_hook_data(user)
157
    hook_data = {
158
      object_kind: self.class.name.underscore,
Kirill Zaitsev's avatar
Kirill Zaitsev committed
159
      user: user.hook_attrs,
160
161
162
163
      project: project.hook_attrs,
      object_attributes: hook_attrs,
      # DEPRECATED
      repository: project.hook_attrs.slice(:name, :url, :description, :homepage)
164
    }
165
166
167
    hook_data.merge!(assignee: assignee.hook_attrs) if assignee

    hook_data
168
  end
169
170
171
172
173

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

174
175
176
177
  def remove_labels
    labels.delete_all
  end

178
179
  def add_labels_by_names(label_names)
    label_names.each do |label_name|
180
181
      label = project.labels.create_with(color: Label::DEFAULT_COLOR).
        find_or_create_by(title: label_name.strip)
182
183
184
      self.labels << label
    end
  end
185

Robert Speicher's avatar
Robert Speicher committed
186
187
188
189
190
191
192
193
194
195
  # 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

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

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

  def updated_tasks
    Taskable.get_updated_tasks(old_content: previous_changes['description'].first,
                               new_content: description)
  end
212
213
214
215
216
217
218
219
220

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