[refs #114] changed rules

This commit is contained in:
Alexander Machehin 2012-01-22 01:13:33 +06:00
parent 21d422c313
commit 36a895385c
5 changed files with 121 additions and 56 deletions

View File

@ -17,6 +17,7 @@ class Comment < ActiveRecord::Base
user_id == user.id
end
protected
def subscribe_on_reply
@ -27,11 +28,4 @@ class Comment < ActiveRecord::Base
def invoke_helper
self.helper
end
def subscribe_committer
committer = User.where(:email => self.commentable.committer.email).first
if committer && !self.project.commit_comments_subscribes.exists?(:user_id => committer.id) && committer.notifier.new_comment_commit_owner
self.project.commit_comments_subscribes.create(:user_id => committer.id)
end
end
end

View File

@ -31,7 +31,7 @@ class Project < ActiveRecord::Base
after_create :attach_to_personal_repository
after_create :create_git_repo
after_create :subscribe_users
after_create :subscribe_owner
after_destroy :destroy_git_repo
# after_rollback lambda { destroy_git_repo rescue true if new_record? }
@ -161,7 +161,7 @@ class Project < ActiveRecord::Base
FileUtils.rm_rf path
end
def subscribe_users
self.commit_comments_subscribes.create(:user_id => self.owner.id)
def subscribe_owner
Subscribe.subscribe_user(self, self.owner.id)
end
end

View File

@ -12,6 +12,8 @@ class Relation < ActiveRecord::Base
scope :by_target, lambda {|tar| {:conditions => ['target_id = ? AND target_type = ?', tar.id, tar.class.to_s]}}
scope :by_role, lambda {|role| {:conditions => ['role = ?', role]}}
after_create :subscribe_project_admin, :if => "role == 'admin' && object_id == 'User' && targer_type == 'Project'"
def self.create_with_role(object, target, role)
r = new
r.object = object
@ -24,4 +26,8 @@ class Relation < ActiveRecord::Base
def add_default_role
self.role = ROLES.first if role.nil? || role.empty?
end
def subscribe_project_admin
Subscribe.subscribe_user(self.target_id, self.object_id)
end
end

View File

@ -6,22 +6,21 @@ class Subscribe < ActiveRecord::Base
commentable_class = comment.commentable.class
subscribes = comment.commentable.subscribes if commentable_class == Issue
if commentable_class == Grit::Commit
Subscribe.subscribe_committer(comment)
subscribes = comment.project.commit_comments_subscribes(true) # FIXME (true) for rspec
committer = User.where(:email => comment.commentable.committer.email).first
Subscribe.send_notification(comment, committer) if committer && committer.notifier.new_comment_commit_owner && subscribes.where(:user_id => committer).empty?
end
subscribes.each do |subscribe|
user = subscribe.user
next if comment.own_comment?(user) || !user.notifier.can_notify
Subscribe.send_notification(comment, user) if commentable_class == Issue && user.notifier.new_comment_reply
Subscribe.send_notification(comment, user) if commentable_class == Grit::Commit && user.notifier.new_comment_commit_repo_owner
user = subscribe.user
next if comment.own_comment?(user) || !user.notifier.can_notify
Subscribe.send_notification(comment, user) if commentable_class == Issue && user.notifier.new_comment_reply
Subscribe.send_notification(comment, user) if commentable_class == Grit::Commit && Subscribe.send_notification_for_commit_comment?(subscribe.subscribeable, user, comment)
end
end
def self.subscribe_committer(comment)
committer = User.where(:email => comment.commentable.committer.email).first
if committer && !comment.project.commit_comments_subscribes.exists?(:user_id => committer.id) && committer.notifier.new_comment_commit_owner
comment.project.commit_comments_subscribes.create(:user_id => committer.id)
end
def self.subscribe_user(project_id, user_id)
list = Project.find(project_id).commit_comments_subscribes
list.create(:user_id => user_id) unless list.exists?(:user_id => user_id)
end
def self.send_notification(comment, user)
@ -31,4 +30,10 @@ class Subscribe < ActiveRecord::Base
UserMailer.delay.new_comment_notification(comment, user)
end
end
def self.send_notification_for_commit_comment?(project, user, comment)
is_owner = (project.owner_id == user.id)
is_commentor = (project.commit_comments_subscribes.exists?(:user_id => user.id))
(is_owner && user.notifier.new_comment_commit_repo_owner) or (is_commentor && user.notifier.new_comment_commit_commentor)
end
end

View File

@ -74,8 +74,8 @@ describe Comment do
@ability.should_not be_able_to(:destroy, @comment)
end
context 'for default enabled settings' do
it 'should send an e-mail by default settings' do
context 'for default settings' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
@ -84,23 +84,58 @@ describe Comment do
end
end
context 'for disabled notify setting in project' do
context 'for disabled notify setting new_comment_commit_repo_owner' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_repo_owner, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1 # cache project.commit_comments_subscribes ...
ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true
end
end
context 'for disabled notify setting new_comment_commit_owner' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_owner, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1 # cache project.commit_comments_subscribes ...
ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true
end
end
context 'for disabled notify setting new_comment_commit_commentor' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_commentor, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1 # cache project.commit_comments_subscribes ...
ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true
end
end
context 'for disabled all notify setting expect global' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@project.commit_comments_subscribes.where(:user_id => @user).first.destroy # FIXME
@user.notifier.update_attribute :new_comment_commit_repo_owner, false
@user.notifier.update_attribute :new_comment_commit_owner, false
@user.notifier.update_attribute :new_comment_commit_commentor, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0 # cache project.commit_comments_subscribes ...
end
end
context 'for disabled notify setting' do
context 'for unsubscribe project' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_repo_owner, false
@project.commit_comments_subscribes.where(:user_id => @user).first.destroy # FIXME
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0
ActionMailer::Base.deliveries.count.should == 0 # cache project.commit_comments_subscribes ...
end
end
@ -153,7 +188,52 @@ describe Comment do
end
end
context 'for disabled notify setting in project' do
context 'for disabled notify setting new_comment_commit_repo_owner' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_repo_owner, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1 # cache project.commit_comments_subscribes ...
ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true
end
end
context 'for disabled notify setting new_comment_commit_owner' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_owner, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1 # cache project.commit_comments_subscribes ...
ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true
end
end
context 'for disabled notify setting new_comment_commit_commentor' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_commentor, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1 # cache project.commit_comments_subscribes ...
ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true
end
end
context 'for disabled all notify setting expect global' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@user.notifier.update_attribute :new_comment_commit_repo_owner, false
@user.notifier.update_attribute :new_comment_commit_owner, false
@user.notifier.update_attribute :new_comment_commit_commentor, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0 # cache project.commit_comments_subscribes ...
end
end
context 'for unsubscribe project' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@project.commit_comments_subscribes.where(:user_id => @user).first.destroy # FIXME
@ -163,20 +243,10 @@ describe Comment do
end
end
context 'for disabled notify setting' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@project.owner.notifier.update_attribute :new_comment_commit_repo_owner, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0
end
end
context 'for disabled global notify setting' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
@project.owner.notifier.update_attribute :can_notify, false
@user.notifier.update_attribute :can_notify, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0
@ -189,19 +259,9 @@ describe Comment do
@project.owner.update_attribute :email, 'code@tpope.net'
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1
ActionMailer::Base.deliveries.last.to.include?(@user.email).should == true
end
it 'should not send an e-mail if disable comments of own commit' do
ActionMailer::Base.deliveries = []
@project.owner.update_attribute :email, 'code@tpope.net'
# what setting is first-priority?
@project.owner.notifier.update_attribute :new_comment_commit_repo_owner, false
@project.owner.notifier.update_attribute :new_comment_commit_owner, false
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0
ActionMailer::Base.deliveries.count.should == 1
ActionMailer::Base.deliveries.last.to.include?(@project.owner.email).should == true
end
end
@ -267,12 +327,13 @@ describe Comment do
end
end
end
context 'for subscribe in project' do
it 'should send an e-mail' do
ActionMailer::Base.deliveries = []
@project.owner.notifier.update_attribute :can_notify, false
@stranger.notifier.update_attribute :new_comment_commit_repo_owner, false
@stranger.notifier.update_attribute :new_comment_commit_owner, false
#@stranger.notifier.update_attribute :new_comment_commit_commentor, false
@project.commit_comments_subscribes.create(:user_id => @stranger.id)
comment = Comment.create(:user => @project.owner, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
@ -282,13 +343,12 @@ describe Comment do
it 'should not send an e-mail for own comment' do
ActionMailer::Base.deliveries = []
@project.owner.notifier.update_attribute :can_notify, false
#@project.owner.notifier.update_attribute :can_notify, false
@project.commit_comments_subscribes.create(:user_id => @stranger.id)
comment = Comment.create(:user => @owner, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0
end
end
end
end