added methods for following/unfollowing users

This commit is contained in:
nilsding 2014-11-30 15:13:17 +01:00
parent 63be00f70f
commit 928addf5db
1 changed files with 22 additions and 0 deletions

View File

@ -42,4 +42,26 @@ class User < ActiveRecord::Base
where(conditions).first where(conditions).first
end end
end end
# follows an user.
def follow(target_user)
active_relationships.create(target: target_user)
# increment counts
increment! :friend_count
target_user.increment! :follower_count
end
def unfollow(target_user)
active_relationships.find_by(target: target_user).destroy
# decrement counts
decrement! :friend_count
target_user.decrement! :follower_count
end
# @return [Boolean] true if +current_user+ is following +target_user+
def following?(target_user)
friends.include? target_user
end
end end