Retrospring/app/models/user/relationship/follow.rb

39 lines
1.2 KiB
Ruby
Raw Normal View History

2021-12-31 13:19:21 -08:00
# frozen_string_literal: true
require "errors"
class User
module Relationship
module Follow
extend ActiveSupport::Concern
included do
2022-01-16 09:51:27 -08:00
has_many :active_follow_relationships, class_name: "Relationships::Follow",
2021-12-31 13:19:21 -08:00
foreign_key: "source_id",
2022-01-16 09:51:27 -08:00
dependent: :destroy
has_many :passive_follow_relationships, class_name: "Relationships::Follow",
2021-12-31 13:19:21 -08:00
foreign_key: "target_id",
2022-01-16 09:51:27 -08:00
dependent: :destroy
2021-12-31 13:19:21 -08:00
has_many :followings, through: :active_follow_relationships, source: :target
has_many :followers, through: :passive_follow_relationships, source: :source
end
# Follow an user
def follow(target_user)
raise Errors::FollowingSelf if target_user == self
2022-01-16 09:51:27 -08:00
2021-12-31 13:19:21 -08:00
create_relationship(active_follow_relationships, target_user)
end
# Unfollow an user
def unfollow(target_user)
destroy_relationship(active_follow_relationships, target_user)
end
def following?(target_user)
relationship_active?(followings, target_user)
end
end
end
end