[refs #114] changed logics

This commit is contained in:
Alexander Machehin 2012-01-24 01:42:54 +06:00
parent 651c5637ed
commit 9127b6b689
14 changed files with 125 additions and 58 deletions

View File

@ -0,0 +1,31 @@
class CommitSubscribesController < ApplicationController
before_filter :authenticate_user!
load_resource :subscribe
load_and_authorize_resource :project
before_filter :find_commit
def create
if Subscribe.set_subscribe(@project, @commit, current_user.id, 1)
flash[:notice] = I18n.t("flash.subscribe.saved")
# TODO js
redirect_to commit_path(@project, @commit)
else
flash[:error] = I18n.t("flash.subscribe.saved_error")
redirect_to commit_path(@project, @commit)
end
end
def destroy
Subscribe.set_subscribe(@project, @commit, current_user.id, 0)
flash[:notice] = t("flash.subscribe.destroyed")
redirect_to commit_path(@project, @commit)
end
protected
def find_commit
@commit = @project.git_repository.commit(params[:commit_id])
end
end

View File

@ -1,24 +0,0 @@
class ProjectSubscribesController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource :project
load_resource :subscribe
#load_and_authorize_resource :subscribe, :find_by => :user_id
def create
@subscribe = @project.commit_comments_subscribes.build(:user_id => current_user.id)
if @subscribe.save
flash[:notice] = I18n.t("flash.subscribe.saved")
redirect_to @project
else
flash[:error] = I18n.t("flash.subscribe.saved_error")
redirect_to @project
end
end
def destroy
@project.commit_comments_subscribes.where(:user_id => current_user.id).first.destroy # FIXME
flash[:notice] = t("flash.subscribe.destroyed")
redirect_to @project
end
end

View File

@ -7,6 +7,7 @@ class Comment < ActiveRecord::Base
after_create :invoke_helper, :if => "commentable_type == 'Grit::Commit'"
after_create :subscribe_on_reply
after_create :subscribe_users, :if => "commentable_type == 'Grit::Commit'"
after_create {|comment| Subscribe.new_comment_notification(comment)}
def helper
@ -22,10 +23,18 @@ class Comment < ActiveRecord::Base
def subscribe_on_reply
self.commentable.subscribes.create(:user_id => self.user_id) if self.commentable_type == 'Issue' && !self.commentable.subscribes.exists?(:user_id => self.user_id)
self.project.commit_comments_subscribes.create(:user_id => self.user_id) if self.commentable_type == 'Grit::Commit' && !self.project.commit_comments_subscribes.exists?(:user_id => self.user_id)
Subscribe.subscribe_user_to_commit(self, self.user_id) if self.commentable_type == 'Grit::Commit'
end
def invoke_helper
self.helper
end
def subscribe_users
recipients = self.project.relations.by_role('admin').where(:object_type => 'User').map { |rel| rel.read_attribute(:object_id) }
committer = User.where(:email => self.commentable.committer.email).first
recipients = recipients | [committer.id] if committer
recipients = recipients | [self.project.owner_id] if self.project.owner_type == 'User'
recipients.each {|user| Subscribe.subscribe_user_to_commit(self, user)}
end
end

View File

@ -11,7 +11,9 @@ class Issue < ActiveRecord::Base
#'WHERE comments.commentable_id = \'#{self.id}\' ' +
#' AND comments.commentable_type = \'#{self.class.name}\' ' +
#'ORDER BY comments.created_at'
has_many :subscribes, :as => :subscribeable
has_many :subscribes, :as => :subscribeable,
:finder_sql => proc { "subscribes.subscribeable_id = '#{self.id}' " +
" AND subscribes.subscribeable_type = '#{self.class.name}'"}
validates :title, :body, :project_id, :presence => true

View File

@ -14,7 +14,6 @@ class Project < ActiveRecord::Base
has_many :relations, :as => :target, :dependent => :destroy
has_many :collaborators, :through => :relations, :source => :object, :source_type => 'User'
has_many :groups, :through => :relations, :source => :object, :source_type => 'Group'
has_many :commit_comments_subscribes, :as => :subscribeable, :class_name => 'Subscribe'
validates :name, :uniqueness => {:scope => [:owner_id, :owner_type]}, :presence => true, :format => { :with => /^[a-zA-Z0-9_\-\+\.]+$/ }
validates :owner, :presence => true
@ -31,7 +30,6 @@ class Project < ActiveRecord::Base
after_create :attach_to_personal_repository
after_create :create_git_repo
after_create :subscribe_owner
after_destroy :destroy_git_repo
# after_rollback lambda { destroy_git_repo rescue true if new_record? }
@ -161,7 +159,4 @@ class Project < ActiveRecord::Base
FileUtils.rm_rf path
end
def subscribe_owner
Subscribe.subscribe_user(self, self.owner.id)
end
end

View File

@ -12,8 +12,6 @@ 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
@ -26,8 +24,4 @@ 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

@ -1,31 +1,49 @@
class Subscribe < ActiveRecord::Base
belongs_to :subscribeable, :polymorphic => true
belongs_to :user
belongs_to :project
validates :status, :inclusion => {:in => 0..1}
scope :subscribed, where(:status => 1)
scope :unsubscribed, where(:status => 0)
def self.new_comment_notification(comment)
commentable_class = comment.commentable.class
subscribes = comment.commentable.subscribes if commentable_class == Issue
if commentable_class == Grit::Commit
subscribes = comment.project.commit_comments_subscribes(true) # FIXME (true) for rspec
committer = User.where(:email => comment.commentable.committer.email).first
UserMailer.delay.new_comment_notification(comment, committer) if committer && !comment.own_comment?(committer) && committer.notifier.new_comment_commit_owner && !committer.notifier.can_notify && subscribes.where(:user_id => committer).empty?
subscribes = Subscribe.where(:subscribeable_id => comment.commentable.id, :subscribeable_type => comment.commentable.class.name.to_s, :project_id => comment.project).subscribed(true) # FIXME (true) for rspec
end
subscribes.each do |subscribe|
user = subscribe.user
next if comment.own_comment?(user) || !user.notifier.can_notify
UserMailer.delay.new_comment_notification(comment, user) if commentable_class == Issue && user.notifier.new_comment_reply
UserMailer.delay.new_comment_notification(comment, user) if commentable_class == Grit::Commit && Subscribe.send_notification_for_commit_comment?(subscribe.subscribeable, user, comment)
UserMailer.delay.new_comment_notification(comment, user) if commentable_class == Grit::Commit
end
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)
def self.subscribe_user_to_commit(comment, user_id)
subscribe = Subscribe.where(:subscribeable_id => comment.commentable.id, :subscribeable_type => comment.commentable.class.name, :project_id => comment.project).unsubscribed.first
subscribe.update_attribute(:status, 1) if subscribe
Subscribe.create(:subscribeable_id => comment.commentable.id, :subscribeable_type => comment.commentable.class.name.to_s, :user_id => user_id, :project_id => comment.project, :status => 1) unless subscribe
end
def self.send_notification_for_commit_comment?(project, user, comment)
def self.subscribed_for_commit?(project, user, commentable)
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)
is_commentor = (Comment.where(:commentable_type => commentable.class.name, :commentable_id => commentable.id).exists?(:user_id => user.id))
is_committer = (user.email == commentable.committer.email)
(is_owner && user.notifier.new_comment_commit_repo_owner) or (is_commentor && user.notifier.new_comment_commit_commentor) or (is_committer && committer.notifier.new_comment_commit_owner)
end
def self.set_subscribe(project, commit, user, status)
# FIXME maybe?
subscribe = Subscribe.where(:subscribeable_id => commit.id, :subscribeable_type => commit.class.name.to_s,
:user_id => user, :project_id => project).first
if subscribe
subscribe.update_attribute(:status, status)
else
Subscribe.create(:subscribeable_id => commit.id, :subscribeable_type => commit.class.name.to_s,
:user_id => user, :project_id => project, :status => status)
end
end
end

View File

@ -28,3 +28,12 @@
- content_for :sidebar, render(:partial => 'git/shared/sidebar')
= render :partial => "comments/list", :locals => {:list => Project.commit_comments(@commit, @project), :project => @project, :commentable => @commit}
%p
%b
= t('layout.issues.subscribe')
\:
- subscribe = Subscribe.where(:subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name.to_s, :project_id => @project, :user_id => current_user.id).first
- if subscribe.try(:status) == 1 or (subscribe.nil? && Subscribe.subscribed_for_commit?(@project, current_user, @commit))
= link_to t('layout.issues.unsubscribe_btn'), unsubscribe_commit_path(@project, @commit), :method => :delete
- else
= link_to t('layout.issues.subscribe_btn'), subscribe_commit_path(@project, @commit), :method => :post

View File

@ -30,14 +30,6 @@
= t("activerecord.attributes.project.repository")
\:
= git_repo_url @project.git_repo_name
%p
%b
= t('layout.issues.subscribe')
\:
- if @project.commit_comments_subscribes.exists? :user_id => current_user.id
= link_to t('layout.issues.unsubscribe_btn'), project_subscribe_path(@project, current_user.id), :method => :delete
- else
= link_to t('layout.issues.subscribe_btn'), project_subscribes_path(@project), :method => :post
.wat-cf
= link_to image_tag("web-app-theme/icons/application_edit.png", :alt => t("layout.edit")) + " " + t("layout.edit"), edit_project_path(@project), :class => "button" if can? :update, @project
= link_to image_tag("web-app-theme/icons/cross.png", :alt => t("layout.delete")) + " " + t("layout.delete"), project_path(@project), :method => "delete", :class => "button", :confirm => t("layout.projects.confirm_delete") if can? :destroy, @project

View File

@ -88,7 +88,6 @@ Rosa::Application.routes.draw do
end
resource :repo, :controller => "git/repositories", :only => [:show]
resources :build_lists, :only => [:index, :new, :create]
resources :subscribes, :only => [:create, :destroy], :controller => 'project_subscribes'
resources :collaborators, :only => [:index, :edit, :update, :add] do
collection do
@ -156,6 +155,9 @@ Rosa::Application.routes.draw do
match '/projects/:project_id/git/commit/:commit_id/comments/:id(.:format)', :controller => "comments", :action => :update, :as => :project_commit_comment, :via => :put
match '/projects/:project_id/git/commit/:commit_id/comments/:id(.:format)', :controller => "comments", :action => :destroy, :via => :delete
match '/projects/:project_id/git/commit/:commit_id/comments(.:format)', :controller => "comments", :action => :create, :as => :project_commit_comments, :via => :post
# Commits subscribe
match '/projects/:project_id/git/commit/:commit_id/subscribe', :controller => "commit_subscribes", :action => :create, :defaults => { :format => :html }, :as => :subscribe_commit, :via => :post
match '/projects/:project_id/git/commit/:commit_id/unsubscribe', :controller => "commit_subscribes", :action => :destroy, :defaults => { :format => :html }, :as => :unsubscribe_commit, :via => :delete
# Blobs
match '/projects/:project_id/git/blob/:treeish/*path', :controller => "git/blobs", :action => :show, :treeish => /[0-9a-zA-Z_.\-]*/, :defaults => { :treeish => :master }, :as => :blob
match '/projects/:project_id/git/commit/blob/:commit_hash/*path', :controller => "git/blobs", :action => :show, :project_name => /[0-9a-zA-Z_.\-]*/, :as => :blob_commit

View File

@ -0,0 +1,9 @@
class AddStatusToSubscribe < ActiveRecord::Migration
def self.up
add_column :subscribes, :status, :integer, :default => 1
end
def self.down
remove_column :subscribes, :status
end
end

View File

@ -0,0 +1,9 @@
class AddProjectToSubscribe < ActiveRecord::Migration
def self.up
add_column :subscribes, :project_id, :integer
end
def self.down
remove_column :subscribes, :project_id
end
end

View File

@ -0,0 +1,9 @@
class ChangeSubscribeableToString < ActiveRecord::Migration
def self.up
change_column :subscribes, :subscribeable_id, :string
end
def self.down
change_column :subscribes, :subscribeable_id, :integer
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120118173141) do
ActiveRecord::Schema.define(:version => 20120123161250) do
create_table "arches", :force => true do |t|
t.string "name", :null => false
@ -304,13 +304,25 @@ ActiveRecord::Schema.define(:version => 20120118173141) do
end
create_table "subscribes", :force => true do |t|
t.integer "subscribeable_id"
t.string "subscribeable_id"
t.string "subscribeable_type"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "status", :default => 1
t.integer "project_id"
end
create_table "user_emails", :force => true do |t|
t.integer "user_id", :null => false
t.string "email", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "user_emails", ["email"], :name => "index_user_emails_on_email"
add_index "user_emails", ["user_id"], :name => "index_user_emails_on_user_id"
create_table "users", :force => true do |t|
t.string "name"
t.string "email", :default => "", :null => false