mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-03 03:40:49 +08:00
init project
This commit is contained in:
26
app/models/concerns/watchable.rb
Normal file
26
app/models/concerns/watchable.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
module Watchable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many :watchers, as: :watchable, dependent: :destroy
|
||||
has_many :watcher_users, through: :watchers, source: :user, validate: false
|
||||
|
||||
scope :watched_by, -> (user_id) { includes(:watchers).where(watchers: { user_id: user_id }) }
|
||||
end
|
||||
|
||||
def watched?(watchable)
|
||||
watchable.watchers.exists?(user: self)
|
||||
end
|
||||
|
||||
def watch!(watchable)
|
||||
watchable.watchers.create!(user: self)
|
||||
end
|
||||
|
||||
def unwatch!(watchable)
|
||||
obj = watchable.watchers.find_by(user: self)
|
||||
obj.destroy! if obj.present?
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user