issuable.rb 4.0 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 Mentionable
10
  include Participable
11 12

  included do
13 14
    belongs_to :author, class_name: "User"
    belongs_to :assignee, class_name: "User"
15
    belongs_to :milestone
16
    has_many :notes, as: :noteable, dependent: :destroy
17 18
    has_many :label_links, as: :target, dependent: :destroy
    has_many :labels, through: :label_links
19
    has_many :subscriptions, dependent: :destroy, as: :subscribable
20

A
Andrey Kumanyaev 已提交
21 22
    validates :author, presence: true
    validates :title, presence: true, length: { within: 0..255 }
23

24
    scope :authored, ->(user) { where(author_id: user) }
25
    scope :assigned_to, ->(u) { where(assignee_id: u.id)}
A
Andrew8xx8 已提交
26
    scope :recent, -> { order("created_at DESC") }
27 28
    scope :assigned, -> { where("assignee_id IS NOT NULL") }
    scope :unassigned, -> { where("assignee_id IS NULL") }
29
    scope :of_projects, ->(ids) { where(project_id: ids) }
30
    scope :opened, -> { with_state(:opened, :reopened) }
31 32
    scope :only_opened, -> { with_state(:opened) }
    scope :only_reopened, -> { with_state(:reopened) }
33
    scope :closed, -> { with_state(:closed) }
34 35
    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') }
36

37 38
    delegate :name,
             :email,
39 40
             to: :author,
             prefix: true
41 42 43

    delegate :name,
             :email,
44 45 46
             to: :assignee,
             allow_nil: true,
             prefix: true
47

48
    attr_mentionable :title, :description
49 50
  end

51 52
  module ClassMethods
    def search(query)
53
      where("LOWER(title) like :query", query: "%#{query.downcase}%")
54
    end
55

56 57 58 59
    def full_search(query)
      where("LOWER(title) like :query OR LOWER(description) like :query", query: "%#{query.downcase}%")
    end

60
    def sort(method)
61 62 63 64 65 66
      case method.to_s
      when 'milestone_due_asc' then order_milestone_due_asc
      when 'milestone_due_desc' then order_milestone_due_desc
      else
        order_by(method)
      end
67
    end
68 69 70 71 72 73 74 75 76
  end

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

  def new?
    today? && created_at == updated_at
  end
77 78 79 80 81 82 83 84 85

  def is_assigned?
    !!assignee_id
  end

  def is_being_reassigned?
    assignee_id_changed?
  end

86 87 88 89 90 91
  #
  # Votes
  #

  # Return the number of -1 comments (downvotes)
  def downvotes
M
Michael Clarke 已提交
92
    filter_superceded_votes(notes.select(&:downvote?), notes).size
93 94
  end

95
  def downvotes_in_percent
96 97 98
    if votes_count.zero?
      0
    else
99
      100.0 - upvotes_in_percent
100 101 102
    end
  end

103 104
  # Return the number of +1 comments (upvotes)
  def upvotes
M
Michael Clarke 已提交
105
    filter_superceded_votes(notes.select(&:upvote?), notes).size
106 107
  end

108
  def upvotes_in_percent
109 110 111
    if votes_count.zero?
      0
    else
112
      100.0 / votes_count * upvotes
113 114 115 116 117 118 119
    end
  end

  # Return the total number of votes
  def votes_count
    upvotes + downvotes
  end
120

V
tests  
Valery Sizov 已提交
121
  def subscribed?(user)
122 123 124 125
    subscription = subscriptions.find_by_user_id(user.id)

    if subscription
      return subscription.subscribed
V
Valery Sizov 已提交
126 127
    end

128
    participants.include?(user)
V
Valery Sizov 已提交
129 130
  end

V
tests  
Valery Sizov 已提交
131 132 133 134 135 136
  def toggle_subscription(user)
    subscriptions.
      find_or_initialize_by(user_id: user.id).
      update(subscribed: !subscribed?(user))
  end

K
Kirill Zaitsev 已提交
137
  def to_hook_data(user)
138 139
    {
      object_kind: self.class.name.underscore,
K
Kirill Zaitsev 已提交
140
      user: user.hook_attrs,
K
Kirill Zaitsev 已提交
141
      object_attributes: hook_attrs
142 143
    }
  end
144 145 146 147 148

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

149 150 151 152
  def remove_labels
    labels.delete_all
  end

153 154
  def add_labels_by_names(label_names)
    label_names.each do |label_name|
155 156
      label = project.labels.create_with(color: Label::DEFAULT_COLOR).
        find_or_create_by(title: label_name.strip)
157 158 159
      self.labels << label
    end
  end
M
Michael Clarke 已提交
160 161 162 163 164

  private

  def filter_superceded_votes(votes, notes)
    filteredvotes = [] + votes
165

M
Michael Clarke 已提交
166 167 168 169 170
    votes.each do |vote|
      if vote.superceded?(notes)
        filteredvotes.delete(vote)
      end
    end
171

M
Michael Clarke 已提交
172 173
    filteredvotes
  end
174
end