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

27 lines
633 B

6 years ago
module Watchable
extend ActiveSupport::Concern
included do
5 years ago
has_many :watchers, as: :watchable, dependent: :destroy
has_many :watcher_users, through: :watchers, source: :user, validate: false
6 years ago
scope :watched_by, -> (user_id) { includes(:watchers).where(watchers: { user_id: user_id }) }
end
def watched?(watchable)
watchable.watchers.exists?(user: self)
end
5 years ago
def watch!(watchable)
watchable.watchers.create!(user: self)
6 years ago
end
5 years ago
def unwatch!(watchable)
obj = watchable.watchers.find_by(user: self)
6 years ago
obj.destroy! if obj.present?
end
module ClassMethods
end
5 years ago
end