BigW Consortium Gitlab

issue_commonality.rb 1.48 KB
Newer Older
1
# Contains common functionality shared between Issues and MergeRequests
2
module IssueCommonality
3 4 5 6
  extend ActiveSupport::Concern

  included do
    belongs_to :project
7 8
    belongs_to :author, class_name: "User"
    belongs_to :assignee, class_name: "User"
9
    belongs_to :milestone
10
    has_many :notes, as: :noteable, dependent: :destroy
11

Andrey Kumanyaev committed
12 13 14
    validates :project, presence: true
    validates :author, presence: true
    validates :title, presence: true, length: { within: 0..255 }
15
    validates :closed, inclusion: { in: [true, false] }
16

17 18
    scope :opened, where(closed: false)
    scope :closed, where(closed: true)
19
    scope :of_group, ->(group) { where(project_id: group.project_ids) }
Valeriy Sizov committed
20
    scope :assigned, ->(u) { where(assignee_id: u.id)}
21
    scope :recent, order("created_at DESC")
22 23 24

    delegate :name,
             :email,
25 26
             to: :author,
             prefix: true
27 28 29

    delegate :name,
             :email,
30 31 32
             to: :assignee,
             allow_nil: true,
             prefix: true
33 34 35 36

    attr_accessor :author_id_of_changes
  end

37 38
  module ClassMethods
    def search(query)
39
      where("title like :query", query: "%#{query}%")
40
    end
41 42 43 44 45 46 47 48 49
  end

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

  def new?
    today? && created_at == updated_at
  end
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

  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

67
end