You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/models/concerns/watchable.rb

26 lines
641 B

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!(user)
user.watchers.create!(user: self)
end
def unwatch!(user)
obj = user.watchers.find_by(user: self)
obj.destroy! if obj.present?
end
module ClassMethods
end
end