issue.rb 1.91 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
class Issue < ActiveRecord::Base
2 3
  include Upvote

gitlabhq's avatar
gitlabhq committed
4
  belongs_to :project
5
  belongs_to :milestone
gitlabhq's avatar
gitlabhq committed
6 7
  belongs_to :author, :class_name => "User"
  belongs_to :assignee, :class_name => "User"
8
  has_many :notes, :as => :noteable, :dependent => :destroy
gitlabhq's avatar
gitlabhq committed
9 10

  attr_protected :author, :author_id, :project, :project_id
11
  attr_accessor :author_id_of_changes
gitlabhq's avatar
gitlabhq committed
12 13 14 15 16

  validates_presence_of :project_id
  validates_presence_of :assignee_id
  validates_presence_of :author_id

17
  delegate :name,
18 19
           :email,
           :to => :author,
20 21
           :prefix => true

gitlabhq's avatar
gitlabhq committed
22 23 24 25 26
  delegate :name,
           :email,
           :to => :assignee,
           :prefix => true

gitlabhq's avatar
gitlabhq committed
27 28 29
  validates :title,
            :presence => true,
            :length   => { :within => 0..255 }
30

Steven Verbeek's avatar
Steven Verbeek committed
31 32
  validates :description,
            :length   => { :within => 0..2000 }
Nihad Abbasov's avatar
Nihad Abbasov committed
33

gitlabhq's avatar
gitlabhq committed
34 35 36
  scope :critical, where(:critical => true)
  scope :non_critical, where(:critical => false)

gitlabhq's avatar
gitlabhq committed
37 38 39
  scope :opened, where(:closed => false)
  scope :closed, where(:closed => true)
  scope :assigned, lambda { |u| where(:assignee_id => u.id)}
VSizov's avatar
VSizov committed
40 41

  acts_as_list
gitlabhq's avatar
gitlabhq committed
42

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
43 44 45 46
  def self.open_for(user)
    opened.assigned(user)
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
47 48 49 50
  def self.search query
    where("title like :query", :query => "%#{query}%")
  end

gitlabhq's avatar
gitlabhq committed
51 52 53
  def today?
    Date.today == created_at.to_date
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
54

gitlabhq's avatar
gitlabhq committed
55 56 57
  def new?
    today? && created_at == updated_at
  end
58 59 60 61 62 63 64 65 66

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

  def is_being_reassigned?
    assignee_id_changed?
  end
gitlabhq's avatar
gitlabhq committed
67 68 69 70 71 72 73
end
# == Schema Information
#
# Table name: issues
#
#  id          :integer         not null, primary key
#  title       :string(255)
Steven Verbeek's avatar
Steven Verbeek committed
74
#  description :text
gitlabhq's avatar
gitlabhq committed
75 76 77 78 79 80
#  assignee_id :integer
#  author_id   :integer
#  project_id  :integer
#  created_at  :datetime
#  updated_at  :datetime
#  closed      :boolean         default(FALSE), not null
gitlabhq's avatar
gitlabhq committed
81
#  position    :integer         default(0)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
82
#  critical    :boolean         default(FALSE), not null
Valery Sizov's avatar
Valery Sizov committed
83
#  branch_name :string(255)
gitlabhq's avatar
gitlabhq committed
84 85
#