issuable.rb 6.58 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
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

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
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, -> { outer_join_milestone.reorder('milestones.due_date IS NULL ASC, milestones.due_date DESC, milestones.id DESC') }
    scope :order_milestone_due_asc, -> { outer_join_milestone.reorder('milestones.due_date IS NULL ASC, milestones.due_date ASC, milestones.id ASC') }
40
    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.where(projects: { archived: false }) }
45
    scope :outer_join_milestone, -> { joins("LEFT OUTER JOIN milestones ON milestones.id = #{table_name}.milestone_id") }
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

    def with_label(title)
      if title.is_a?(Array) && title.count > 1
        joins(:labels).where(labels: { title: title }).group('issues.id').having("count(distinct labels.title) = #{title.count}")
      else
        joins(:labels).where(labels: { title: title })
      end
    end
133
134
135
136
137
138
139
140
141
  end

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

  def new?
    today? && created_at == updated_at
  end
142
143
144
145
146
147
148
149
150

  def is_assigned?
    !!assignee_id
  end

  def is_being_reassigned?
    assignee_id_changed?
  end

151
152
153
154
  def open?
    opened? || reopened?
  end

155
  def downvotes
156
    notes.awards.where(note: "thumbsdown").count
157
158
  end

159
  def upvotes
160
    notes.awards.where(note: "thumbsup").count
161
  end
162

163
164
165
166
  def subscribed_without_subscriptions?(user)
    participants(user).include?(user)
  end

Kirill Zaitsev's avatar
Kirill Zaitsev committed
167
  def to_hook_data(user)
168
    hook_data = {
169
      object_kind: self.class.name.underscore,
Kirill Zaitsev's avatar
Kirill Zaitsev committed
170
      user: user.hook_attrs,
171
172
173
174
      project: project.hook_attrs,
      object_attributes: hook_attrs,
      # DEPRECATED
      repository: project.hook_attrs.slice(:name, :url, :description, :homepage)
175
    }
176
177
178
    hook_data.merge!(assignee: assignee.hook_attrs) if assignee

    hook_data
179
  end
180
181
182
183
184

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

185
186
187
188
  def remove_labels
    labels.delete_all
  end

189
190
  def add_labels_by_names(label_names)
    label_names.each do |label_name|
191
192
      label = project.labels.create_with(color: Label::DEFAULT_COLOR).
        find_or_create_by(title: label_name.strip)
193
194
195
      self.labels << label
    end
  end
196

Robert Speicher's avatar
Robert Speicher committed
197
198
199
200
201
202
203
204
205
206
  # 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

207
208
209
210
211
212
213
214
  # Returns a Hash of attributes to be used for Twitter card metadata
  def card_attributes
    {
      'Author'   => author.try(:name),
      'Assignee' => assignee.try(:name)
    }
  end

215
216
217
218
  def notes_with_associations
    notes.includes(:author, :project)
  end

219
220
221
  def updated_tasks
    Taskable.get_updated_tasks(old_content: previous_changes['description'].first,
                               new_content: description)
222
  end
223
224
225
226
227
228
229
230
231

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