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-02-19 17:27:47 +00:00
|
|
|
belongs_to :creator, :class_name => 'User', :foreign_key => 'creator_id'
|
2011-12-19 15:30:14 +00:00
|
|
|
|
2012-02-14 16:05:41 +00:00
|
|
|
has_many :comments, :as => :commentable, :dependent => :destroy #, :finder_sql => proc { "comments.commentable_id = '#{self.id}' AND comments.commentable_type = '#{self.class.name}'"}
|
|
|
|
has_many :subscribes, :as => :subscribeable, :dependent => :destroy #, :finder_sql => proc { "subscribes.subscribeable_id = '#{self.id}' AND subscribes.subscribeable_type = '#{self.class.name}'"}
|
2012-02-21 20:28:04 +00:00
|
|
|
has_many :labels, :through => :labelings
|
|
|
|
has_many :labelings
|
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
|
|
|
|
2011-12-20 16:57:34 +00:00
|
|
|
#attr_readonly :serial_id
|
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
|
|
|
|
after_create :deliver_new_issue_notification
|
|
|
|
after_create :deliver_issue_assign_notification
|
|
|
|
after_update :deliver_issue_assign_notification
|
2012-01-12 13:07:54 +00:00
|
|
|
after_update :subscribe_issue_assigned_user
|
2011-12-19 15:30:14 +00:00
|
|
|
|
2012-02-27 16:10:12 +00:00
|
|
|
attr_accessible :labelings_attributes, :title, :body, :project, :project_id
|
|
|
|
accepts_nested_attributes_for :labelings, :allow_destroy => true
|
|
|
|
|
2011-12-27 17:49:08 +00:00
|
|
|
def assign_uname
|
|
|
|
user.uname if user
|
|
|
|
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
|
|
|
|
|
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 deliver_new_issue_notification
|
|
|
|
recipients = collect_recipient_ids
|
|
|
|
recipients.each do |recipient_id|
|
|
|
|
recipient = User.find(recipient_id)
|
2012-01-13 15:07:01 +00:00
|
|
|
UserMailer.delay.new_issue_notification(self, recipient) if User.find(recipient).notifier.can_notify && User.find(recipient).notifier.new_issue
|
2011-12-26 15:48:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def deliver_issue_assign_notification
|
2012-01-13 15:07:01 +00:00
|
|
|
UserMailer.delay.issue_assign_notification(self, self.user) if self.user_id_was != self.user_id && self.user.notifier.issue_assign && self.user.notifier.can_notify
|
2011-12-26 15:48:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def subscribe_users
|
|
|
|
recipients = collect_recipient_ids
|
|
|
|
recipients.each do |recipient_id|
|
|
|
|
ss = self.subscribes.build(:user_id => recipient_id)
|
|
|
|
ss.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def collect_recipient_ids
|
|
|
|
recipients = self.project.relations.by_role('admin').where(:object_type => 'User').map { |rel| rel.read_attribute(:object_id) }
|
2011-12-27 17:49:08 +00:00
|
|
|
recipients = recipients | [self.user_id] if self.user_id
|
2011-12-26 15:48:57 +00:00
|
|
|
recipients = recipients | [self.project.owner_id] if self.project.owner_type == 'User'
|
2012-01-11 13:58:13 +00:00
|
|
|
|
|
|
|
# filter by notification settings
|
|
|
|
recipients = recipients.select do |recipient|
|
2012-01-13 15:07:01 +00:00
|
|
|
User.find(recipient).notifier.new_issue && User.find(recipient).notifier.can_notify
|
2012-01-11 13:58:13 +00:00
|
|
|
end
|
|
|
|
|
2011-12-26 15:48:57 +00:00
|
|
|
recipients
|
|
|
|
end
|
|
|
|
|
2012-01-12 13:07:54 +00:00
|
|
|
def subscribe_issue_assigned_user
|
|
|
|
if self.user_id_was != self.user_id
|
2012-01-13 15:07:01 +00:00
|
|
|
self.subscribes.where(:user_id => self.user_id_was).first.destroy unless self.user_id_was.blank?
|
2012-01-12 13:07:54 +00:00
|
|
|
if self.user.notifier.issue_assign && !self.subscribes.exists?(:user_id => self.user_id)
|
2012-01-13 15:07:01 +00:00
|
|
|
self.subscribes.create(:user_id => self.user_id)
|
2012-01-12 13:07:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|