46 lines
1.7 KiB
Ruby
46 lines
1.7 KiB
Ruby
# -*- encoding : utf-8 -*-
|
|
class Comment < ActiveRecord::Base
|
|
belongs_to :commentable, :polymorphic => true
|
|
belongs_to :user
|
|
attr_accessor :project
|
|
|
|
validates :body, :user_id, :commentable_id, :commentable_type, :presence => true
|
|
|
|
after_create :invoke_helper, :if => "commentable_type == 'Grit::Commit'"
|
|
after_create :subscribe_users
|
|
after_create {|c| Subscribe.new_comment_notification(c)}
|
|
before_save lambda {|c| c.commentable_id = c.commentable_id.to_s.hex if c.commit_comment? and c.commentable_id_changed?}
|
|
|
|
def helper
|
|
class_eval "def commentable; project.git_repository.commit('#{commentable_id}'); end" if commit_comment?
|
|
end
|
|
|
|
def own_comment?(user)
|
|
user_id == user.id
|
|
end
|
|
|
|
def commit_comment?
|
|
commentable_type == 'Grit::Commit'
|
|
end
|
|
|
|
protected
|
|
|
|
def invoke_helper
|
|
self.helper
|
|
end
|
|
|
|
def subscribe_users
|
|
if self.commentable.class == Issue
|
|
self.commentable.subscribes.create(:user => self.user) if !self.commentable.subscribes.exists?(:user_id => self.user.id)
|
|
elsif self.commentable.class == Grit::Commit
|
|
recipients = self.project.relations.by_role('admin').where(:object_type => 'User').map &:object # admins
|
|
recipients << self.user << User.where(:email => self.commentable.committer.email).first # commentor and committer
|
|
recipients << self.project.owner if self.project.owner_type == 'User' # project owner
|
|
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.subscribe_to_commit(options) if Subscribe.subscribed_to_commit?(self.project, user, self.commentable)
|
|
end
|
|
end
|
|
end
|
|
end
|