From 119c378149e988ed71147ba072ad56948c85d063 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Mon, 30 Jan 2012 17:59:57 +0600 Subject: [PATCH] [refs #114] change subsrcibe status to boolean --- .../commit_subscribes_controller.rb | 4 +-- app/models/comment.rb | 2 +- app/models/subscribe.rb | 25 ++++++++++++------- ...20120130111133_change_status_subscribes.rb | 11 ++++++++ db/schema.rb | 4 +-- spec/models/comment_for_commit_spec.rb | 10 ++++---- 6 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 db/migrate/20120130111133_change_status_subscribes.rb diff --git a/app/controllers/commit_subscribes_controller.rb b/app/controllers/commit_subscribes_controller.rb index 4d33c06ee..c50902dca 100644 --- a/app/controllers/commit_subscribes_controller.rb +++ b/app/controllers/commit_subscribes_controller.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index 13118e964..e1e0ad22f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/app/models/subscribe.rb b/app/models/subscribe.rb index b0c31a216..66b5727b1 100644 --- a/app/models/subscribe.rb +++ b/app/models/subscribe.rb @@ -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 \ No newline at end of file diff --git a/db/migrate/20120130111133_change_status_subscribes.rb b/db/migrate/20120130111133_change_status_subscribes.rb new file mode 100644 index 000000000..9d31898c8 --- /dev/null +++ b/db/migrate/20120130111133_change_status_subscribes.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index db5afc7d7..b0c0d3a73 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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| diff --git a/spec/models/comment_for_commit_spec.rb b/spec/models/comment_for_commit_spec.rb index f5eed5cf7..c190b9f77 100644 --- a/spec/models/comment_for_commit_spec.rb +++ b/spec/models/comment_for_commit_spec.rb @@ -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)