2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2011-12-19 15:30:14 +00:00
|
|
|
class Issue < ActiveRecord::Base
|
2011-12-21 14:48:16 +00:00
|
|
|
STATUSES = ['open', 'closed']
|
2011-12-19 15:30:14 +00:00
|
|
|
|
|
|
|
belongs_to :project
|
|
|
|
belongs_to :user
|
2012-04-13 20:44:04 +01:00
|
|
|
belongs_to :assignee, :class_name => 'User', :foreign_key => 'assignee_id'
|
2012-02-28 14:05:18 +00:00
|
|
|
belongs_to :closer, :class_name => 'User', :foreign_key => 'closed_by'
|
2011-12-19 15:30:14 +00:00
|
|
|
|
2012-04-13 20:44:04 +01:00
|
|
|
has_many :comments, :as => :commentable, :dependent => :destroy
|
|
|
|
has_many :subscribes, :as => :subscribeable, :dependent => :destroy
|
2012-04-19 20:45:50 +01:00
|
|
|
has_many :labelings, :dependent => :destroy
|
2012-03-06 15:20:29 +00:00
|
|
|
has_many :labels, :through => :labelings, :uniq => true
|
2011-12-19 15:30:14 +00:00
|
|
|
|
2011-12-27 17:49:08 +00:00
|
|
|
validates :title, :body, :project_id, :presence => true
|
2011-12-19 15:30:14 +00:00
|
|
|
|
|
|
|
after_create :set_serial_id
|
2011-12-26 15:48:57 +00:00
|
|
|
after_create :subscribe_users
|
2012-01-12 13:07:54 +00:00
|
|
|
after_update :subscribe_issue_assigned_user
|
2011-12-19 15:30:14 +00:00
|
|
|
|
2012-04-13 20:44:04 +01:00
|
|
|
attr_accessible :labelings_attributes, :title, :body, :assignee_id
|
2012-02-27 16:10:12 +00:00
|
|
|
accepts_nested_attributes_for :labelings, :allow_destroy => true
|
|
|
|
|
2012-04-20 18:19:23 +01:00
|
|
|
scope :opened, where(:state => 'open')
|
|
|
|
scope :closed, where(:state => 'closed')
|
2012-02-28 14:28:11 +00:00
|
|
|
|
2011-12-27 17:49:08 +00:00
|
|
|
def assign_uname
|
2012-04-13 20:44:04 +01:00
|
|
|
assignee.uname if assignee
|
2011-12-27 17:49:08 +00:00
|
|
|
end
|
|
|
|
|
2011-12-28 02:57:42 +00:00
|
|
|
def to_param
|
|
|
|
serial_id.to_s
|
|
|
|
end
|
|
|
|
|
2012-01-13 15:07:01 +00:00
|
|
|
def subscribe_creator(creator_id)
|
|
|
|
if !self.subscribes.exists?(:user_id => creator_id)
|
|
|
|
self.subscribes.create(:user_id => creator_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-28 14:05:18 +00:00
|
|
|
def closed?
|
2012-04-20 18:19:23 +01:00
|
|
|
closed_by && closed_at && state == 'closed'
|
2012-02-28 14:05:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_close(closed_by)
|
|
|
|
self.closed_at = Time.now
|
|
|
|
self.closer = closed_by
|
2012-04-20 18:19:23 +01:00
|
|
|
self.state = 'closed'
|
2012-02-28 14:05:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_open
|
|
|
|
self.closed_at = self.closed_by = nil
|
2012-04-20 18:19:23 +01:00
|
|
|
self.state = 'open'
|
2012-02-28 14:05:18 +00:00
|
|
|
end
|
|
|
|
|
2012-01-27 12:06:04 +00:00
|
|
|
def collect_recipient_ids
|
2012-04-26 02:38:33 +01:00
|
|
|
recipients = self.project.relations.by_role('admin').where(:actor_type => 'User').map { |rel| rel.read_attribute(:actor_id) }
|
2012-04-13 20:44:04 +01:00
|
|
|
recipients = recipients | [self.assignee_id] if self.assignee_id
|
2012-01-27 12:06:04 +00:00
|
|
|
recipients = recipients | [self.project.owner_id] if self.project.owner_type == 'User'
|
|
|
|
|
|
|
|
recipients
|
|
|
|
end
|
|
|
|
|
2011-12-19 15:30:14 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def set_serial_id
|
2011-12-20 16:57:34 +00:00
|
|
|
self.serial_id = self.project.issues.count
|
|
|
|
self.save!
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|
2011-12-26 15:48:57 +00:00
|
|
|
|
|
|
|
def subscribe_users
|
|
|
|
recipients = collect_recipient_ids
|
|
|
|
recipients.each do |recipient_id|
|
2012-03-13 13:56:58 +00:00
|
|
|
if User.find(recipient_id).notifier.new_comment && !self.subscribes.exists?(:user_id => recipient_id)
|
|
|
|
ss = self.subscribes.create(:user_id => recipient_id)
|
|
|
|
end
|
2011-12-26 15:48:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-12 13:07:54 +00:00
|
|
|
def subscribe_issue_assigned_user
|
2012-04-13 20:44:04 +01:00
|
|
|
if self.assignee_id && self.assignee_id_changed?
|
|
|
|
self.subscribes.where(:user_id => self.assignee_id_was).first.destroy unless self.assignee_id_was.blank?
|
|
|
|
if self.assignee.notifier.issue_assign && !self.subscribes.exists?(:user_id => self.assignee_id)
|
|
|
|
self.subscribes.create(:user_id => self.assignee_id)
|
2012-01-12 13:07:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-02-28 14:05:18 +00:00
|
|
|
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|