2011-12-26 15:48:57 +00:00
|
|
|
class Subscribe < ActiveRecord::Base
|
2014-01-21 04:51:49 +00:00
|
|
|
belongs_to :subscribeable, polymorphic: true
|
2011-12-26 15:48:57 +00:00
|
|
|
belongs_to :user
|
2012-01-23 19:42:54 +00:00
|
|
|
belongs_to :project
|
|
|
|
|
2014-03-17 20:18:46 +00:00
|
|
|
attr_accessible :status
|
|
|
|
|
2012-02-14 16:05:41 +00:00
|
|
|
def commit_subscribe?
|
|
|
|
subscribeable_type == 'Grit::Commit'
|
|
|
|
end
|
|
|
|
|
2012-01-29 20:18:14 +00:00
|
|
|
def subscribed?
|
2012-01-30 11:59:57 +00:00
|
|
|
status
|
2012-01-29 20:18:14 +00:00
|
|
|
end
|
|
|
|
|
2012-01-24 12:18:39 +00:00
|
|
|
def self.comment_subscribes(comment)
|
2014-01-21 04:51:49 +00:00
|
|
|
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)
|
2014-01-21 04:51:49 +00:00
|
|
|
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
|
2012-03-14 14:33:16 +00:00
|
|
|
true
|
2012-01-23 19:42:54 +00:00
|
|
|
end
|
|
|
|
|
2012-01-30 11:59:57 +00:00
|
|
|
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
|
2014-01-21 04:51:49 +00:00
|
|
|
subscribe.update_attributes(status: status)
|
2012-01-23 19:42:54 +00:00
|
|
|
else
|
2014-03-17 20:18:46 +00:00
|
|
|
Subscribe.create(options.merge(status: status), without_protection: true)
|
2012-01-23 19:42:54 +00:00
|
|
|
end
|
2012-01-21 19:13:33 +00:00
|
|
|
end
|
2012-01-30 11:59:57 +00:00
|
|
|
|
2011-12-26 15:48:57 +00:00
|
|
|
end
|