summaryrefslogtreecommitdiff
path: root/app/roles/issue_commonality.rb
blob: 55b46ec0c898e1fe245643a4321482b708a3ce32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Contains common functionality shared between Issues and MergeRequests
module IssueCommonality
  extend ActiveSupport::Concern

  included do
    belongs_to :project
    belongs_to :author, class_name: "User"
    belongs_to :assignee, class_name: "User"
    has_many :notes, as: :noteable, dependent: :destroy

    validates_presence_of :project_id
    validates_presence_of :author_id

    validates :title,
              presence: true,
              length: { within: 0..255 }
    validates :closed, inclusion: { in: [true, false] }

    scope :opened, where(closed: false)
    scope :closed, where(closed: true)
    scope :assigned, lambda { |u| where(assignee_id: u.id)}

    delegate :name,
             :email,
             to: :author,
             prefix: true

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

    attr_accessor :author_id_of_changes
  end

  module ClassMethods
    def search(query)
      where("title like :query", query: "%#{query}%")
    end
  end

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

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