2020-04-19 00:59:18 +02:00
|
|
|
class Relationship < ApplicationRecord
|
2014-11-30 14:43:35 +01:00
|
|
|
belongs_to :source, class_name: 'User'
|
|
|
|
belongs_to :target, class_name: 'User'
|
|
|
|
validates :source_id, presence: true
|
|
|
|
validates :target_id, presence: true
|
2014-12-14 14:58:29 +01:00
|
|
|
|
2014-12-28 21:40:33 +01:00
|
|
|
after_create do
|
|
|
|
Notification.notify target, self
|
|
|
|
|
|
|
|
# increment counts
|
|
|
|
source.increment! :friend_count
|
|
|
|
target.increment! :follower_count
|
|
|
|
end
|
|
|
|
|
|
|
|
before_destroy do
|
|
|
|
Notification.denotify target, self
|
|
|
|
|
|
|
|
# decrement counts
|
|
|
|
source.decrement! :friend_count
|
|
|
|
target.decrement! :follower_count
|
|
|
|
end
|
|
|
|
|
2014-12-14 14:58:29 +01:00
|
|
|
def notification_type(*_args)
|
|
|
|
Notifications::StartedFollowing
|
|
|
|
end
|
2014-11-30 14:06:05 +01:00
|
|
|
end
|