forked from microverseinc/ror-social-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent user from sending a friend request to themselves
- Loading branch information
Showing
6 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
class User < ApplicationRecord | ||
# Include default devise modules. Others available are: | ||
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable | ||
devise :database_authenticatable, :registerable, | ||
:recoverable, :rememberable, :validatable | ||
|
||
validates :name, presence: true, length: { maximum: 20 } | ||
|
||
has_many :friendships | ||
has_many :friends, through: :friendships | ||
has_many :inverse_friendships, class_name: "Friendship", foreign_key: "friend_id" | ||
has_many :posts | ||
has_many :comments, dependent: :destroy | ||
has_many :likes, dependent: :destroy | ||
|
||
def pending | ||
self.status = 'p' | ||
end | ||
|
||
def friends | ||
friends_array = friendships.map{ |friendship| friendship.friend if friendship.status == 'a' } | ||
friends_array.compact | ||
end | ||
|
||
def confirm_friend(user) | ||
friendship = inverse_friendships.find{ |friendship| friendship.user == user } | ||
friendship.status = 'a' | ||
friendship.save | ||
end | ||
|
||
def friend?(user) | ||
friends.include?(user) | ||
end | ||
|
||
def pending_friends | ||
friends_array = friendships.map{ |friendship| friendship.friend if !friendship.status = 'p' } | ||
friends_array.compact | ||
end | ||
|
||
def reject_friend(user) | ||
friendship = inverse_friendships.find{ |friendship| friendship.user == user } | ||
friendship.status = 'r' | ||
friendship.save | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
class Friendship < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :friend, class_name: "User" | ||
|
||
validates_uniqueness_of :user_id, scope: :friend_id | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters