BigW Consortium Gitlab

milestone.rb 1.95 KB
Newer Older
1 2 3 4 5 6 7 8 9
# == Schema Information
#
# Table name: milestones
#
#  id          :integer          not null, primary key
#  title       :string(255)      not null
#  project_id  :integer          not null
#  description :text
#  due_date    :date
Dmitriy Zaporozhets committed
10 11
#  created_at  :datetime
#  updated_at  :datetime
Dmitriy Zaporozhets committed
12
#  state       :string(255)
Dmitriy Zaporozhets committed
13
#  iid         :integer
14 15
#

16
class Milestone < ActiveRecord::Base
17 18 19 20
  # Represents a "No Milestone" state used for filtering Issues and Merge
  # Requests that have no milestone assigned.
  None = Struct.new(:title).new('No Milestone')

21
  include InternalId
22
  include Sortable
23

24 25
  belongs_to :project
  has_many :issues
26
  has_many :merge_requests
Andrey Kumanyaev committed
27
  has_many :participants, through: :issues, source: :assignee
28

29 30
  scope :active, -> { with_state(:active) }
  scope :closed, -> { with_state(:closed) }
31
  scope :of_projects, ->(ids) { where(project_id: ids) }
32

Andrey Kumanyaev committed
33 34
  validates :title, presence: true
  validates :project, presence: true
35

Andrew8xx8 committed
36
  state_machine :state, initial: :active do
37
    event :close do
Andrew8xx8 committed
38
      transition active: :closed
39 40 41
    end

    event :activate do
Andrew8xx8 committed
42
      transition closed: :active
43 44 45 46 47 48
    end

    state :closed

    state :active
  end
49

50 51
  def expired?
    if due_date
52
      due_date.past?
53 54 55
    else
      false
    end
56 57
  end

58 59
  def open_items_count
    self.issues.opened.count + self.merge_requests.opened.count
60 61
  end

62
  def closed_items_count
63
    self.issues.closed.count + self.merge_requests.closed_and_merged.count
64 65 66 67
  end

  def total_items_count
    self.issues.count + self.merge_requests.count
68 69 70
  end

  def percent_complete
71
    ((closed_items_count * 100) / total_items_count).abs
72
  rescue ZeroDivisionError
73
    0
74 75 76
  end

  def expires_at
77 78 79 80
    if due_date
      if due_date.past?
        "expired at #{due_date.stamp("Aug 21, 2011")}"
      else
81
        "expires at #{due_date.stamp("Aug 21, 2011")}"
82
      end
83
    end
84
  end
85 86

  def can_be_closed?
87
    active? && issues.opened.count.zero?
88 89 90 91
  end

  def is_empty?
    total_items_count.zero?
92 93
  end

94
  def author_id
95
    nil
96
  end
97
end