[refs #114] change subsrcibe status to boolean

This commit is contained in:
Alexander Machehin 2012-01-30 17:59:57 +06:00
parent 5efaca77cf
commit 119c378149
6 changed files with 37 additions and 19 deletions

View File

@ -6,7 +6,7 @@ class CommitSubscribesController < ApplicationController
before_filter :find_commit
def create
if Subscribe.set_subscribe_to_commit(@options, Subscribe::ON)
if Subscribe.subscribe_to_commit(@options)
flash[:notice] = I18n.t("flash.subscribe.commit.saved")
# TODO js
redirect_to commit_path(@project, @commit)
@ -17,7 +17,7 @@ class CommitSubscribesController < ApplicationController
end
def destroy
Subscribe.set_subscribe_to_commit(@options, Subscribe::OFF)
Subscribe.unsubscribe_from_commit(@options)
flash[:notice] = t("flash.subscribe.commit.destroyed")
redirect_to commit_path(@project, @commit)
end

View File

@ -32,7 +32,7 @@ class Comment < ActiveRecord::Base
recipients << self.project.owner if self.project.owner_type == 'User' # project owner
recipients.compact.uniq.each do |user|
options = {:project_id => self.project.id, :subscribeable_id => self.commentable.id, :subscribeable_type => self.commentable.class.name, :user_id => user.id}
Subscribe.set_subscribe_to_commit(options, Subscribe::ON) if Subscribe.subscribed_to_commit?(self.project, user, self.commentable)
Subscribe.subscribe_to_commit(options) if Subscribe.subscribed_to_commit?(self.project, user, self.commentable)
end
end
end

View File

@ -1,18 +1,12 @@
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 :on, where(:status => ON)
scope :off, where(:status => OFF)
scope :finder_hack, order('') # FIXME .subscribes - error; .subscribes.finder_hack - success Oo
def subscribed?
status == ON
status
end
def self.comment_subscribes(comment)
@ -33,7 +27,7 @@ class Subscribe < ActiveRecord::Base
end
def self.new_comment_commit_notification(comment)
subscribes = Subscribe.comment_subscribes(comment).on
subscribes = Subscribe.comment_subscribes(comment).where(:status => true)
subscribes.each do |subscribe|
next if comment.own_comment?(subscribe.user) || !subscribe.user.notifier.can_notify
UserMailer.delay.new_comment_notification(comment, subscribe.user)
@ -49,6 +43,18 @@ class Subscribe < ActiveRecord::Base
(user.committer?(commit) && user.notifier.new_comment_commit_owner)
end
def self.subscribe_to_commit(options)
Subscribe.set_subscribe_to_commit(options, true)
end
def self.unsubscribe_from_commit(options)
Subscribe.set_subscribe_to_commit(options, false)
end
private
def self.set_subscribe_to_commit(options, status)
if subscribe = Subscribe.where(options).first
subscribe.update_attribute(:status, status)
@ -56,4 +62,5 @@ class Subscribe < ActiveRecord::Base
Subscribe.create(options.merge(:status => status))
end
end
end
end

View File

@ -0,0 +1,11 @@
class ChangeStatusSubscribes < ActiveRecord::Migration
def self.up
remove_column :subscribes, :status
add_column :subscribes, :status, :boolean, :default => true
end
def self.down
remove_column :subscribes, :status
add_column :subscribes, :status, :integer, :default => 1
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 => 20120129120025) do
ActiveRecord::Schema.define(:version => 20120130111133) do
create_table "arches", :force => true do |t|
t.string "name", :null => false
@ -299,8 +299,8 @@ ActiveRecord::Schema.define(:version => 20120129120025) do
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "status", :default => 1
t.integer "project_id"
t.boolean "status", :default => true
end
create_table "users", :force => true do |t|

View File

@ -131,7 +131,7 @@ describe Comment do
context 'for unsubscribe commit' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
Subscribe.set_subscribe_to_commit({:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @user.id}, Subscribe::OFF)
Subscribe.unsubscribe_from_commit(:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @user.id)
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 ...
@ -234,7 +234,7 @@ describe Comment do
context 'for unsubscribe project' do
it 'should not send an e-mail' do
ActionMailer::Base.deliveries = []
Subscribe.set_subscribe_to_commit({:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @user.id}, Subscribe::OFF)
Subscribe.unsubscribe_from_commit(:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @user.id)
comment = Comment.create(:user => @stranger, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 0
@ -321,7 +321,7 @@ describe Comment do
@stranger.notifier.update_attribute :new_comment_commit_owner, false
#@stranger.notifier.update_attribute :new_comment_commit_commentor, false
Subscribe.set_subscribe_to_commit({:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @stranger.id}, Subscribe::ON)
Subscribe.subscribe_to_commit(:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @stranger.id)
comment = Comment.create(:user => @project.owner, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)
ActionMailer::Base.deliveries.count.should == 1
@ -331,7 +331,7 @@ 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
Subscribe.set_subscribe_to_commit({:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @stranger.id}, Subscribe::ON)
Subscribe.subscribe_to_commit(:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :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
@ -350,7 +350,7 @@ describe Comment do
it 'should send a one e-mail when subscribed to commit' do
ActionMailer::Base.deliveries = []
Subscribe.set_subscribe_to_commit({:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @stranger.id}, Subscribe::ON)
Subscribe.subscribe_to_commit(:project_id => @project.id, :subscribeable_id => @commit.id, :subscribeable_type => @commit.class.name, :user_id => @stranger.id)
@stranger.update_attribute :email, 'code@tpope.net'
comment = Comment.create(:user => @user, :body => 'hello!', :project => @project,
:commentable_type => @commit.class.name, :commentable_id => @commit.id)