[refs #114] fix logics
This commit is contained in:
parent
6582a9afa5
commit
6df1640485
|
@ -1,13 +1,12 @@
|
||||||
class CommitSubscribesController < ApplicationController
|
class CommitSubscribesController < ApplicationController
|
||||||
before_filter :authenticate_user!
|
before_filter :authenticate_user!
|
||||||
|
|
||||||
load_resource :subscribe
|
|
||||||
load_and_authorize_resource :project
|
load_and_authorize_resource :project
|
||||||
|
|
||||||
before_filter :find_commit
|
before_filter :find_commit
|
||||||
|
|
||||||
def create
|
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")
|
flash[:notice] = I18n.t("flash.subscribe.saved")
|
||||||
# TODO js
|
# TODO js
|
||||||
redirect_to commit_path(@project, @commit)
|
redirect_to commit_path(@project, @commit)
|
||||||
|
@ -18,7 +17,7 @@ class CommitSubscribesController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
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")
|
flash[:notice] = t("flash.subscribe.destroyed")
|
||||||
redirect_to commit_path(@project, @commit)
|
redirect_to commit_path(@project, @commit)
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,12 +18,11 @@ class Comment < ActiveRecord::Base
|
||||||
user_id == user.id
|
user_id == user.id
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def subscribe_on_reply
|
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.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
|
end
|
||||||
|
|
||||||
def invoke_helper
|
def invoke_helper
|
||||||
|
|
|
@ -5,15 +5,9 @@ class Issue < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
has_many :comments, :as => :commentable,
|
has_many :comments, :as => :commentable,
|
||||||
:finder_sql => proc { "comments.commentable_id = '#{self.id}' " +
|
:finder_sql => proc { "comments.commentable_id = '#{self.id}' AND comments.commentable_type = '#{self.class.name}'"}
|
||||||
" 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'
|
|
||||||
has_many :subscribes, :as => :subscribeable,
|
has_many :subscribes, :as => :subscribeable,
|
||||||
:finder_sql => proc { "subscribes.subscribeable_id = '#{self.id}' " +
|
:finder_sql => proc { "subscribes.subscribeable_id = '#{self.id}' AND subscribes.subscribeable_type = '#{self.class.name}'"}
|
||||||
" AND subscribes.subscribeable_type = '#{self.class.name}'"}
|
|
||||||
|
|
||||||
validates :title, :body, :project_id, :presence => true
|
validates :title, :body, :project_id, :presence => true
|
||||||
|
|
||||||
|
|
|
@ -1,49 +1,65 @@
|
||||||
class Subscribe < ActiveRecord::Base
|
class Subscribe < ActiveRecord::Base
|
||||||
|
ON = 1
|
||||||
|
OFF = 0
|
||||||
belongs_to :subscribeable, :polymorphic => true
|
belongs_to :subscribeable, :polymorphic => true
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :project
|
belongs_to :project
|
||||||
|
|
||||||
validates :status, :inclusion => {:in => 0..1}
|
validates :status, :inclusion => {:in => 0..1}
|
||||||
|
|
||||||
scope :subscribed, where(:status => 1)
|
scope :on, where(:status => ON)
|
||||||
scope :unsubscribed, where(:status => 0)
|
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)
|
def self.new_comment_notification(comment)
|
||||||
commentable_class = comment.commentable.class
|
commentable_class = comment.commentable.class
|
||||||
subscribes = comment.commentable.subscribes if commentable_class == Issue
|
Subscribe.new_comment_issue_notification(comment) if commentable_class == Issue
|
||||||
if commentable_class == Grit::Commit
|
Subscribe.new_comment_commit_notification(comment) 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
|
||||||
end
|
|
||||||
subscribes.each do |subscribe|
|
def self.new_comment_issue_notification(comment)
|
||||||
user = subscribe.user
|
comment.commentable.subscribes.finder_hack.each do |subscribe|
|
||||||
next if comment.own_comment?(user) || !user.notifier.can_notify
|
next if comment.own_comment?(subscribe.user) || !subscribe.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, subscribe.user) if subscribe.user.notifier.new_comment_reply
|
||||||
UserMailer.delay.new_comment_notification(comment, user) if commentable_class == Grit::Commit
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.subscribe_user_to_commit(comment, user_id)
|
def self.new_comment_commit_notification(comment)
|
||||||
subscribe = Subscribe.where(:subscribeable_id => comment.commentable.id, :subscribeable_type => comment.commentable.class.name, :project_id => comment.project).unsubscribed.first
|
subscribes = Subscribe.comment_subscribes(comment).on(true) # FIXME (true) for rspec
|
||||||
subscribe.update_attribute(:status, 1) if subscribe
|
subscribes.each do |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
|
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
|
end
|
||||||
|
|
||||||
def self.subscribed_for_commit?(project, user, commentable)
|
def self.subscribed_for_commit?(project, user, commentable)
|
||||||
is_owner = (project.owner_id == user.id)
|
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_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_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
|
end
|
||||||
|
|
||||||
def self.set_subscribe(project, commit, user, status)
|
def self.set_subscribe(project, commit, user, status)
|
||||||
# FIXME maybe?
|
# 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
|
:user_id => user, :project_id => project).first
|
||||||
if subscribe
|
if subscribe
|
||||||
subscribe.update_attribute(:status, status)
|
subscribe.update_attribute(:status, status)
|
||||||
else
|
else
|
||||||
Subscribe.create(:subscribeable_id => commit.id, :subscribeable_type => commit.class.name.to_s,
|
Subscribe.create(:subscribeable_id => commit.id, :subscribeable_type => commit.class.name,
|
||||||
:user_id => user, :project_id => project, :status => status)
|
:user_id => user, :project_id => project.id, :status => status)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
= t('layout.issues.subscribe')
|
= 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
|
- 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
|
= link_to t('layout.issues.unsubscribe_btn'), unsubscribe_commit_path(@project, @commit), :method => :delete
|
||||||
- else
|
- else
|
||||||
= link_to t('layout.issues.subscribe_btn'), subscribe_commit_path(@project, @commit), :method => :post
|
= link_to t('layout.issues.subscribe_btn'), subscribe_commit_path(@project, @commit), :method => :post
|
||||||
|
|
13
db/schema.rb
13
db/schema.rb
|
@ -278,6 +278,19 @@ ActiveRecord::Schema.define(:version => 20120123161250) do
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
end
|
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|
|
create_table "rpms", :force => true do |t|
|
||||||
t.string "name", :null => false
|
t.string "name", :null => false
|
||||||
t.integer "arch_id", :null => false
|
t.integer "arch_id", :null => false
|
||||||
|
|
Loading…
Reference in New Issue