Module: DataMapper::Is::Friendly::InstanceMethods

Defined in:
lib/dm-is-friendly/is/friendly.rb

Instance Method Summary collapse

Instance Method Details

#confirm_friendship_with(friend) ⇒ self

Accepts a user and updates an existing friendship to be accepted.

Parameters:

  • friend

    The friend that needs to be confirmed

Returns:

  • (self)


126
127
128
129
130
131
# File 'lib/dm-is-friendly/is/friendly.rb', line 126

def confirm_friendship_with(friend)
  self.friendship(friend,{:accepted_at => nil}).update({:accepted_at => Time.now})
  # reload so old relationship won't be lingering in the IdentityMap
  friend.reload
  self.reload
end

#end_friendship_with(friend) ⇒ true, false

Accepts a user and deletes a friendship between both users.

Parameters:

  • friend

    The friend who’s friendship will now end

Returns:

  • (true, false)

    Outcome of model.destroy



136
137
138
# File 'lib/dm-is-friendly/is/friendly.rb', line 136

def end_friendship_with(friend)
  self.friendship(friend).destroy if self.is_friends_with?(friend)
end

#friendsDataMapper::Collection

returns all of the friends this person has that are accepted

Returns:

  • (DataMapper::Collection)

    All the person’s friends



62
63
64
# File 'lib/dm-is-friendly/is/friendly.rb', line 62

def friends
  friendship_requests(nil,true) | friendships_to_accept(nil,true)
end

#friendship_requested?(friend) ⇒ true, false

see if there is a pending friendship request from this person to another

Parameters:

  • friend

Returns:

  • (true, false)


95
96
97
# File 'lib/dm-is-friendly/is/friendly.rb', line 95

def friendship_requested?(friend)
  !friendship_requests(friend).empty?
end

#friendship_requests(friend = nil, include_accepted = false) ⇒ DataMapper::Collection

returns all the people I have requested frienship from

Parameters:

  • friend (nil) (defaults to: nil)
  • include_accepted (false) (defaults to: false)

Returns:

  • (DataMapper::Collection)

    All the people that the person has sent friendship requests to



70
71
72
73
74
75
76
77
# File 'lib/dm-is-friendly/is/friendly.rb', line 70

def friendship_requests(friend = nil, include_accepted = false)
  conditions = {}
  friend_acceptance_condition(conditions, include_accepted)
  friend_scope_condition(conditions, friend)
  
  conditions[friendly_config.friendship_foreign_key] = self.id
  friendly_config.friendship_class.all(conditions).friend
end

#friendship_to_accept?(friend) ⇒ true, false

see if user has a friend request to accept from this person

Parameters:

  • friend

Returns:

  • (true, false)


102
103
104
105
# File 'lib/dm-is-friendly/is/friendly.rb', line 102

def friendship_to_accept?(friend)
  return false unless friendly_config.require_acceptance?
  !friendships_to_accept(friend).empty?
end

#friendships_to_accept(friend = nil, include_accepted = false) ⇒ DataMapper::Collection

returns all the people that have requested my friendship

Parameters:

  • friend (nil) (defaults to: nil)
  • include_accepted (false) (defaults to: false)

Returns:

  • (DataMapper::Collection)

    All the people that have requested friendship



83
84
85
86
87
88
89
90
# File 'lib/dm-is-friendly/is/friendly.rb', line 83

def friendships_to_accept(friend = nil, include_accepted = false)
  conditions = {}
  friend_acceptance_condition(conditions, include_accepted)
  friend_scope_condition(conditions, friend, true)
  
  conditions[:friend_id] = self.id                      
  friendly_config.friendship_class.all(conditions).send(friendly_config.reference_model_name.downcase)
end

#is_friends_with?(friend) ⇒ true, false

Accepts a user object and returns true if both users are friends and the friendship has been accepted.

Parameters:

  • friend

Returns:

  • (true, false)


111
112
113
# File 'lib/dm-is-friendly/is/friendly.rb', line 111

def is_friends_with?(friend)
  !self.friendship(friend).nil?
end

#request_friendship(friend) ⇒ Object

request friendship from “friend”

Parameters:

  • friend

    The friend who’s friendship is being requested

Returns:

  • The instance of the friendship_class



118
119
120
121
# File 'lib/dm-is-friendly/is/friendly.rb', line 118

def request_friendship(friend)
  return false if friendship(friend)
  self.friendships.create(:friend => friend)
end