2011-12-19 15:30:14 +00:00
|
|
|
class Comment < ActiveRecord::Base
|
2011-12-20 10:20:00 +00:00
|
|
|
belongs_to :commentable, :polymorphic => true
|
|
|
|
belongs_to :user
|
2012-01-12 11:22:12 +00:00
|
|
|
attr_accessor :project
|
2011-12-19 15:30:14 +00:00
|
|
|
|
2011-12-20 10:20:00 +00:00
|
|
|
validates :body, :user_id, :commentable_id, :commentable_type, :presence => true
|
2011-12-26 15:48:57 +00:00
|
|
|
|
2012-01-20 15:17:05 +00:00
|
|
|
after_create :invoke_helper, :if => "commentable_type == 'Grit::Commit'"
|
2012-01-25 17:33:26 +00:00
|
|
|
after_create :subscribe_users
|
2012-01-21 13:32:22 +00:00
|
|
|
after_create {|comment| Subscribe.new_comment_notification(comment)}
|
2011-12-26 15:48:57 +00:00
|
|
|
|
2012-01-20 15:17:05 +00:00
|
|
|
def helper
|
|
|
|
class_eval "def commentable; project.git_repository.commit('#{commentable_id}'); end" if commentable_type == 'Grit::Commit'
|
|
|
|
end
|
2011-12-26 15:48:57 +00:00
|
|
|
|
2012-01-21 13:32:22 +00:00
|
|
|
def own_comment?(user)
|
|
|
|
user_id == user.id
|
2011-12-26 15:48:57 +00:00
|
|
|
end
|
2012-01-13 15:07:01 +00:00
|
|
|
|
2012-01-21 13:32:22 +00:00
|
|
|
protected
|
|
|
|
|
2012-01-20 15:17:05 +00:00
|
|
|
def invoke_helper
|
|
|
|
self.helper
|
|
|
|
end
|
2012-01-23 19:42:54 +00:00
|
|
|
|
|
|
|
def subscribe_users
|
2012-01-25 17:33:26 +00:00
|
|
|
if self.commentable.class == Issue
|
2012-01-29 20:18:14 +00:00
|
|
|
self.commentable.subscribes.create(:user => self.user) if !self.commentable.subscribes.exists?(:user_id => self.user.id)
|
2012-01-25 17:33:26 +00:00
|
|
|
elsif self.commentable.class == Grit::Commit
|
|
|
|
recipients = self.project.relations.by_role('admin').where(:object_type => 'User').map &:object # admins
|
2012-01-30 06:19:54 +00:00
|
|
|
recipients << self.user << User.where(:email => self.commentable.committer.email).first # commentor and committer
|
2012-01-25 17:33:26 +00:00
|
|
|
recipients << self.project.owner if self.project.owner_type == 'User' # project owner
|
2012-01-29 20:18:14 +00:00
|
|
|
recipients.compact.uniq.each do |user|
|
|
|
|
options = {:project_id => self.project.id, :subscribeable_id => self.commentable.id, :subscribeable_type => self.commentable.class.name, :user_id => user.id}
|
|
|
|
Subscribe.set_subscribe_to_commit(options, Subscribe::ON) if Subscribe.subscribed_to_commit?(self.project, user, self.commentable)
|
|
|
|
end
|
2012-01-25 17:33:26 +00:00
|
|
|
end
|
2012-01-23 19:42:54 +00:00
|
|
|
end
|
2011-12-19 15:30:14 +00:00
|
|
|
end
|