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
|
|
|
|
|
|
|
|
has_many :comments, :as => :commentable
|
2011-12-26 15:48:57 +00:00
|
|
|
has_many :subscribes, :as => :subscribeable
|
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
|
2011-12-19 15:30:14 +00:00
|
|
|
|
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
|
|
|
|
|
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)
|
2011-12-28 10:47:54 +00:00
|
|
|
UserMailer.delay.new_issue_notification(self, recipient)#.deliver
|
2011-12-26 15:48:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def deliver_issue_assign_notification
|
2012-01-11 13:58:13 +00:00
|
|
|
UserMailer.delay.issue_assign_notification(self, self.user) if self.user_id_was != self.user_id && self.user.notifier.issue_assign
|
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|
|
|
|
|
User.find(recipient).notifier.new_issue
|
|
|
|
end
|
|
|
|
|
2011-12-26 15:48:57 +00:00
|
|
|
recipients
|
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|