2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
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-03-06 21:49:29 +00:00
|
|
|
belongs_to :project
|
2011-12-19 15:30:14 +00:00
|
|
|
|
2012-03-27 16:09:04 +01:00
|
|
|
validates :body, :user_id, :commentable_id, :commentable_type, :project_id, :presence => true
|
2011-12-26 15:48:57 +00:00
|
|
|
|
2012-04-04 22:43:06 +01:00
|
|
|
scope :for_commit, lambda {|c| where(:commentable_id => c.id.hex, :commentable_type => c.class)}
|
2012-03-01 19:10:12 +00:00
|
|
|
default_scope order('created_at')
|
|
|
|
|
2012-03-02 23:38:43 +00:00
|
|
|
after_create :subscribe_on_reply, :unless => lambda {|c| c.commit_comment?}
|
2012-01-25 17:33:26 +00:00
|
|
|
after_create :subscribe_users
|
2012-04-04 22:43:06 +01:00
|
|
|
|
|
|
|
attr_accessible :body
|
|
|
|
|
|
|
|
def commentable
|
|
|
|
commit_comment? ? project.git_repository.commit(commentable_id.to_s(16)) : super
|
2012-01-20 15:17:05 +00:00
|
|
|
end
|
2011-12-26 15:48:57 +00:00
|
|
|
|
2012-04-04 22:43:06 +01:00
|
|
|
def commentable=(c)
|
|
|
|
if self.class.commit_comment?(c.class)
|
|
|
|
self.commentable_id = c.id.hex
|
|
|
|
self.commentable_type = c.class.name
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2012-03-27 16:09:04 +01:00
|
|
|
|
2012-04-04 22:43:06 +01:00
|
|
|
def self.commit_comment?(class_name)
|
|
|
|
class_name.to_s == 'Grit::Commit'
|
2011-12-26 15:48:57 +00:00
|
|
|
end
|
2012-01-13 15:07:01 +00:00
|
|
|
|
2012-02-14 16:05:41 +00:00
|
|
|
def commit_comment?
|
2012-04-04 22:43:06 +01:00
|
|
|
self.class.commit_comment?(commentable_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.issue_comment?(class_name)
|
|
|
|
class_name.to_s == 'Issue'
|
|
|
|
end
|
|
|
|
|
|
|
|
def issue_comment?
|
|
|
|
self.class.issue_comment?(commentable_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def own_comment?(user)
|
|
|
|
user_id == user.id
|
2012-02-14 16:05:41 +00:00
|
|
|
end
|
|
|
|
|
2012-01-25 08:31:49 +00:00
|
|
|
def can_notify_on_new_comment?(subscribe)
|
|
|
|
User.find(subscribe.user).notifier.new_comment && User.find(subscribe.user).notifier.can_notify
|
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-13 15:07:01 +00:00
|
|
|
def subscribe_on_reply
|
2012-04-04 22:43:06 +01:00
|
|
|
commentable.subscribes.create(:user_id => user_id) if !commentable.subscribes.exists?(:user_id => user_id)
|
2012-01-13 15:07:01 +00:00
|
|
|
end
|
2012-02-06 15:46:32 +00:00
|
|
|
|
2012-01-23 19:42:54 +00:00
|
|
|
def subscribe_users
|
2012-04-04 22:43:06 +01:00
|
|
|
if issue_comment?
|
|
|
|
commentable.subscribes.create(:user => user) if !commentable.subscribes.exists?(:user_id => user.id)
|
|
|
|
elsif commit_comment?
|
|
|
|
recipients = project.relations.by_role('admin').where(:object_type => 'User').map &:object # admins
|
|
|
|
recipients << user << User.where(:email => commentable.committer.email).first # commentor and committer
|
|
|
|
recipients << project.owner if project.owner_type == 'User' # project owner
|
2012-01-29 20:18:14 +00:00
|
|
|
recipients.compact.uniq.each do |user|
|
2012-04-04 22:43:06 +01:00
|
|
|
options = {:project_id => project.id, :subscribeable_id => commentable_id, :subscribeable_type => commentable.class.name, :user_id => user.id}
|
|
|
|
Subscribe.subscribe_to_commit(options) if Subscribe.subscribed_to_commit?(project, user, commentable)
|
2012-01-29 20:18:14 +00:00
|
|
|
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
|