amistad 0.5.0 → 0.5.8

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ spec/db/*.db
5
+ Gemfile.lock
6
+ nbproject
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ mtime
data/CHANGELOG CHANGED
@@ -1 +1,22 @@
1
- A history of changes to the gem...
1
+ Version 0.5.8 (08.12.2010)
2
+ --------------------------
3
+ - made find_any_friendship_with public
4
+
5
+ Version 0.5.7 (07.12.2010)
6
+ --------------------------
7
+ - fixed bug with invitation approving
8
+ - cleaned up the invite and approve methods
9
+
10
+ Version 0.5.6 (30.11.2010)
11
+ --------------------------
12
+ - added has_invited? method in FriendModel
13
+ - added is_connected_with? method in FriendModel
14
+ - improved was_invited_by?
15
+
16
+ Version 0.5.5 (29.11.2010)
17
+ --------------------------
18
+ - added unblock method in FriendModel
19
+
20
+ Version 0.5.2 (28.11.2010)
21
+ --------------------------
22
+ - Migrated gem to bundler
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in amistad.gemspec
4
+ gemspec
data/README.markdown CHANGED
@@ -11,7 +11,11 @@ Just run the following command :
11
11
  Then in your Gemfile add the following line :
12
12
 
13
13
  gem 'amistad'
14
-
14
+
15
+ And run
16
+
17
+ bundle install
18
+
15
19
  ## Usage ##
16
20
 
17
21
  First generate a friendship model :
@@ -26,7 +30,7 @@ This commands create a new model called __friendship__ in *'app/models'* :
26
30
 
27
31
  It also creates a new migration for the friendship model so don't forget to migrate your database :
28
32
 
29
- db:migrate
33
+ rake db:migrate
30
34
 
31
35
  Then activate __amistad__ in your user model :
32
36
 
@@ -80,15 +84,24 @@ It is also possible to check if two users are friends :
80
84
 
81
85
  @john.is_friend_with? @jane #=> true
82
86
  @victoria.is_friend_with? @john #=> false
83
-
84
- You can also check if a user invited anoter :
87
+
88
+ You can also check if a user is somehow connected to another :
89
+
90
+ @john.is_connected_with? @jane #=> true
91
+ @victoria.is_connected_with? @john #=> true
92
+
93
+ You can also check if a user was invited by anoter :
85
94
 
86
95
  @john.was_invited_by? @john #=> true
87
96
  @victoria.was_invited_by? @john #=> false
88
-
97
+
98
+ You can also check if a user invited another :
99
+
100
+ @john.has_invited? @jane #=> true
101
+
89
102
  You can also find the friends that two users have in common :
90
103
 
91
- @john.common_friend_with(@peter) #=> [@jane]
104
+ @john.common_friends_with(@peter) #=> [@jane]
92
105
 
93
106
  ### Removing friendships ###
94
107
 
@@ -108,7 +121,17 @@ The __block()__ method allow a user to block a friendship with another user :
108
121
  To get the blocked users :
109
122
 
110
123
  @jane.blocked #=> [@john]
111
-
124
+
125
+ ### Unblocking friendship ###
126
+
127
+ The __unblock()__ method allow a user to unblock previously blocked friendship with another user :
128
+
129
+ @jane.block @john
130
+ @jane.blocked #=> [@john]
131
+
132
+ @jane.unblock @john
133
+ @jane.blocked #=> []
134
+
112
135
  ## Testing the gem ##
113
136
 
114
137
  It is possible to test amistad by running the following command from the gem directory:
@@ -117,6 +140,10 @@ It is possible to test amistad by running the following command from the gem dir
117
140
 
118
141
  Remember that amistad is only compatible with ActiveRecord 3.x.
119
142
 
143
+ ## Acknowledgement ##
144
+
145
+ * David Czarnecki : block friendships
146
+ * Adrian Dulić : unblock friendships (and many other improvements)
120
147
 
121
148
  ## Note on Patches/Pull Requests ##
122
149
 
@@ -128,4 +155,4 @@ Remember that amistad is only compatible with ActiveRecord 3.x.
128
155
 
129
156
  ## Copyright ##
130
157
 
131
- Copyright © 2010 Rawane ZOSSOU. See LICENSE for details.
158
+ Copyright © 2010 Rawane ZOSSOU. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,28 +1,7 @@
1
- require 'rake'
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
2
3
 
3
- begin
4
- require 'jeweler'
5
- Jeweler::Tasks.new do |gemspec|
6
- gemspec.name = "amistad"
7
- gemspec.summary = "Adds friendships management into a rails 3.0 application"
8
- gemspec.description = "Extends your user model with friendships management methods"
9
- gemspec.email = "dev@raw1z.fr"
10
- gemspec.homepage = "http://github.com/raw1z/amistad"
11
- gemspec.authors = ["Rawane ZOSSOU"]
12
- gemspec.files = FileList["[A-Z]*", "{lib}/**/*"]
13
- end
14
- rescue LoadError
15
- puts "Jeweler not available. Install it with: gem install jeweler"
16
- end
17
-
18
- require 'rspec/core'
4
+ require 'rspec'
19
5
  require 'rspec/core/rake_task'
20
- Rspec::Core::RakeTask.new(:spec) do |spec|
21
- end
22
-
23
- Rspec::Core::RakeTask.new(:rcov) do |spec|
24
- end
25
-
26
- task :spec => :check_dependencies
27
6
 
28
- task :default => :spec
7
+ Rspec::Core::RakeTask.new(:spec)
data/amistad.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "amistad/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "amistad"
7
+ s.version = Amistad::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Rawane ZOSSOU"]
10
+ s.email = ["dev@raw1z.fr"]
11
+ s.homepage = "http://github.com/raw1z/amistad"
12
+ s.summary = %q{Adds friendships management into a rails 3.0 application}
13
+ s.description = %q{Extends your user model with friendships management methods}
14
+
15
+ s.rubyforge_project = "amistad"
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"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ s.require_paths = ["lib"]
26
+ end
data/lib/amistad.rb CHANGED
@@ -1,2 +1,5 @@
1
- require File.join(File.dirname(__FILE__), 'amistad/friend_model')
2
- require File.join(File.dirname(__FILE__), 'amistad/friendship_model')
1
+ require 'amistad/friend_model'
2
+ require 'amistad/friendship_model'
3
+
4
+ module Amistad
5
+ end
@@ -38,21 +38,15 @@ module Amistad
38
38
  module InstanceMethods
39
39
  # suggest a user to become a friend. If the operation succeeds, the method returns true, else false
40
40
  def invite(user)
41
- return false if user == self
42
-
43
- friendship = find_any_friendship_with(user)
44
- return false if not friendship.nil?
45
-
46
- friendship = Friendship.new(:user_id => self.id, :friend_id => user.id)
47
- friendship.save
41
+ return false if user == self || find_any_friendship_with(user)
42
+ Friendship.new(:user_id => self.id, :friend_id => user.id).save
48
43
  end
49
44
 
50
45
  # approve a friendship invitation. If the operation succeeds, the method returns true, else false
51
46
  def approve(user)
52
47
  friendship = find_any_friendship_with(user)
53
- return false if friendship.nil?
54
- friendship.pending = false
55
- friendship.save
48
+ return false if friendship.nil? || has_invited?(user)
49
+ friendship.update_attribute(:pending, false)
56
50
  end
57
51
 
58
52
  # returns the list of approved friends
@@ -63,9 +57,16 @@ module Amistad
63
57
  # blocks a friendship request
64
58
  def block(user)
65
59
  friendship = find_any_friendship_with(user)
66
- return false if friendship.nil?
60
+ return false if friendship.nil? || friendship.user == self
67
61
  friendship.blocked = true
68
- friendship.pending = false
62
+ friendship.save
63
+ end
64
+
65
+ # unblocks a friendship
66
+ def unblock(user)
67
+ friendship = find_any_friendship_with(user)
68
+ return false if friendship.nil? || friendship.user == self
69
+ friendship.blocked = false
69
70
  friendship.save
70
71
  end
71
72
 
@@ -81,22 +82,29 @@ module Amistad
81
82
  def is_friend_with?(user)
82
83
  friends.include?(user)
83
84
  end
84
-
85
+
86
+ def is_connected_with?(user)
87
+ !find_any_friendship_with(user).nil?
88
+ end
89
+
85
90
  # checks if a user send a friendship's invitation
86
91
  def was_invited_by?(user)
87
- inverse_friendships.each do |friendship|
88
- return true if friendship.user == user
89
- end
90
- false
92
+ friendship = find_any_friendship_with(user)
93
+ return false if friendship.nil?
94
+ friendship.user == user
95
+ end
96
+
97
+ def has_invited?(user)
98
+ friendship = find_any_friendship_with(user)
99
+ return false if friendship.nil?
100
+ friendship.friend == user
91
101
  end
92
102
 
93
103
  # return the list of the ones among its friends which are also friend with the given use
94
104
  def common_friends_with(user)
95
105
  self.friends & user.friends
96
106
  end
97
-
98
- private
99
-
107
+
100
108
  def find_any_friendship_with(user)
101
109
  friendship = Friendship.where(:user_id => self.id, :friend_id => user.id).first
102
110
  if friendship.nil?
@@ -104,8 +112,6 @@ module Amistad
104
112
  end
105
113
  friendship
106
114
  end
107
-
108
115
  end
109
116
  end
110
117
  end
111
-
@@ -0,0 +1,3 @@
1
+ module Amistad
2
+ VERSION = "0.5.8"
3
+ end
@@ -6,9 +6,11 @@ class CreateFriendships < ActiveRecord::Migration
6
6
  t.boolean :pending, :default => true
7
7
  t.boolean :blocked, :default => false
8
8
  end
9
+
10
+ add_index :friendships, [:user_id, :friend_id], :unique => true
9
11
  end
10
12
 
11
13
  def self.down
12
14
  drop_table :friendships
13
15
  end
14
- end
16
+ end
@@ -1,3 +1,3 @@
1
1
  class Friendship < ActiveRecord::Base
2
2
  include Amistad::FriendshipModel
3
- end
3
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  describe Amistad::FriendModel do
4
4
 
@@ -25,13 +25,21 @@ describe Amistad::FriendModel do
25
25
  @jane.approve(@john).should == true
26
26
  @john.approve(@victoria).should == true
27
27
  end
28
-
28
+
29
29
  it "could not create a new friendship with a user which is already a friend" do
30
30
  @john.invite(@jane).should == true
31
31
  @john.invite(@jane).should == false
32
32
  @jane.invite(@john).should == false
33
33
  end
34
-
34
+
35
+ it "could not approve self requested friendship" do
36
+ @john.invite(@jane).should == true
37
+ @victoria.invite(@john).should == true
38
+
39
+ @john.approve(@jane).should == false
40
+ @victoria.approve(@john).should == false
41
+ end
42
+
35
43
  it "could not create a friendship with himself" do
36
44
  @john.invite(@john).should == false
37
45
  end
@@ -44,6 +52,7 @@ describe Amistad::FriendModel do
44
52
  context "when listing friendships" do
45
53
  before(:each) do
46
54
  Friendship.delete_all
55
+
47
56
  @john.invite(@jane).should == true
48
57
  @john.invite(@james).should == true
49
58
 
@@ -83,11 +92,22 @@ describe Amistad::FriendModel do
83
92
  it "checks if a user is a friend" do
84
93
  @john.is_friend_with?(@james).should == true
85
94
  @john.is_friend_with?(@mary).should == true
86
-
95
+
87
96
  @john.is_friend_with?(@jane).should == false
88
97
  @john.is_friend_with?(@peter).should == false
89
98
  end
90
-
99
+
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
103
+
104
+ @john.is_connected_with?(@peter).should == true
105
+ @peter.is_connected_with?(@john).should == true
106
+
107
+ @victoria.is_connected_with?(@john).should == false
108
+ @john.is_connected_with?(@victoria).should == false
109
+ end
110
+
91
111
  it "checks if a user was invited by another" do
92
112
  @jane.was_invited_by?(@john).should == true
93
113
  @james.was_invited_by?(@john).should == true
@@ -95,7 +115,18 @@ describe Amistad::FriendModel do
95
115
  @john.was_invited_by?(@jane).should == false
96
116
  @victoria.was_invited_by?(@john).should == false
97
117
  end
98
-
118
+
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
122
+
123
+ @jane.has_invited?(@john).should == false
124
+ @james.has_invited?(@john).should == false
125
+
126
+ @victoria.has_invited?(@john).should == false
127
+ @john.has_invited?(@victoria).should == false
128
+ end
129
+
99
130
  it "lists the friends he has in common with another user" do
100
131
  @james.common_friends_with(@mary).count.should == 1
101
132
  @james.common_friends_with(@mary).should include(@john)
@@ -105,6 +136,7 @@ describe Amistad::FriendModel do
105
136
  context "when removing friendships" do
106
137
  before(:each) do
107
138
  Friendship.delete_all
139
+
108
140
  @victoria.invite(@mary).should == true
109
141
  @victoria.invite(@john).should == true
110
142
  @victoria.invite(@elisabeth).should == true
@@ -166,6 +198,11 @@ describe Amistad::FriendModel do
166
198
  @david.block(@victoria).should == false
167
199
  end
168
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
+
169
206
  it "should return the correct number of friends blocked" do
170
207
  @david.blocked.count.should == 1
171
208
  @david.blocked.should include(@john)
@@ -192,4 +229,43 @@ describe Amistad::FriendModel do
192
229
  end
193
230
  end
194
231
 
195
- end
232
+ context "when unblocking friendships" do
233
+ before(:each) do
234
+ Friendship.delete_all
235
+
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
242
+ end
243
+
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
249
+ end
250
+
251
+ it "should return the correct number of friends blocked" do
252
+ @john.blocked.count.should == 0
253
+ @david.blocked.count.should == 0
254
+ end
255
+
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)
260
+
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)
269
+ end
270
+ end
271
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  describe Amistad::FriendshipModel do
4
4
 
@@ -23,4 +23,4 @@ describe Amistad::FriendshipModel do
23
23
  friendship = Friendship.create(:user_id => 1, :friend_id => 2)
24
24
  friendship.blocked?.should == false
25
25
  end
26
- end
26
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,5 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'rspec/core'
4
- require 'rspec/autorun'
1
+ require 'rspec'
5
2
  require 'active_record'
6
- require 'logger'
7
3
  require 'amistad'
8
4
 
9
5
  ActiveRecord::Base.establish_connection(
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 5
8
- - 0
9
- version: 0.5.0
8
+ - 8
9
+ version: 0.5.8
10
10
  platform: ruby
11
11
  authors:
12
12
  - Rawane ZOSSOU
@@ -14,27 +14,91 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-24 00:00:00 +02:00
17
+ date: 2010-12-08 00:00:00 +01:00
18
18
  default_executable:
19
- dependencies: []
20
-
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 0
31
+ - 0
32
+ version: 1.0.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 2
46
+ - 0
47
+ version: 2.2.0
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: activerecord
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 3
60
+ - 0
61
+ - 0
62
+ version: 3.0.0
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: sqlite3-ruby
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 1
75
+ - 3
76
+ - 2
77
+ version: 1.3.2
78
+ type: :development
79
+ version_requirements: *id004
21
80
  description: Extends your user model with friendships management methods
22
- email: dev@raw1z.fr
81
+ email:
82
+ - dev@raw1z.fr
23
83
  executables: []
24
84
 
25
85
  extensions: []
26
86
 
27
- extra_rdoc_files:
28
- - README.markdown
87
+ extra_rdoc_files: []
88
+
29
89
  files:
90
+ - .gitignore
91
+ - .rspec
30
92
  - CHANGELOG
93
+ - Gemfile
31
94
  - LICENCE
32
95
  - README.markdown
33
96
  - Rakefile
34
- - VERSION
97
+ - amistad.gemspec
35
98
  - lib/amistad.rb
36
99
  - lib/amistad/friend_model.rb
37
100
  - lib/amistad/friendship_model.rb
101
+ - lib/amistad/version.rb
38
102
  - lib/generators/amistad/install/install_generator.rb
39
103
  - lib/generators/amistad/install/templates/create_friendships.rb
40
104
  - lib/generators/amistad/install/templates/friendship.rb
@@ -68,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
132
  version: "0"
69
133
  requirements: []
70
134
 
71
- rubyforge_project:
135
+ rubyforge_project: amistad
72
136
  rubygems_version: 1.3.7
73
137
  signing_key:
74
138
  specification_version: 3
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.5.0