add is_following field on user endpoint

This commit is contained in:
João Victor Geonizeli
2022-02-28 13:50:22 -03:00
parent a08a889e7d
commit c115da9a35
3 changed files with 22 additions and 0 deletions

View File

@@ -14,4 +14,8 @@ class User < ApplicationRecord
has_many :user_follows_followers, class_name: 'UserFollow', foreign_key: :followed_id
has_many :following, through: :user_follows_following, source: :followed
has_many :followers, through: :user_follows_followers, source: :follower
def is_following?(user)
user_follows_following.where(followed_id: user&.id).exists?
end
end

View File

@@ -3,5 +3,6 @@ json.joined_at user.created_at.strftime("%B %d, %Y")
json.following_count user.following.count
json.follower_count user.followers.count
json.posts_count user.posts.count
json.is_following user.is_following?(@current_user)
json.url user_url(user, format: :json)

View File

@@ -61,4 +61,21 @@ RSpec.describe User, type: :model do
end
end
end
describe '#is_following?' do
it do
following_user = create(:user)
now_following_user = create(:user)
followed_user = create(:user)
create(:user_follow, follower_id: following_user.id, followed_id: followed_user.id)
expect(
following_user.is_following?(followed_user)
).to be_truthy
expect(
now_following_user.is_following?(followed_user)
).to be_falsey
end
end
end