Module: MuckFriends::Models::MuckFriend::ClassMethods

Defined in:
lib/muck-friends/models/friend.rb

Instance Method Summary collapse

Instance Method Details

#add_follower(inviter, invited) ⇒ Object

When enable_following is true this will setup a follower. When enable_following is false the system only allows friends and so this becomes a friend request



16
17
18
19
20
21
22
23
24
25
# File 'lib/muck-friends/models/friend.rb', line 16

def add_follower(inviter, invited)
  return false if inviter.blank? || invited.blank? || inviter == invited
  success = false
  transaction do
    friend = self.create(:inviter => inviter, :invited => invited, :status => MuckFriends::FOLLOWING)
    friend.add_follow_activity
    success = !friend.new_record?
  end
  success
end

#block_user(user, target) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/muck-friends/models/friend.rb', line 81

def block_user(user, target)
  return false if user.blank? || target.blank?
  friend = self.find(:first, :conditions => {:inviter_id => user.id, :invited_id => target.id})
  friend.destroy if friend
  friend = self.find(:first, :conditions => {:inviter_id => target.id, :invited_id => user.id})
  if friend
    friend.update_attribute(:status, MuckFriends::BLOCKED)
  else
    false
  end
end

#blocked?(user, target) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/muck-friends/models/friend.rb', line 103

def blocked?(user, target)
  !self.find(:first, :conditions => {:inviter_id => target.id, :invited_id => user.id, :status => MuckFriends::BLOCKED}).blank?
end

#friendship_with(user, target) ⇒ Object



111
112
113
# File 'lib/muck-friends/models/friend.rb', line 111

def friendship_with(user, target)
  self.find(:first, :conditions => {:inviter_id => target.id, :invited_id => user.id})
end

#has_network?(user) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/muck-friends/models/friend.rb', line 107

def has_network?(user)
  !self.find(:first, :conditions => ["invited_id = ? or inviter_id = ?", user.id, user.id]).blank?
end

#make_friends(user, target) ⇒ Object

Called after add_follower. This establishes a friend relationship between the two parties



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/muck-friends/models/friend.rb', line 28

def make_friends(user, target)
  return false if user.blank? || target.blank? || user == target
  transaction do
    begin
      self.find(:first, :conditions => {:inviter_id => user.id, :invited_id => target.id, :status => MuckFriends::FOLLOWING}).update_attribute(:status, MuckFriends::FRIENDING)
      friend = self.create!(:inviter_id => target.id, :invited_id => user.id, :status => MuckFriends::FRIENDING)
      friend.add_friends_with_activity
    rescue Exception
      return make_friends(target, user) if user.followed_by?(target)
      return add_follower(user, target)
    end
  end
  true
end

#revert_to_follower(user, target) ⇒ Object

Stop friendship with another user. This will turn the other user into a follower



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/muck-friends/models/friend.rb', line 56

def revert_to_follower(user, target)
  return false if user.blank? || target.blank?
  transaction do
    friend = self.find(:first, :conditions => {:inviter_id => user.id, :invited_id => target.id})
    friend.destroy if friend
    friend = self.find(:first, :conditions => {:inviter_id => target.id, :invited_id => user.id})
    if friend
      return friend.update_attribute(:status, MuckFriends::FOLLOWING)
    else
      return false
    end
  end
end

#stop_being_friends(user, target) ⇒ Object

Destroy the frienship :(



44
45
46
47
48
49
50
51
52
53
# File 'lib/muck-friends/models/friend.rb', line 44

def stop_being_friends(user, target)
  return false if user.blank? || target.blank?
  transaction do
    friend = self.find(:first, :conditions => {:inviter_id => target.id, :invited_id => user.id})
    friend.destroy if friend
    friend = self.find(:first, :conditions => {:inviter_id => user.id, :invited_id => target.id})
    friend.destroy if friend
  end
  true
end

#stop_following(user, target) ⇒ Object

User stops following target



71
72
73
74
75
76
77
78
79
# File 'lib/muck-friends/models/friend.rb', line 71

def stop_following(user, target)
  return false if user.blank? || target.blank?
  friend = self.find(:first, :conditions => {:inviter_id => user.id, :invited_id => target.id})
  if friend
    friend.destroy
  else
    false
  end
end

#unblock_user(user, target) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/muck-friends/models/friend.rb', line 93

def unblock_user(user, target)
  return false if user.blank? || target.blank?
  friend = self.find(:first, :conditions => {:inviter_id => target.id, :invited_id => user.id})
  if friend
    friend.update_attribute(:status, MuckFriends::FOLLOWING)
  else
    false
  end
end