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