Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add add_friendship method #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/amistad/active_record_friend_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def approve(user)
friendship.update_attribute(:pending, false)
end

# adds a friendship directly, without invitation process
def add_friendship(user)
return false if user == self || find_any_friendship_with(user)
Amistad.friendship_class.new{ |f| f.friendable = self ; f.friend = user ; f.pending = false }.save
end

# deletes a friendship
def remove_friendship(user)
friendship = find_any_friendship_with(user)
Expand Down
8 changes: 8 additions & 0 deletions lib/amistad/mongo_friend_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def invited?(user)
self.friend_ids.include?(user.id) or self.pending_friend_ids.include?(user.id)
end

# adds a friendship directly, without invitation process
def add_friendship(user)
return false if friendshiped_with?(user) or user == self or blocked?(user)
inverse_friend_ids << user.id
user.friend_ids << self.id
self.save && user.save
end

# deletes a friendship
def remove_friendship(user)
friend_ids.delete(user.id)
Expand Down