amistad 0.5.8 → 0.6.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/CHANGELOG CHANGED
@@ -1,3 +1,12 @@
1
+ Version 0.6.0 (06.01.2011)
2
+ --------------------------
3
+ - improved blocking and unblocking friends
4
+ - method is_friend_with? changed to friend_with?
5
+ - method is_connected_with? changed to connected_with?
6
+ - method was_invited_by? changed to invited_by?
7
+ - method has_invited? changed to invited?
8
+ - added method blocked?
9
+
1
10
  Version 0.5.8 (08.12.2010)
2
11
  --------------------------
3
12
  - made find_any_friendship_with public
data/README.markdown CHANGED
@@ -82,22 +82,22 @@ To get the pending friendships use :
82
82
 
83
83
  It is also possible to check if two users are friends :
84
84
 
85
- @john.is_friend_with? @jane #=> true
86
- @victoria.is_friend_with? @john #=> false
85
+ @john.friend_with? @jane #=> true
86
+ @victoria.friend_with? @john #=> false
87
87
 
88
88
  You can also check if a user is somehow connected to another :
89
89
 
90
- @john.is_connected_with? @jane #=> true
91
- @victoria.is_connected_with? @john #=> true
90
+ @john.connected_with? @jane #=> true
91
+ @victoria.connected_with? @john #=> true
92
92
 
93
93
  You can also check if a user was invited by anoter :
94
94
 
95
- @john.was_invited_by? @john #=> true
96
- @victoria.was_invited_by? @john #=> false
95
+ @john.invited_by? @john #=> true
96
+ @victoria.invited_by? @john #=> false
97
97
 
98
98
  You can also check if a user invited another :
99
99
 
100
- @john.has_invited? @jane #=> true
100
+ @john.invited? @jane #=> true
101
101
 
102
102
  You can also find the friends that two users have in common :
103
103
 
@@ -122,6 +122,10 @@ To get the blocked users :
122
122
 
123
123
  @jane.blocked #=> [@john]
124
124
 
125
+ You can also check if a user is blocked :
126
+
127
+ @jane.blocked? @john #=> true
128
+
125
129
  ### Unblocking friendship ###
126
130
 
127
131
  The __unblock()__ method allow a user to unblock previously blocked friendship with another user :
data/amistad.gemspec CHANGED
@@ -14,10 +14,10 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.rubyforge_project = "amistad"
16
16
 
17
- s.add_development_dependency "bundler", ">= 1.0.0"
18
- s.add_development_dependency "rspec", "~> 2.2.0"
19
- s.add_development_dependency "activerecord", "~> 3.0.0"
20
- s.add_development_dependency "sqlite3-ruby", ">= 1.3.2"
17
+ s.add_development_dependency "bundler", "~>1.0.7"
18
+ s.add_development_dependency "rspec", "~> 2.4.0"
19
+ s.add_development_dependency "activerecord", "~> 3.0.3"
20
+ s.add_development_dependency "sqlite3-ruby", "1.3.2"
21
21
 
22
22
  s.files = `git ls-files`.split("\n")
23
23
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
data/lib/amistad.rb CHANGED
@@ -1,5 +1,2 @@
1
1
  require 'amistad/friend_model'
2
2
  require 'amistad/friendship_model'
3
-
4
- module Amistad
5
- end
@@ -3,35 +3,42 @@ module Amistad
3
3
  def self.included(receiver)
4
4
  receiver.class_exec do
5
5
  include InstanceMethods
6
-
6
+
7
7
  has_many :friendships
8
-
8
+
9
9
  has_many :pending_invited,
10
10
  :through => :friendships,
11
11
  :source => :friend,
12
- :conditions => { :'friendships.pending' => true, :'friendships.blocked' => false }
13
-
12
+ :conditions => { :'friendships.pending' => true, :'friendships.blocker_id' => nil }
13
+
14
14
  has_many :invited,
15
15
  :through => :friendships,
16
16
  :source => :friend,
17
- :conditions => { :'friendships.pending' => false, :'friendships.blocked' => false }
17
+ :conditions => { :'friendships.pending' => false, :'friendships.blocker_id' => nil }
18
18
 
19
19
  has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
20
-
20
+
21
21
  has_many :pending_invited_by,
22
22
  :through => :inverse_friendships,
23
23
  :source => :user,
24
- :conditions => { :'friendships.pending' => true, :'friendships.blocked' => false }
25
-
24
+ :conditions => { :'friendships.pending' => true, :'friendships.blocker_id' => nil }
25
+
26
26
  has_many :invited_by,
27
27
  :through => :inverse_friendships,
28
28
  :source => :user,
29
- :conditions => { :'friendships.pending' => false, :'friendships.blocked' => false }
30
-
31
- has_many :blocked,
32
- :through => :inverse_friendships,
29
+ :conditions => { :'friendships.pending' => false, :'friendships.blocker_id' => nil }
30
+
31
+ has_many :blocked_friendships, :class_name => "Friendship", :foreign_key => "blocker_id"
32
+
33
+ has_many :blockades,
34
+ :through => :blocked_friendships,
35
+ :source => :friend,
36
+ :conditions => "friend_id <> blocker_id"
37
+
38
+ has_many :blockades_by,
39
+ :through => :blocked_friendships,
33
40
  :source => :user,
34
- :conditions => { :'friendships.blocked' => true }
41
+ :conditions => "user_id <> blocker_id"
35
42
  end
36
43
  end
37
44
 
@@ -41,70 +48,80 @@ module Amistad
41
48
  return false if user == self || find_any_friendship_with(user)
42
49
  Friendship.new(:user_id => self.id, :friend_id => user.id).save
43
50
  end
44
-
51
+
45
52
  # approve a friendship invitation. If the operation succeeds, the method returns true, else false
46
53
  def approve(user)
47
54
  friendship = find_any_friendship_with(user)
48
- return false if friendship.nil? || has_invited?(user)
55
+ return false if friendship.nil? || invited?(user)
49
56
  friendship.update_attribute(:pending, false)
50
57
  end
51
-
58
+
59
+ # deletes a friendship
60
+ def remove(user)
61
+ friendship = find_any_friendship_with(user)
62
+ return false if friendship.nil?
63
+ friendship.destroy && friendship.destroyed?
64
+ end
65
+
52
66
  # returns the list of approved friends
53
67
  def friends
54
68
  self.invited(true) + self.invited_by(true)
55
69
  end
56
-
57
- # blocks a friendship request
70
+
71
+ # blocks a friendship
58
72
  def block(user)
59
73
  friendship = find_any_friendship_with(user)
60
- return false if friendship.nil? || friendship.user == self
61
- friendship.blocked = true
62
- friendship.save
74
+ return false if friendship.nil? || !friendship.can_block?(self)
75
+ friendship.update_attribute(:blocker, self)
63
76
  end
64
77
 
65
78
  # unblocks a friendship
66
79
  def unblock(user)
67
80
  friendship = find_any_friendship_with(user)
68
- return false if friendship.nil? || friendship.user == self
69
- friendship.blocked = false
70
- friendship.save
81
+ return false if friendship.nil? || !friendship.can_unblock?(self)
82
+ friendship.update_attribute(:blocker, nil)
71
83
  end
72
-
73
- # deletes a friendship
74
- def remove(user)
75
- friendship = find_any_friendship_with(user)
76
- return false if friendship.nil?
77
- friendship.destroy
78
- friendship.destroyed?
84
+
85
+ # returns the list of blocked friends
86
+ def blocked
87
+ self.blockades(true) + self.blockades_by(true)
88
+ end
89
+
90
+ # checks if a user is blocked
91
+ def blocked?(user)
92
+ blocked.include?(user)
79
93
  end
80
-
94
+
81
95
  # checks if a user is a friend
82
- def is_friend_with?(user)
96
+ def friend_with?(user)
83
97
  friends.include?(user)
84
98
  end
85
99
 
86
- def is_connected_with?(user)
87
- !find_any_friendship_with(user).nil?
100
+ # checks if a current user is connected to given user
101
+ def connected_with?(user)
102
+ find_any_friendship_with(user).present?
88
103
  end
89
104
 
90
- # checks if a user send a friendship's invitation
91
- def was_invited_by?(user)
105
+ # checks if a current user received invitation from given user
106
+ def invited_by?(user)
92
107
  friendship = find_any_friendship_with(user)
93
108
  return false if friendship.nil?
94
109
  friendship.user == user
95
110
  end
96
111
 
97
- def has_invited?(user)
112
+ # checks if a current user invited given user
113
+ def invited?(user)
98
114
  friendship = find_any_friendship_with(user)
99
115
  return false if friendship.nil?
100
116
  friendship.friend == user
101
117
  end
102
-
118
+
103
119
  # return the list of the ones among its friends which are also friend with the given use
104
120
  def common_friends_with(user)
105
121
  self.friends & user.friends
106
122
  end
107
-
123
+
124
+ # returns friendship with given user or nil
108
125
  def find_any_friendship_with(user)
109
126
  friendship = Friendship.where(:user_id => self.id, :friend_id => user.id).first
110
127
  if friendship.nil?
@@ -1,29 +1,48 @@
1
1
  module Amistad
2
2
  module FriendshipModel
3
-
4
3
  def self.included(receiver)
5
4
  receiver.class_exec do
6
5
  include InstanceMethods
7
-
6
+
8
7
  belongs_to :user
9
8
  belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"
10
-
9
+ belongs_to :blocker, :class_name => "User", :foreign_key => "blocker_id"
10
+
11
11
  validates_presence_of :user_id, :friend_id
12
12
  validates_uniqueness_of :friend_id, :scope => :user_id
13
13
  end
14
14
  end
15
-
15
+
16
16
  module InstanceMethods
17
+ # returns true if a friendship has been approved, else false
18
+ def approved?
19
+ !self.pending
20
+ end
21
+
17
22
  # returns true if a friendship has not been approved, else false
18
23
  def pending?
19
24
  self.pending
20
25
  end
21
-
26
+
22
27
  # returns true if a friendship has been blocked, else false
23
28
  def blocked?
24
- self.blocked
29
+ self.blocker_id.present?
30
+ end
31
+
32
+ # returns true if a friendship has not beed blocked, else false
33
+ def active?
34
+ self.blocker_id.nil?
35
+ end
36
+
37
+ # returns true if a friendship can be blocked by given user
38
+ def can_block?(user)
39
+ active? && (approved? || (pending? && self.friend == user))
40
+ end
41
+
42
+ # returns true if a friendship can be unblocked by given user
43
+ def can_unblock?(user)
44
+ blocked? && self.blocker == user
25
45
  end
26
46
  end
27
-
28
47
  end
29
48
  end
@@ -1,3 +1,3 @@
1
1
  module Amistad
2
- VERSION = "0.5.8"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -3,8 +3,8 @@ class CreateFriendships < ActiveRecord::Migration
3
3
  create_table :friendships do |t|
4
4
  t.integer :user_id
5
5
  t.integer :friend_id
6
+ t.integer :blocker_id
6
7
  t.boolean :pending, :default => true
7
- t.boolean :blocked, :default => false
8
8
  end
9
9
 
10
10
  add_index :friendships, [:user_id, :friend_id], :unique => true
@@ -1,271 +1,394 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Amistad::FriendModel do
4
-
4
+
5
5
  before(:all) do
6
- %w(John Jane david James Peter Mary Victoria Elisabeth).each do |name|
7
- eval "@#{name.downcase} = User.create(:name => '#{name}')"
6
+ %w(John Jane David James Peter Mary Victoria Elisabeth).each do |name|
7
+ instance_variable_set("@#{name.downcase}".to_sym, User.create(:name => name))
8
8
  end
9
9
  end
10
-
10
+
11
11
  context "when creating friendships" do
12
- before(:each) do
12
+ before do
13
13
  Friendship.delete_all
14
14
  end
15
-
16
- it "requests friendships with other users" do
17
- @john.invite(@jane).should == true
18
- @victoria.invite(@john).should == true
15
+
16
+ it "should invite other users to friends" do
17
+ @john.invite(@jane).should be_true
18
+ @victoria.invite(@john).should be_true
19
19
  end
20
20
 
21
- it "approves friendships requested by other users" do
22
- @john.invite(@jane).should == true
23
- @victoria.invite(@john).should == true
21
+ it "should approve only friendships requested by other users" do
22
+ @john.invite(@jane).should be_true
23
+ @jane.approve(@john).should be_true
24
+ @victoria.invite(@john).should be_true
25
+ @john.approve(@victoria).should be_true
26
+ end
24
27
 
25
- @jane.approve(@john).should == true
26
- @john.approve(@victoria).should == true
28
+ it "should not invite an already invited user" do
29
+ @john.invite(@jane).should be_true
30
+ @john.invite(@jane).should be_false
31
+ @jane.invite(@john).should be_false
27
32
  end
28
33
 
29
- it "could not create a new friendship with a user which is already a friend" do
30
- @john.invite(@jane).should == true
31
- @john.invite(@jane).should == false
32
- @jane.invite(@john).should == false
34
+ it "should not invite an already approved user" do
35
+ @john.invite(@jane).should be_true
36
+ @jane.approve(@john).should be_true
37
+ @jane.invite(@john).should be_false
38
+ @john.invite(@jane).should be_false
33
39
  end
34
40
 
35
- it "could not approve self requested friendship" do
36
- @john.invite(@jane).should == true
37
- @victoria.invite(@john).should == true
41
+ it "should not invite an already blocked user" do
42
+ @john.invite(@jane).should be_true
43
+ @jane.block(@john).should be_true
44
+ @jane.invite(@john).should be_false
45
+ @john.invite(@jane).should be_false
46
+ end
38
47
 
39
- @john.approve(@jane).should == false
40
- @victoria.approve(@john).should == false
48
+ it "should not approve a self requested friendship" do
49
+ @john.invite(@jane).should be_true
50
+ @john.approve(@jane).should be_false
51
+ @victoria.invite(@john).should be_true
52
+ @victoria.approve(@john).should be_false
41
53
  end
42
54
 
43
- it "could not create a friendship with himself" do
44
- @john.invite(@john).should == false
55
+ it "should not create a friendship with himself" do
56
+ @john.invite(@john).should be_false
45
57
  end
46
-
47
- it "could not approuve a non-existent friendship" do
48
- @peter.approve(@john).should == false
58
+
59
+ it "should not approve a non-existent friendship" do
60
+ @peter.approve(@john).should be_false
49
61
  end
50
62
  end
51
-
63
+
52
64
  context "when listing friendships" do
53
- before(:each) do
65
+ before do
54
66
  Friendship.delete_all
67
+ @john.invite(@jane).should be_true
68
+ @peter.invite(@john).should be_true
69
+ @john.invite(@james).should be_true
70
+ @james.approve(@john).should be_true
71
+ @mary.invite(@john).should be_true
72
+ @john.approve(@mary).should be_true
73
+ end
55
74
 
56
- @john.invite(@jane).should == true
57
- @john.invite(@james).should == true
58
-
59
- @peter.invite(@john).should == true
60
- @mary.invite(@john).should == true
61
-
62
- @james.approve(@john).should == true
63
- @john.approve(@mary).should == true
75
+ it "should list all the friends" do
76
+ @john.friends.should =~ [@mary, @james]
64
77
  end
65
-
66
- it "lists all the friends" do
67
- @john.friends.count.should == 2
68
- @john.friends.should include(@james)
69
- @john.friends.should include(@mary)
78
+
79
+ it "should not list non-friended users" do
80
+ @john.friends.should =~ [@mary, @james]
81
+ @john.friends.should_not include(@peter)
82
+ @john.friends.should_not include(@victoria)
70
83
  end
71
-
72
- it "lists the friends who invited him" do
73
- @john.invited_by.should include(@mary)
74
- @john.invited_by.should_not include(@peter)
84
+
85
+ it "should list the friends who invited him" do
86
+ @john.invited_by.should == [@mary]
75
87
  end
76
-
77
- it "lists the friends who was invited by him" do
78
- @john.invited.should include(@james)
79
- @john.invited.should_not include(@jane)
88
+
89
+ it "should list the friends who were invited by him" do
90
+ @john.invited.should == [@james]
80
91
  end
81
-
82
- it "lists the pending friends who invited him" do
83
- @john.pending_invited_by.should_not include(@mary)
84
- @john.pending_invited_by.should include(@peter)
92
+
93
+ it "should list the pending friends who invited him" do
94
+ @john.pending_invited_by.should == [@peter]
85
95
  end
86
96
 
87
- it "lists the pending friends who was invited by him" do
88
- @john.pending_invited.should_not include(@james)
89
- @john.pending_invited.should include(@jane)
97
+ it "should list the pending friends who were invited by him" do
98
+ @john.pending_invited.should == [@jane]
90
99
  end
91
100
 
92
- it "checks if a user is a friend" do
93
- @john.is_friend_with?(@james).should == true
94
- @john.is_friend_with?(@mary).should == true
101
+ it "should list the friends he has in common with another user" do
102
+ @james.common_friends_with(@mary).should == [@john]
103
+ end
95
104
 
96
- @john.is_friend_with?(@jane).should == false
97
- @john.is_friend_with?(@peter).should == false
105
+ it "should not list the friends he does not have in common" do
106
+ @john.common_friends_with(@mary).count.should == 0
107
+ @john.common_friends_with(@mary).should_not include(@james)
108
+ @john.common_friends_with(@peter).count.should == 0
109
+ @john.common_friends_with(@peter).should_not include(@jane)
98
110
  end
99
111
 
100
- it "checks if a user has any connection with another user" do
101
- @john.is_connected_with?(@jane).should == true
102
- @jane.is_connected_with?(@john).should == true
112
+ it "should check if a user is a friend" do
113
+ @john.friend_with?(@mary).should be_true
114
+ @mary.friend_with?(@john).should be_true
115
+ @john.friend_with?(@james).should be_true
116
+ @james.friend_with?(@john).should be_true
117
+ end
103
118
 
104
- @john.is_connected_with?(@peter).should == true
105
- @peter.is_connected_with?(@john).should == true
119
+ it "should check if a user is not a friend" do
120
+ @john.friend_with?(@jane).should be_false
121
+ @jane.friend_with?(@john).should be_false
122
+ @john.friend_with?(@peter).should be_false
123
+ @peter.friend_with?(@john).should be_false
124
+ end
106
125
 
107
- @victoria.is_connected_with?(@john).should == false
108
- @john.is_connected_with?(@victoria).should == false
126
+ it "should check if a user has any connections with another user" do
127
+ @john.connected_with?(@jane).should be_true
128
+ @jane.connected_with?(@john).should be_true
129
+ @john.connected_with?(@peter).should be_true
130
+ @peter.connected_with?(@john).should be_true
109
131
  end
110
132
 
111
- it "checks if a user was invited by another" do
112
- @jane.was_invited_by?(@john).should == true
113
- @james.was_invited_by?(@john).should == true
114
-
115
- @john.was_invited_by?(@jane).should == false
116
- @victoria.was_invited_by?(@john).should == false
133
+ it "should check if a user does not have any connections with another user" do
134
+ @victoria.connected_with?(@john).should be_false
135
+ @john.connected_with?(@victoria).should be_false
117
136
  end
118
137
 
119
- it "checks if a user has invited another user" do
120
- @john.has_invited?(@jane).should == true
121
- @john.has_invited?(@james).should == true
138
+ it "should check if a user was invited by another" do
139
+ @jane.invited_by?(@john).should be_true
140
+ @james.invited_by?(@john).should be_true
141
+ end
122
142
 
123
- @jane.has_invited?(@john).should == false
124
- @james.has_invited?(@john).should == false
143
+ it "should check if a user was not invited by another" do
144
+ @john.invited_by?(@jane).should be_false
145
+ @victoria.invited_by?(@john).should be_false
146
+ end
125
147
 
126
- @victoria.has_invited?(@john).should == false
127
- @john.has_invited?(@victoria).should == false
148
+ it "should check if a user has invited another user" do
149
+ @john.invited?(@jane).should be_true
150
+ @john.invited?(@james).should be_true
128
151
  end
129
152
 
130
- it "lists the friends he has in common with another user" do
131
- @james.common_friends_with(@mary).count.should == 1
132
- @james.common_friends_with(@mary).should include(@john)
153
+ it "should check if a user did not invite another user" do
154
+ @jane.invited?(@john).should be_false
155
+ @james.invited?(@john).should be_false
156
+ @john.invited?(@victoria).should be_false
157
+ @victoria.invited?(@john).should be_false
133
158
  end
134
159
  end
135
-
160
+
136
161
  context "when removing friendships" do
137
- before(:each) do
162
+ before do
138
163
  Friendship.delete_all
164
+ @jane.invite(@james).should be_true
165
+ @james.approve(@jane).should be_true
166
+ @james.invite(@victoria).should be_true
167
+ @victoria.approve(@james).should be_true
168
+ @victoria.invite(@mary).should be_true
169
+ @mary.approve(@victoria).should be_true
170
+ @victoria.invite(@john).should be_true
171
+ @john.approve(@victoria).should be_true
172
+ @peter.invite(@victoria).should be_true
173
+ @victoria.invite(@elisabeth).should be_true
174
+ end
139
175
 
140
- @victoria.invite(@mary).should == true
141
- @victoria.invite(@john).should == true
142
- @victoria.invite(@elisabeth).should == true
143
-
144
- @james.invite(@victoria).should == true
145
- @peter.invite(@victoria).should == true
146
- @jane.invite(@victoria).should == true
147
-
148
- @mary.approve(@victoria).should == true
149
- @john.approve(@victoria).should == true
150
- @victoria.approve(@james).should == true
151
- @victoria.approve(@jane).should == true
152
- end
153
-
154
- it "removes the friends invited by him" do
155
- @victoria.remove(@mary).should == true
156
-
157
- @victoria.invited.count.should == 1
158
- @victoria.invited.should include(@john)
176
+ it "should remove the friends invited by him" do
177
+ @victoria.friends.size.should == 3
178
+ @victoria.friends.should include(@mary)
179
+ @victoria.invited.should include(@mary)
180
+ @mary.friends.size.should == 1
181
+ @mary.friends.should include(@victoria)
182
+ @mary.invited_by.should include(@victoria)
183
+ @victoria.remove(@mary).should be_true
184
+ @victoria.friends.size.should == 2
159
185
  @victoria.friends.should_not include(@mary)
160
-
161
- @mary.invited_by.should_not include(@victoria)
186
+ @victoria.invited.should_not include(@mary)
187
+ @mary.friends.size.should == 0
162
188
  @mary.friends.should_not include(@victoria)
189
+ @mary.invited_by.should_not include(@victoria)
163
190
  end
164
-
165
- it "removes the friends who invited him" do
166
- @victoria.remove(@james).should == true
167
-
168
- @victoria.invited_by.count.should == 1
169
- @victoria.invited_by.should include(@jane)
191
+
192
+ it "should remove the friends who invited him" do
193
+ @victoria.friends.size.should == 3
194
+ @victoria.friends.should include(@james)
195
+ @victoria.invited_by.should include(@james)
196
+ @james.friends.size.should == 2
197
+ @james.friends.should include(@victoria)
198
+ @james.invited.should include(@victoria)
199
+ @victoria.remove(@james).should be_true
200
+ @victoria.friends.size.should == 2
170
201
  @victoria.friends.should_not include(@james)
171
-
172
- @james.invited.should_not include(@victoria)
202
+ @victoria.invited_by.should_not include(@james)
203
+ @james.friends.size.should == 1
173
204
  @james.friends.should_not include(@victoria)
205
+ @james.invited.should_not include(@victoria)
174
206
  end
175
-
176
- it "removes the pending friends invited by him" do
177
- @victoria.remove(@elisabeth).should == true
178
- @victoria.pending_invited.count.should == 0
207
+
208
+ it "should remove the pending friends invited by him" do
209
+ @victoria.pending_invited.size.should == 1
210
+ @victoria.pending_invited.should include(@elisabeth)
211
+ @elisabeth.pending_invited_by.size.should == 1
212
+ @elisabeth.pending_invited_by.should include(@victoria)
213
+ @victoria.remove(@elisabeth).should be_true
214
+ [@victoria, @elisabeth].map(&:reload)
215
+ @victoria.pending_invited.size.should == 0
216
+ @victoria.pending_invited.should_not include(@elisabeth)
217
+ @elisabeth.pending_invited_by.size.should == 0
179
218
  @elisabeth.pending_invited_by.should_not include(@victoria)
180
219
  end
181
-
182
- it "removes the pending friends who invited him" do
183
- @victoria.remove(@peter).should == true
220
+
221
+ it "should remove the pending friends who invited him" do
222
+ @victoria.pending_invited_by.count.should == 1
223
+ @victoria.pending_invited_by.should include(@peter)
224
+ @peter.pending_invited.count.should == 1
225
+ @peter.pending_invited.should include(@victoria)
226
+ @victoria.remove(@peter).should be_true
227
+ [@victoria, @peter].map(&:reload)
184
228
  @victoria.pending_invited_by.count.should == 0
229
+ @victoria.pending_invited_by.should_not include(@peter)
230
+ @peter.pending_invited.count.should == 0
185
231
  @peter.pending_invited.should_not include(@victoria)
186
232
  end
187
233
  end
188
-
234
+
189
235
  context "when blocking friendships" do
190
- before(:each) do
236
+ before do
191
237
  Friendship.delete_all
238
+ @john.invite(@james).should be_true
239
+ @james.approve(@john).should be_true
240
+ @james.block(@john).should be_true
241
+ @mary.invite(@victoria).should be_true
242
+ @victoria.approve(@mary).should be_true
243
+ @victoria.block(@mary).should be_true
244
+ @victoria.invite(@david).should be_true
245
+ @david.block(@victoria).should be_true
246
+ @john.invite(@david).should be_true
247
+ @david.block(@john).should be_true
248
+ @peter.invite(@elisabeth).should be_true
249
+ @elisabeth.block(@peter).should be_true
250
+ @jane.invite(@john).should be_true
251
+ @jane.invite(@james).should be_true
252
+ @james.approve(@jane).should be_true
253
+ @victoria.invite(@jane).should be_true
254
+ @victoria.invite(@james).should be_true
255
+ @james.approve(@victoria).should be_true
256
+ end
192
257
 
193
- @john.invite(@jane).should == true
194
- @john.invite(@james).should == true
195
- @john.invite(@david).should == true
196
- @james.approve(@john).should == true
197
- @david.block(@john).should == true
198
- @david.block(@victoria).should == false
199
- end
200
-
201
- it "should allow to block only invited user" do
202
- @jane.block(@john).should == true
203
- @john.block(@jane).should == false
204
- end
205
-
206
- it "should return the correct number of friends blocked" do
207
- @david.blocked.count.should == 1
208
- @david.blocked.should include(@john)
209
- end
210
-
211
- it "should not return any blocked friends in friends or invited or pending invites" do
212
- @john.friends.count.should == 1
213
- @john.friends.should include(@james)
214
- @john.friends.should_not include(@david)
215
-
216
- @john.invited.count.should == 1
217
- @john.invited.should_not include(@david)
218
- @john.invited.should include(@james)
219
-
220
- @john.pending_invited.count.should == 1
221
- @john.pending_invited.should_not include(@david)
222
- @john.pending_invited.should include(@jane)
223
-
224
- @david.friends.count.should == 0
225
- end
226
-
227
- it "should not return any blocked friends for the friend who was blocked" do
228
- @john.blocked.count.should == 0
229
- end
258
+ it "should allow to block author of the invitation by invited user" do
259
+ @john.block(@jane).should be_true
260
+ @jane.block(@victoria).should be_true
261
+ end
262
+
263
+ it "should not allow to block invited user by invitation author" do
264
+ @jane.block(@john).should be_false
265
+ @victoria.block(@jane).should be_false
266
+ end
267
+
268
+ it "should allow to block approved users on both sides" do
269
+ @james.block(@jane).should be_true
270
+ @victoria.block(@james).should be_true
271
+ end
272
+
273
+ it "should not allow to block not connected user" do
274
+ @david.block(@peter).should be_false
275
+ @peter.block(@david).should be_false
276
+ end
277
+
278
+ it "should not allow to block already blocked user" do
279
+ @john.block(@jane).should be_true
280
+ @john.block(@jane).should be_false
281
+ @james.block(@jane).should be_true
282
+ @james.block(@jane).should be_false
283
+ end
284
+
285
+ it "should list the blocked users" do
286
+ @jane.blocked.should be_empty
287
+ @peter.blocked.should be_empty
288
+ @james.blocked.should == [@john]
289
+ @victoria.blocked.should == [@mary]
290
+ @david.blocked.should =~ [@john, @victoria]
291
+ end
292
+
293
+ it "should not list blocked users in friends" do
294
+ @james.friends.should =~ [@jane, @victoria]
295
+ @james.blocked.each do |user|
296
+ @james.friends.should_not include(user)
297
+ user.friends.should_not include(@james)
298
+ end
299
+ end
300
+
301
+ it "should not list blocked users in invited" do
302
+ @victoria.invited.should == [@james]
303
+ @victoria.blocked.each do |user|
304
+ @victoria.invited.should_not include(user)
305
+ user.invited_by.should_not include(@victoria)
306
+ end
307
+ end
308
+
309
+ it "should not list blocked users in invited pending by" do
310
+ @david.pending_invited_by.should be_empty
311
+ @david.blocked.each do |user|
312
+ @david.pending_invited_by.should_not include(user)
313
+ user.pending_invited.should_not include(@david)
314
+ end
315
+ end
316
+
317
+ it "should check if a user is blocked" do
318
+ @james.blocked?(@john).should be_true
319
+ @victoria.blocked?(@mary).should be_true
320
+ @david.blocked?(@john).should be_true
321
+ @david.blocked?(@victoria).should be_true
322
+ end
230
323
  end
231
324
 
232
325
  context "when unblocking friendships" do
233
- before(:each) do
326
+ before do
234
327
  Friendship.delete_all
328
+ @john.invite(@james).should be_true
329
+ @james.approve(@john).should be_true
330
+ @john.block(@james).should be_true
331
+ @john.unblock(@james).should be_true
332
+ @mary.invite(@victoria).should be_true
333
+ @victoria.approve(@mary).should be_true
334
+ @victoria.block(@mary).should be_true
335
+ @victoria.unblock(@mary).should be_true
336
+ @victoria.invite(@david).should be_true
337
+ @david.block(@victoria).should be_true
338
+ @david.unblock(@victoria).should be_true
339
+ @john.invite(@david).should be_true
340
+ @david.block(@john).should be_true
341
+ @peter.invite(@elisabeth).should be_true
342
+ @elisabeth.block(@peter).should be_true
343
+ @jane.invite(@john).should be_true
344
+ @jane.invite(@james).should be_true
345
+ @james.approve(@jane).should be_true
346
+ @victoria.invite(@jane).should be_true
347
+ @victoria.invite(@james).should be_true
348
+ @james.approve(@victoria).should be_true
349
+ end
235
350
 
236
- @john.invite(@jane).should == true
237
- @john.invite(@david).should == true
238
- @david.approve(@john).should == true
239
- @david.block(@john).should == true
240
- @david.unblock(@john).should == true
241
- @john.unblock(@victoria).should == false
351
+ it "should allow to unblock prevoiusly blocked user" do
352
+ @david.unblock(@john).should be_true
353
+ @elisabeth.unblock(@peter).should be_true
242
354
  end
243
355
 
244
- it "should allow to unblock only invited user" do
245
- @jane.approve(@john).should == true
246
- @jane.block(@john).should == true
247
- @john.unblock(@jane).should == false
248
- @jane.unblock(@john).should == true
356
+ it "should not allow to unblock not prevoiusly blocked user" do
357
+ @john.unblock(@jane).should be_false
358
+ @james.unblock(@jane).should be_false
359
+ @victoria.unblock(@jane).should be_false
360
+ @james.unblock(@victoria).should be_false
249
361
  end
250
362
 
251
- it "should return the correct number of friends blocked" do
252
- @john.blocked.count.should == 0
253
- @david.blocked.count.should == 0
363
+ it "should not allow to unblock blocked user by himself" do
364
+ @john.unblock(@david).should be_false
365
+ @peter.unblock(@elisabeth).should be_false
254
366
  end
255
367
 
256
- it "should return unblocked users in friends" do
257
- @john.friends.count.should == 1
258
- @john.friends.should include(@david)
259
- @john.friends.should_not include(@victoria)
368
+ it "should list unblocked users in friends" do
369
+ @john.friends.should == [@james]
370
+ @mary.friends.should == [@victoria]
371
+ @victoria.friends.should =~ [@mary, @james]
372
+ @james.friends.should =~ [@john, @jane, @victoria]
373
+ end
374
+
375
+ it "should list unblocked users in invited" do
376
+ @john.invited.should == [@james]
377
+ @mary.invited.should == [@victoria]
378
+ end
379
+
380
+ it "should list unblocked users in invited by" do
381
+ @victoria.invited_by.should == [@mary]
382
+ @james.invited_by.should =~ [@john, @jane, @victoria]
383
+ end
384
+
385
+ it "should list unblocked users in pending invited" do
386
+ @victoria.pending_invited.should =~ [@jane, @david]
387
+ end
260
388
 
261
- @john.invited.count.should == 1
262
- @john.invited.should include(@david)
263
- @john.invited.should_not include(@victoria)
264
-
265
- @john.pending_invited.count.should == 1
266
- @john.pending_invited.should include(@jane)
267
- @john.pending_invited.should_not include(@david)
268
- @john.pending_invited.should_not include(@victoria)
389
+ it "should list unblocked users in pending invited by" do
390
+ @david.reload
391
+ @david.pending_invited_by.should == [@victoria]
269
392
  end
270
393
  end
271
394
  end
@@ -1,26 +1,122 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Amistad::FriendshipModel do
4
-
5
- before(:each) do
6
- Friendship.delete_all
4
+
5
+ before(:all) do
6
+ %w(Jane David).each do |name|
7
+ instance_variable_set("@#{name.downcase}".to_sym, User.create(:name => name))
8
+ end
7
9
  end
8
-
9
- it "validates presence of the user's id and the friend's id" do
10
+
11
+ it "should validate presence of the user's id and the friend's id" do
10
12
  friendship = Friendship.new
11
- friendship.save.should == false
12
-
13
- friendship = Friendship.new(:user_id => 1, :friend_id => 2)
14
- friendship.save.should == true
13
+ friendship.valid?.should be_false
14
+ friendship.errors.should include(:user_id)
15
+ friendship.errors.should include(:friend_id)
16
+ friendship.errors.size.should == 2
17
+ end
18
+
19
+ context "when creating friendship" do
20
+ before do
21
+ Friendship.delete_all
22
+ @jane.invite(@david)
23
+ @friendship = Friendship.first
24
+ end
25
+
26
+ it "should be pending" do
27
+ @friendship.pending?.should be_true
28
+ end
29
+
30
+ it "should not be approved" do
31
+ @friendship.approved?.should be_false
32
+ end
33
+
34
+ it "should be active" do
35
+ @friendship.active?.should be_true
36
+ end
37
+
38
+ it "should not be blocked" do
39
+ @friendship.blocked?.should be_false
40
+ end
41
+
42
+ it "should be available to block only by invited user" do
43
+ @friendship.can_block?(@david).should be_true
44
+ @friendship.can_block?(@sane).should be_false
45
+ end
46
+
47
+ it "should not be availabel to unblock" do
48
+ @friendship.can_unblock?(@jane).should be_false
49
+ @friendship.can_unblock?(@david).should be_false
50
+ end
15
51
  end
16
-
17
- it "is in pending state when created" do
18
- friendship = Friendship.create(:user_id => 1, :friend_id => 2)
19
- friendship.pending?.should == true
52
+
53
+ context "when approving friendship" do
54
+ before do
55
+ Friendship.delete_all
56
+ @jane.invite(@david)
57
+ @david.approve(@jane)
58
+ @friendship = Friendship.first
59
+ end
60
+
61
+ it "should be approved" do
62
+ @friendship.approved?.should be_true
63
+ end
64
+
65
+ it "should not be pending" do
66
+ @friendship.pending?.should be_false
67
+ end
68
+
69
+ it "should be active" do
70
+ @friendship.active?.should be_true
71
+ end
72
+
73
+ it "should not be blocked" do
74
+ @friendship.blocked?.should be_false
75
+ end
76
+
77
+ it "should be available to block by both users" do
78
+ @friendship.can_block?(@sane).should be_true
79
+ @friendship.can_block?(@david).should be_true
80
+ end
81
+
82
+ it "should not be availabel to unblock" do
83
+ @friendship.can_unblock?(@jane).should be_false
84
+ @friendship.can_unblock?(@david).should be_false
85
+ end
20
86
  end
21
-
22
- it "is not in a blocked state when created" do
23
- friendship = Friendship.create(:user_id => 1, :friend_id => 2)
24
- friendship.blocked?.should == false
87
+
88
+ context "when blocking friendship" do
89
+ before do
90
+ Friendship.delete_all
91
+ @jane.invite(@david)
92
+ @david.block(@jane)
93
+ @friendship = Friendship.first
94
+ end
95
+
96
+ it "should not be approved" do
97
+ @friendship.approved?.should be_false
98
+ end
99
+
100
+ it "should be pending" do
101
+ @friendship.pending?.should be_true
102
+ end
103
+
104
+ it "should not be active" do
105
+ @friendship.active?.should be_false
106
+ end
107
+
108
+ it "should be blocked" do
109
+ @friendship.blocked?.should be_true
110
+ end
111
+
112
+ it "should not be available to block" do
113
+ @friendship.can_block?(@sane).should be_false
114
+ @friendship.can_block?(@david).should be_false
115
+ end
116
+
117
+ it "should be available to unblock only by user who blocked it" do
118
+ @friendship.can_unblock?(@david).should be_true
119
+ @friendship.can_unblock?(@jane).should be_false
120
+ end
25
121
  end
26
122
  end
data/spec/spec_helper.rb CHANGED
@@ -17,9 +17,11 @@ ActiveRecord::Schema.define do
17
17
  create_table :friendships, :force => true do |t|
18
18
  t.integer :user_id
19
19
  t.integer :friend_id
20
+ t.integer :blocker_id
20
21
  t.boolean :pending, :default => true
21
- t.boolean :blocked, :default => false
22
22
  end
23
+
24
+ add_index :friendships, [:user_id, :friend_id], :unique => true
23
25
  end
24
26
 
25
27
  class User < ActiveRecord::Base
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 5
8
- - 8
9
- version: 0.5.8
7
+ - 6
8
+ - 0
9
+ version: 0.6.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Rawane ZOSSOU
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-08 00:00:00 +01:00
17
+ date: 2011-01-10 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -23,13 +23,13 @@ dependencies:
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ">="
26
+ - - ~>
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
29
  - 1
30
30
  - 0
31
- - 0
32
- version: 1.0.0
31
+ - 7
32
+ version: 1.0.7
33
33
  type: :development
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
@@ -42,9 +42,9 @@ dependencies:
42
42
  - !ruby/object:Gem::Version
43
43
  segments:
44
44
  - 2
45
- - 2
45
+ - 4
46
46
  - 0
47
- version: 2.2.0
47
+ version: 2.4.0
48
48
  type: :development
49
49
  version_requirements: *id002
50
50
  - !ruby/object:Gem::Dependency
@@ -58,8 +58,8 @@ dependencies:
58
58
  segments:
59
59
  - 3
60
60
  - 0
61
- - 0
62
- version: 3.0.0
61
+ - 3
62
+ version: 3.0.3
63
63
  type: :development
64
64
  version_requirements: *id003
65
65
  - !ruby/object:Gem::Dependency
@@ -68,7 +68,7 @@ dependencies:
68
68
  requirement: &id004 !ruby/object:Gem::Requirement
69
69
  none: false
70
70
  requirements:
71
- - - ">="
71
+ - - "="
72
72
  - !ruby/object:Gem::Version
73
73
  segments:
74
74
  - 1
@@ -88,7 +88,6 @@ extra_rdoc_files: []
88
88
 
89
89
  files:
90
90
  - .gitignore
91
- - .rspec
92
91
  - CHANGELOG
93
92
  - Gemfile
94
93
  - LICENCE
data/.rspec DELETED
@@ -1,4 +0,0 @@
1
- --colour
2
- --format
3
- progress
4
- mtime