Module: PowerOfFriendship::ActsAsFriend::LocalInstanceMethods
- Defined in:
- lib/power_of_friendship.rb
Instance Method Summary collapse
- #approve(friend) ⇒ Object
- #connected_with?(model) ⇒ Boolean
- #create_complimentary_friendship(friendship) ⇒ Object
- #destroy_complimentary_friendship(friendship) ⇒ Object
- #find_any_friendship_with(model) ⇒ Object
- #find_friendship_complement(friendship) ⇒ Object
- #follow(model) ⇒ Object
- #followed_by?(model) ⇒ Boolean
- #follows?(model) ⇒ Boolean
- #friends_with?(model) ⇒ Boolean
- #invite(friend) ⇒ Object
- #invited?(model) ⇒ Boolean
- #invited_by?(model) ⇒ Boolean
- #pow_friendship_model ⇒ Object
- #pow_friendship_table ⇒ Object
- #pow_model_id ⇒ Object
- #pow_name_und ⇒ Object
- #suggested_friends ⇒ Object
- #unfollow(model) ⇒ Object
- #unfriend(friend) ⇒ Object
Instance Method Details
#approve(friend) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/power_of_friendship.rb', line 111 def approve(friend) friendship = pow_friendship_model.where(pow_model_id => friend.id, :friend_id => self.id, pending: true).first return false if friendship.nil? if friendship.update_attribute(:pending, false) create_complimentary_friendship friendship true else false end end |
#connected_with?(model) ⇒ Boolean
159 160 161 |
# File 'lib/power_of_friendship.rb', line 159 def connected_with?(model) find_any_friendship_with(model).present? end |
#create_complimentary_friendship(friendship) ⇒ Object
223 224 225 226 227 |
# File 'lib/power_of_friendship.rb', line 223 def create_complimentary_friendship(friendship) return false if friendship.pending? return pow_friendship_model.create(pow_model_id => friendship.friend_id, friend_id: friendship.send(pow_model_id), pending: false) end |
#destroy_complimentary_friendship(friendship) ⇒ Object
229 230 231 232 233 |
# File 'lib/power_of_friendship.rb', line 229 def destroy_complimentary_friendship(friendship) return false if friendship.pending? friendship_compliment = find_friendship_complement friendship return friendship_compliment.destroy end |
#find_any_friendship_with(model) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/power_of_friendship.rb', line 239 def find_any_friendship_with(model) pow_model_id_column = pow_model_id # TODO figure out how to avoid this friendship = pow_friendship_model.where{ (( __send__(pow_model_id_column) == my{id} ) & ( friend_id == my{model.id} )) | (( __send__(pow_model_id_column) == my{model.id} ) & ( friend_id == my{id} )) }.order(created_at: :desc).first; friendship end |
#find_friendship_complement(friendship) ⇒ Object
235 236 237 |
# File 'lib/power_of_friendship.rb', line 235 def find_friendship_complement friendship friendship = pow_friendship_model.where(pow_model_id => friendship.friend_id, :friend_id => friendship.send(pow_model_id)).first end |
#follow(model) ⇒ Object
129 130 131 132 |
# File 'lib/power_of_friendship.rb', line 129 def follow(model) return true if self.invite(model) return self.approve model end |
#followed_by?(model) ⇒ Boolean
155 156 157 |
# File 'lib/power_of_friendship.rb', line 155 def followed_by?(model) self.followers.include?(model) end |
#follows?(model) ⇒ Boolean
151 152 153 |
# File 'lib/power_of_friendship.rb', line 151 def follows?(model) self.inverse_followers.include?(model) end |
#friends_with?(model) ⇒ Boolean
147 148 149 |
# File 'lib/power_of_friendship.rb', line 147 def friends_with?(model) self.friends.include?(model) end |
#invite(friend) ⇒ Object
105 106 107 108 109 |
# File 'lib/power_of_friendship.rb', line 105 def invite(friend) return false if friend == self || find_any_friendship_with(friend) friendship = pow_friendship_model.new( pow_model_id => self.id, :friend_id => friend.id, pending: true ).save friendship end |
#invited?(model) ⇒ Boolean
172 173 174 175 176 177 178 179 |
# File 'lib/power_of_friendship.rb', line 172 def invited?(model) friendship = pow_friendship_model.where(pow_model_id => self.id, :friend_id => model.id).first return false if friendship.nil? return friendship.friend_id == model.id if friendship.pending == true inverse_friendship = find_friendship_complement friendship return friendship.friend_id == model.id if friendship.created_at <= inverse_friendship.created_at return inverse_friendship.friend_id == model.id end |
#invited_by?(model) ⇒ Boolean
163 164 165 166 167 168 169 170 |
# File 'lib/power_of_friendship.rb', line 163 def invited_by?(model) friendship = find_any_friendship_with(model) return false if friendship.nil? return friendship.send(pow_model_id) == model.id if friendship.pending == true inverse_friendship = find_friendship_complement friendship return friendship.send(pow_model_id) == model.id if friendship.created_at <= inverse_friendship.created_at return inverse_friendship.send(pow_model_id) == model.id end |
#pow_friendship_model ⇒ Object
95 96 97 |
# File 'lib/power_of_friendship.rb', line 95 def pow_friendship_model self.class.pow_friendship_model end |
#pow_friendship_table ⇒ Object
99 100 101 |
# File 'lib/power_of_friendship.rb', line 99 def pow_friendship_table self.class.pow_friendship_table end |
#pow_model_id ⇒ Object
91 92 93 |
# File 'lib/power_of_friendship.rb', line 91 def pow_model_id self.class.pow_model_id end |
#pow_name_und ⇒ Object
87 88 89 |
# File 'lib/power_of_friendship.rb', line 87 def pow_name_und self.class.pow_name_und end |
#suggested_friends ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/power_of_friendship.rb', line 181 def suggested_friends pow_model_id_column = pow_model_id # TODO figure out how to avoid this table = pow_friendship_table # TODO figure out how to avoid this self_table = "#{pow_name_und}s" approved_friendships = pow_friendship_model.where{ ( __send__(pow_model_id_column) == my{id}) & ( pending == false ) } pending_friendships = pow_friendship_model.where{ ( ( __send__(pow_model_id_column) == my{id}) | ( friend_id == my{id}) ) & ( pending == true ) } buddies_relations = pow_friendship_model.where{ ( __send__(pow_model_id_column).in(approved_friendships.select{friend_id} ) ) & ( __send__(pow_model_id_column) != my{id} ) } potential = self.class.where{ ( __send__(table).__send__(pow_model_id_column).in(buddies_relations.select{friend_id} ) ) & ( __send__(table).pending == false) & ( __send__(table).friend_id.in(approved_friendships.select{friend_id} ) ) & ( __send__(table).__send__(pow_model_id_column) != my{id} ) & ( __send__(table).__send__(pow_model_id_column).not_in(pending_friendships.select{ __send__(table).__send__(pow_model_id_column)} ) ) & ( __send__(table).__send__(pow_model_id_column).not_in(pending_friendships.select{ __send__(table).friend_id} ) ) & ( __send__(table).__send__(pow_model_id_column).not_in(approved_friendships.select{ __send__(table).friend_id} ) ) }.select{ ["#{self_table}.id", "#{self_table}.*", __send__(table).count.as(:jcount)] }.joins(:friendships).group{ [__send__(table).__send__(pow_model_id_column), "#{self_table}.id"] }.order("jcount DESC") potential end |
#unfollow(model) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/power_of_friendship.rb', line 134 def unfollow(model) followership = pow_friendship_model.where(pow_model_id => self.id, :friend_id => model.id).first return true if not followership followership.destroy if followership.approved? other_followership = find_friendship_complement followership other_followership.pending = true other_followership.save end return true end |
#unfriend(friend) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/power_of_friendship.rb', line 122 def unfriend(friend) friendship = self.find_any_friendship_with friend return true if not friendship friendship.destroy self.destroy_complimentary_friendship friendship end |