[refs #114] fix logics

This commit is contained in:
Alexander Machehin 2012-01-24 18:18:39 +06:00
parent 6582a9afa5
commit 6df1640485
6 changed files with 54 additions and 33 deletions

View File

@ -1,13 +1,12 @@
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)
if Subscribe.set_subscribe(@project, @commit, current_user.id, Subscribe::ON)
flash[:notice] = I18n.t("flash.subscribe.saved")
# TODO js
redirect_to commit_path(@project, @commit)
@ -18,7 +17,7 @@ class CommitSubscribesController < ApplicationController
end
def destroy
Subscribe.set_subscribe(@project, @commit, current_user.id, 0)
Subscribe.set_subscribe(@project, @commit, current_user.id, Subscribe::OFF)
flash[:notice] = t("flash.subscribe.destroyed")
redirect_to commit_path(@project, @commit)
end

View File

@ -18,12 +18,11 @@ class Comment < ActiveRecord::Base
user_id == user.id
end
protected
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)
Subscribe.subscribe_user_to_commit(self, self.user_id) if self.commentable_type == 'Grit::Commit'
Subscribe.subscribe_user_to_commit(self, self.user.id) if self.commentable_type == 'Grit::Commit'
end
def invoke_helper

View File

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

View File

@ -1,49 +1,65 @@
class Subscribe < ActiveRecord::Base
ON = 1
OFF = 0
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)
scope :on, where(:status => ON)
scope :off, where(:status => OFF)
scope :finder_hack, order('') # FIXME .subscribes - error; .subscribes.finder_hack - success Oo
def self.comment_subscribes(comment)
Subscribe.where(:subscribeable_id => comment.commentable.id, :subscribeable_type => comment.commentable.class.name.to_s, :project_id => comment.project)
end
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 = 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.new_comment_issue_notification(comment) if commentable_class == Issue
Subscribe.new_comment_commit_notification(comment) if commentable_class == Grit::Commit
end
def self.new_comment_issue_notification(comment)
comment.commentable.subscribes.finder_hack.each do |subscribe|
next if comment.own_comment?(subscribe.user) || !subscribe.user.notifier.can_notify
UserMailer.delay.new_comment_notification(comment, subscribe.user) if subscribe.user.notifier.new_comment_reply
end
end
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
def self.new_comment_commit_notification(comment)
subscribes = Subscribe.comment_subscribes(comment).on(true) # FIXME (true) for rspec
subscribes.each do |subscribe|
next if comment.own_comment?(subscribe.user) || !subscribe.user.notifier.can_notify
UserMailer.delay.new_comment_notification(comment, subscribe.user)
end
end
def self.subscribe_user_to_commit(comment, user)
Subscribe.set_subscribe(comment.project, comment.commentable, user, Subscribe::ON) if Subscribe.subscribed_for_commit?(comment.project, User.find(user), comment.commentable)
end
def self.subscribed_for_commit?(project, user, commentable)
is_owner = (project.owner_id == user.id)
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)
return false if Subscribe.where(:subscribeable_id => commentable.id, :subscribeable_type => commentable.class.name,
:user_id => user.id, :project_id => project.id, :status => Subscribe::OFF).first.present?
(is_owner && user.notifier.new_comment_commit_repo_owner) or
(is_commentor && user.notifier.new_comment_commit_commentor) or
(is_committer && user.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,
subscribe = Subscribe.where(:subscribeable_id => commit.id, :subscribeable_type => commit.class.name,
: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)
Subscribe.create(:subscribeable_id => commit.id, :subscribeable_type => commit.class.name,
:user_id => user, :project_id => project.id, :status => status)
end
end
end

View File

@ -33,7 +33,7 @@
= 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))
- if subscribe.try(:status) == Subscribe::ON 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

@ -278,6 +278,19 @@ ActiveRecord::Schema.define(:version => 20120123161250) do
t.datetime "updated_at"
end
create_table "role_lines", :force => true do |t|
t.integer "role_id"
t.integer "relation_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "roles", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "rpms", :force => true do |t|
t.string "name", :null => false
t.integer "arch_id", :null => false