amistad 0.7.5 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -1
- data/Gemfile.lock +56 -39
- data/README.markdown +29 -126
- data/Rakefile +35 -1
- data/amistad.gemspec +8 -3
- data/lib/amistad.rb +14 -9
- data/lib/amistad/active_record_friend_model.rb +157 -0
- data/lib/amistad/active_record_friendship_model.rb +52 -0
- data/lib/amistad/config.rb +21 -0
- data/lib/amistad/friend_model.rb +10 -4
- data/lib/amistad/friendship_model.rb +2 -2
- data/lib/amistad/friendships.rb +19 -0
- data/lib/amistad/mongo_friend_model.rb +168 -0
- data/lib/amistad/mongo_mapper_friend_model.rb +58 -0
- data/lib/amistad/mongoid_friend_model.rb +58 -0
- data/lib/amistad/version.rb +1 -1
- data/lib/generators/amistad/install/install_generator.rb +0 -1
- data/lib/generators/amistad/install/templates/create_friendships.rb +2 -2
- data/spec/activerecord/activerecord_spec_helper.rb +11 -24
- data/spec/activerecord/friend_custom_model_spec.rb +17 -0
- data/spec/activerecord/friend_spec.rb +16 -0
- data/spec/activerecord/friendship_spec.rb +13 -0
- data/spec/activerecord/friendship_with_custom_friend_model_spec.rb +18 -0
- data/spec/mongo_mapper/friend_custom_model_spec.rb +27 -0
- data/spec/mongo_mapper/friend_spec.rb +17 -0
- data/spec/mongo_mapper/mongo_mapper_spec_helper.rb +9 -0
- data/spec/mongoid/friend_custom_model_spec.rb +27 -0
- data/spec/mongoid/friend_spec.rb +17 -0
- data/spec/mongoid/mongoid_spec_helper.rb +8 -7
- data/spec/spec_helper.rb +33 -4
- data/spec/{activerecord/activerecord_friendship_model_spec.rb → support/activerecord/friendship_examples.rb} +11 -23
- data/spec/support/activerecord/schema.rb +40 -0
- data/spec/support/friend_examples.rb +2 -12
- data/spec/support/parameterized_models.rb +19 -0
- metadata +177 -36
- data/lib/amistad/active_record/friend_model.rb +0 -146
- data/lib/amistad/active_record/friendship_model.rb +0 -50
- data/lib/amistad/mongoid/friend_model.rb +0 -195
- data/lib/generators/amistad/install/templates/friendship.rb +0 -3
- data/spec/activerecord/activerecord_friend_model_spec.rb +0 -13
- data/spec/mongoid/mongoid_friend_model_spec.rb +0 -47
@@ -1,50 +0,0 @@
|
|
1
|
-
module Amistad
|
2
|
-
module ActiveRecord
|
3
|
-
module FriendshipModel
|
4
|
-
def self.included(receiver)
|
5
|
-
receiver.class_exec do
|
6
|
-
include InstanceMethods
|
7
|
-
|
8
|
-
belongs_to :user
|
9
|
-
belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"
|
10
|
-
belongs_to :blocker, :class_name => "User", :foreign_key => "blocker_id"
|
11
|
-
|
12
|
-
validates_presence_of :user_id, :friend_id
|
13
|
-
validates_uniqueness_of :friend_id, :scope => :user_id
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
module InstanceMethods
|
18
|
-
# returns true if a friendship has been approved, else false
|
19
|
-
def approved?
|
20
|
-
!self.pending
|
21
|
-
end
|
22
|
-
|
23
|
-
# returns true if a friendship has not been approved, else false
|
24
|
-
def pending?
|
25
|
-
self.pending
|
26
|
-
end
|
27
|
-
|
28
|
-
# returns true if a friendship has been blocked, else false
|
29
|
-
def blocked?
|
30
|
-
self.blocker_id.present?
|
31
|
-
end
|
32
|
-
|
33
|
-
# returns true if a friendship has not beed blocked, else false
|
34
|
-
def active?
|
35
|
-
self.blocker_id.nil?
|
36
|
-
end
|
37
|
-
|
38
|
-
# returns true if a friendship can be blocked by given user
|
39
|
-
def can_block?(user)
|
40
|
-
active? && (approved? || (pending? && self.friend == user))
|
41
|
-
end
|
42
|
-
|
43
|
-
# returns true if a friendship can be unblocked by given user
|
44
|
-
def can_unblock?(user)
|
45
|
-
blocked? && self.blocker == user
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
@@ -1,195 +0,0 @@
|
|
1
|
-
module Amistad
|
2
|
-
module Mongoid
|
3
|
-
module FriendModel
|
4
|
-
def self.included(receiver)
|
5
|
-
receiver.class_exec do
|
6
|
-
field :friend_ids, :type => Array, :default => [], :accessible => true
|
7
|
-
field :inverse_friend_ids, :type => Array, :default => [], :accessible => true
|
8
|
-
field :pending_friend_ids, :type => Array, :default => [], :accessible => true
|
9
|
-
field :pending_inverse_friend_ids, :type => Array, :default => [], :accessible => true
|
10
|
-
field :blocked_friend_ids, :type => Array, :default => [], :accessible => true
|
11
|
-
field :blocked_inverse_friend_ids, :type => Array, :default => [], :accessible => true
|
12
|
-
field :blocked_pending_friend_ids, :type => Array, :default => [], :accessible => true
|
13
|
-
field :blocked_pending_inverse_friend_ids, :type => Array, :default => [], :accessible => true
|
14
|
-
|
15
|
-
%w(friend_ids inverse_friend_ids pending_friend_ids pending_inverse_friend_ids blocked_friend_ids blocked_inverse_friend_ids blocked_pending_friend_ids blocked_pending_inverse_friend_ids).each do |attribute|
|
16
|
-
define_method(attribute.to_sym) do
|
17
|
-
value = read_attribute(attribute)
|
18
|
-
write_attribute(attribute, value = []) if value.nil?
|
19
|
-
value
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
include InstanceMethods
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
module InstanceMethods
|
28
|
-
# suggest a user to become a friend. If the operation succeeds, the method returns true, else false
|
29
|
-
def invite(user)
|
30
|
-
return false if friendshiped_with?(user) or user == self or blocked?(user)
|
31
|
-
pending_friend_ids << user.id
|
32
|
-
user.pending_inverse_friend_ids << self.id
|
33
|
-
self.save && user.save
|
34
|
-
end
|
35
|
-
|
36
|
-
# approve a friendship invitation. If the operation succeeds, the method returns true, else false
|
37
|
-
def approve(user)
|
38
|
-
return false unless pending_inverse_friend_ids.include?(user.id) && user.pending_friend_ids.include?(self.id)
|
39
|
-
pending_inverse_friend_ids.delete(user.id)
|
40
|
-
user.pending_friend_ids.delete(self.id)
|
41
|
-
inverse_friend_ids << user.id
|
42
|
-
user.friend_ids << self.id
|
43
|
-
self.save && user.save
|
44
|
-
end
|
45
|
-
|
46
|
-
# returns the list of approved friends
|
47
|
-
def friends
|
48
|
-
self.invited + self.invited_by
|
49
|
-
end
|
50
|
-
|
51
|
-
# total # of invited and invited_by without association loading
|
52
|
-
def total_friends
|
53
|
-
(friend_ids + inverse_friend_ids).count
|
54
|
-
end
|
55
|
-
|
56
|
-
# return the list of invited friends
|
57
|
-
def invited
|
58
|
-
self.class.find(friend_ids)
|
59
|
-
end
|
60
|
-
|
61
|
-
# return the list of friends who invited
|
62
|
-
def invited_by
|
63
|
-
self.class.find(inverse_friend_ids)
|
64
|
-
end
|
65
|
-
|
66
|
-
# return the list of pending invited friends
|
67
|
-
def pending_invited
|
68
|
-
self.class.find(pending_friend_ids)
|
69
|
-
end
|
70
|
-
|
71
|
-
# return the list of pending friends who invited
|
72
|
-
def pending_invited_by
|
73
|
-
self.class.find(pending_inverse_friend_ids)
|
74
|
-
end
|
75
|
-
|
76
|
-
# return the list of the ones among its friends which are also friend with the given use
|
77
|
-
def common_friends_with(user)
|
78
|
-
self.friends & user.friends
|
79
|
-
end
|
80
|
-
|
81
|
-
# checks if a user is a friend
|
82
|
-
def friend_with?(user)
|
83
|
-
return false if user == self
|
84
|
-
(friend_ids + inverse_friend_ids).include?(user.id)
|
85
|
-
end
|
86
|
-
|
87
|
-
# checks if a current user is connected to given user
|
88
|
-
def connected_with?(user)
|
89
|
-
friendshiped_with?(user)
|
90
|
-
end
|
91
|
-
|
92
|
-
# checks if a current user received invitation from given user
|
93
|
-
def invited_by?(user)
|
94
|
-
user.friend_ids.include?(self.id) or user.pending_friend_ids.include?(self.id)
|
95
|
-
end
|
96
|
-
|
97
|
-
# checks if a current user invited given user
|
98
|
-
def invited?(user)
|
99
|
-
self.friend_ids.include?(user.id) or self.pending_friend_ids.include?(user.id)
|
100
|
-
end
|
101
|
-
|
102
|
-
# deletes a friendship
|
103
|
-
def remove_friendship(user)
|
104
|
-
friend_ids.delete(user.id)
|
105
|
-
user.inverse_friend_ids.delete(self.id)
|
106
|
-
inverse_friend_ids.delete(user.id)
|
107
|
-
user.friend_ids.delete(self.id)
|
108
|
-
pending_friend_ids.delete(user.id)
|
109
|
-
user.pending_inverse_friend_ids.delete(self.id)
|
110
|
-
pending_inverse_friend_ids.delete(user.id)
|
111
|
-
user.pending_friend_ids.delete(self.id)
|
112
|
-
self.save && user.save
|
113
|
-
end
|
114
|
-
|
115
|
-
# blocks a friendship
|
116
|
-
def block(user)
|
117
|
-
if inverse_friend_ids.include?(user.id)
|
118
|
-
inverse_friend_ids.delete(user.id)
|
119
|
-
user.friend_ids.delete(self.id)
|
120
|
-
blocked_inverse_friend_ids << user.id
|
121
|
-
elsif pending_inverse_friend_ids.include?(user.id)
|
122
|
-
pending_inverse_friend_ids.delete(user.id)
|
123
|
-
user.pending_friend_ids.delete(self.id)
|
124
|
-
blocked_pending_inverse_friend_ids << user.id
|
125
|
-
elsif friend_ids.include?(user.id)
|
126
|
-
friend_ids.delete(user.id)
|
127
|
-
user.inverse_friend_ids.delete(user.id)
|
128
|
-
blocked_friend_ids << user.id
|
129
|
-
else
|
130
|
-
return false
|
131
|
-
end
|
132
|
-
|
133
|
-
self.save
|
134
|
-
end
|
135
|
-
|
136
|
-
# unblocks a friendship
|
137
|
-
def unblock(user)
|
138
|
-
if blocked_inverse_friend_ids.include?(user.id)
|
139
|
-
blocked_inverse_friend_ids.delete(user.id)
|
140
|
-
user.blocked_friend_ids.delete(self.id)
|
141
|
-
inverse_friend_ids << user.id
|
142
|
-
user.friend_ids << self.id
|
143
|
-
elsif blocked_pending_inverse_friend_ids.include?(user.id)
|
144
|
-
blocked_pending_inverse_friend_ids.delete(user.id)
|
145
|
-
pending_inverse_friend_ids << user.id
|
146
|
-
user.pending_friend_ids << self.id
|
147
|
-
elsif blocked_friend_ids.include?(user.id)
|
148
|
-
blocked_friend_ids.delete(user.id)
|
149
|
-
user.blocked_inverse_friend_ids.delete(self.id)
|
150
|
-
friend_ids << user.id
|
151
|
-
user.inverse_friend_ids << self.id
|
152
|
-
else
|
153
|
-
return false
|
154
|
-
end
|
155
|
-
|
156
|
-
self.save && user.save
|
157
|
-
end
|
158
|
-
|
159
|
-
# returns the list of blocked friends
|
160
|
-
def blocked
|
161
|
-
blocked_ids = blocked_friend_ids + blocked_inverse_friend_ids + blocked_pending_inverse_friend_ids
|
162
|
-
self.class.find(blocked_ids)
|
163
|
-
end
|
164
|
-
|
165
|
-
# total # of blockades and blockedes_by without association loading
|
166
|
-
def total_blocked
|
167
|
-
(blocked_friend_ids + blocked_inverse_friend_ids + blocked_pending_inverse_friend_ids).count
|
168
|
-
end
|
169
|
-
|
170
|
-
# checks if a user is blocked
|
171
|
-
def blocked?(user)
|
172
|
-
(blocked_friend_ids + blocked_inverse_friend_ids + blocked_pending_inverse_friend_ids).include?(user.id) or user.blocked_pending_inverse_friend_ids.include?(self.id)
|
173
|
-
end
|
174
|
-
|
175
|
-
# check if any friendship exists with another user
|
176
|
-
def friendshiped_with?(user)
|
177
|
-
(friend_ids + inverse_friend_ids + pending_friend_ids + pending_inverse_friend_ids + blocked_friend_ids).include?(user.id)
|
178
|
-
end
|
179
|
-
|
180
|
-
# deletes all the friendships
|
181
|
-
def delete_all_friendships
|
182
|
-
friend_ids.clear
|
183
|
-
inverse_friend_ids.clear
|
184
|
-
pending_friend_ids.clear
|
185
|
-
pending_inverse_friend_ids.clear
|
186
|
-
blocked_friend_ids.clear
|
187
|
-
blocked_inverse_friend_ids.clear
|
188
|
-
blocked_pending_friend_ids.clear
|
189
|
-
blocked_pending_inverse_friend_ids.clear
|
190
|
-
self.save
|
191
|
-
end
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/activerecord_spec_helper"
|
2
|
-
|
3
|
-
describe Amistad::ActiveRecord::FriendModel do
|
4
|
-
def reset_friendships
|
5
|
-
Friendship.delete_all
|
6
|
-
end
|
7
|
-
|
8
|
-
before(:all) do
|
9
|
-
create_users
|
10
|
-
end
|
11
|
-
|
12
|
-
it_should_behave_like "a friend model"
|
13
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/mongoid_spec_helper'
|
2
|
-
|
3
|
-
describe Amistad::Mongoid::FriendModel do
|
4
|
-
def reset_friendships
|
5
|
-
%w(john jane david james peter mary victoria elisabeth).each do |var|
|
6
|
-
eval "@#{var}.delete_all_friendships.should be_true"
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
context "When users are created after activating amistad" do
|
11
|
-
before(:all) do
|
12
|
-
# create a user model with amistad activated
|
13
|
-
Object.send(:remove_const, :User) if Object.const_defined?(:User)
|
14
|
-
User = Class.new
|
15
|
-
User.class_exec do
|
16
|
-
include Mongoid::Document
|
17
|
-
include Amistad::FriendModel
|
18
|
-
field :name, :require => true
|
19
|
-
end
|
20
|
-
|
21
|
-
# create the users
|
22
|
-
create_users
|
23
|
-
end
|
24
|
-
|
25
|
-
it_should_behave_like "a friend model"
|
26
|
-
end
|
27
|
-
|
28
|
-
context "When users are created before activating amistad" do
|
29
|
-
before(:all) do
|
30
|
-
# create a user model without amistad activated
|
31
|
-
Object.send(:remove_const, :User) if Object.const_defined?(:User)
|
32
|
-
User = Class.new
|
33
|
-
User.class_exec do
|
34
|
-
include Mongoid::Document
|
35
|
-
field :name, :require => true
|
36
|
-
end
|
37
|
-
|
38
|
-
# create the users
|
39
|
-
create_users
|
40
|
-
|
41
|
-
# activate amistad
|
42
|
-
User.class_exec { include Amistad::FriendModel }
|
43
|
-
end
|
44
|
-
|
45
|
-
it_should_behave_like "a friend model"
|
46
|
-
end
|
47
|
-
end
|