rosa-build/app/models/subscribe.rb

49 lines
1.5 KiB
Ruby
Raw Normal View History

2012-01-30 20:39:34 +00:00
# -*- encoding : utf-8 -*-
class Subscribe < ActiveRecord::Base
belongs_to :subscribeable, :polymorphic => true
belongs_to :user
2012-01-23 19:42:54 +00:00
belongs_to :project
2012-01-24 12:18:39 +00:00
scope :finder_hack, order('') # FIXME .subscribes - error; .subscribes.finder_hack - success Oo
def commit_subscribe?
subscribeable_type == 'Grit::Commit'
end
2012-01-29 20:18:14 +00:00
def subscribed?
status
2012-01-29 20:18:14 +00:00
end
2012-01-24 12:18:39 +00:00
def self.comment_subscribes(comment)
Subscribe.where(:subscribeable_id => comment.commentable_id, :subscribeable_type => comment.commentable.class.name, :project_id => comment.project)
2012-01-24 12:18:39 +00:00
end
2012-01-19 18:20:03 +00:00
2012-01-29 20:18:14 +00:00
def self.subscribed_to_commit?(project, user, commit)
subscribe = user.subscribes.where(:subscribeable_id => commit.id.hex, :subscribeable_type => commit.class.name, :project_id => project.id).first
2012-01-29 20:18:14 +00:00
return subscribe.subscribed? if subscribe # return status if already subscribe present
# return status by settings
2012-01-25 17:33:26 +00:00
(project.owner?(user) && user.notifier.new_comment_commit_repo_owner) or
2012-01-29 20:18:14 +00:00
(user.commentor?(commit) && user.notifier.new_comment_commit_commentor) or
(user.committer?(commit) && user.notifier.new_comment_commit_owner)
2012-01-23 19:42:54 +00:00
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
2012-01-29 20:18:14 +00:00
def self.set_subscribe_to_commit(options, status)
if subscribe = Subscribe.where(options).first
2012-01-23 19:42:54 +00:00
subscribe.update_attribute(:status, status)
else
2012-01-25 17:33:26 +00:00
Subscribe.create(options.merge(:status => status))
2012-01-23 19:42:54 +00:00
end
2012-01-21 19:13:33 +00:00
end
end