issuable.rb 2.18 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 9 10 11
  extend ActiveSupport::Concern

  included do
    belongs_to :project
12 13
    belongs_to :author, class_name: "User"
    belongs_to :assignee, class_name: "User"
14
    belongs_to :milestone
15
    has_many :notes, as: :noteable, dependent: :destroy
16

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
17 18 19
    validates :project, presence: true
    validates :author, presence: true
    validates :title, presence: true, length: { within: 0..255 }
20
    validates :closed, inclusion: { in: [true, false] }
21

Andrew8xx8's avatar
Andrew8xx8 committed
22 23
    scope :opened, -> { where(closed: false) }
    scope :closed, -> { where(closed: true) }
24
    scope :of_group, ->(group) { where(project_id: group.project_ids) }
25
    scope :of_user_team, ->(team) { where(project_id: team.project_ids, assignee_id: team.member_ids) }
Valeriy Sizov's avatar
Valeriy Sizov committed
26
    scope :assigned, ->(u) { where(assignee_id: u.id)}
Andrew8xx8's avatar
Andrew8xx8 committed
27
    scope :recent, -> { order("created_at DESC") }
28 29 30

    delegate :name,
             :email,
31 32
             to: :author,
             prefix: true
33 34 35

    delegate :name,
             :email,
36 37 38
             to: :assignee,
             allow_nil: true,
             prefix: true
39 40 41 42

    attr_accessor :author_id_of_changes
  end

43 44
  module ClassMethods
    def search(query)
45
      where("title like :query", query: "%#{query}%")
46
    end
47 48 49 50 51 52 53 54 55
  end

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

  def new?
    today? && created_at == updated_at
  end
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

  def is_assigned?
    !!assignee_id
  end

  def is_being_reassigned?
    assignee_id_changed?
  end

  def is_being_closed?
    closed_changed? && closed
  end

  def is_being_reopened?
    closed_changed? && !closed
  end

73 74 75 76 77 78 79
  #
  # Votes
  #

  # Return the number of -1 comments (downvotes)
  def downvotes
    notes.select(&:downvote?).size
80 81
  end

82
  def downvotes_in_percent
83 84 85
    if votes_count.zero?
      0
    else
86
      100.0 - upvotes_in_percent
87 88 89
    end
  end

90 91 92
  # Return the number of +1 comments (upvotes)
  def upvotes
    notes.select(&:upvote?).size
93 94
  end

95
  def upvotes_in_percent
96 97 98
    if votes_count.zero?
      0
    else
99
      100.0 / votes_count * upvotes
100 101 102 103 104 105 106
    end
  end

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