has_friends_ti2 0.0.22
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +168 -0
- data/Rakefile +2 -0
- data/has_friends_ti2.gemspec +23 -0
- data/lib/has_friends_ti2/friendship.rb +40 -0
- data/lib/has_friends_ti2/has_friends.rb +76 -0
- data/lib/has_friends_ti2/version.rb +3 -0
- data/lib/has_friends_ti2.rb +4 -0
- data/spec/has_friends_spec.rb +176 -0
- data/spec/schema.rb +18 -0
- data/spec/spec_helper.rb +113 -0
- metadata +58 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 097ffae0ab0f59a935d09428c7d045aeb4da24e8
|
|
4
|
+
data.tar.gz: 4ffde702a150b4dcef50079293719bb6c6f93450
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d67263d8d8c742a9d6da53c9e7eac92875f57d313c6b00bb929ce97ec02351853d354bd6097a85f850a77f12039af57e83670a0dbc76b598c22b7692bb53cd92
|
|
7
|
+
data.tar.gz: 2203974ac10a80eb829d175565823c3aa61e13fc593ecb3d827de53b35a9129f0ff43d1b73fe8b1148df308e957987b85860d4447cf2a7f9ef27b7d78081c24f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2015 Tiago D Cipriano
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# HasFriends
|
|
2
|
+
|
|
3
|
+
**ATTENTION:** This is a new simpler implementation.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
Add this line to your application's Gemfile:
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
gem 'has_friends'
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
And then execute:
|
|
15
|
+
|
|
16
|
+
$ bundle
|
|
17
|
+
|
|
18
|
+
Or install it yourself as:
|
|
19
|
+
|
|
20
|
+
$ gem install has_friends
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Generate a migration with:
|
|
25
|
+
```
|
|
26
|
+
rails generate migration create_friendships
|
|
27
|
+
```
|
|
28
|
+
And add the following code:
|
|
29
|
+
```
|
|
30
|
+
class CreateFriendships < ActiveRecord::Migration
|
|
31
|
+
def self.up
|
|
32
|
+
create_table :friendships do |t|
|
|
33
|
+
t.references :user, :friend
|
|
34
|
+
t.datetime :requested_at, :accepted_at, :null => true, :default => nil
|
|
35
|
+
t.string :status
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
add_index :friendships, :user_id
|
|
39
|
+
add_index :friendships, :friend_id
|
|
40
|
+
add_index :friendships, :status
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.down
|
|
44
|
+
drop_table :friendships
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
```
|
|
48
|
+
Finally, run the migrations with
|
|
49
|
+
```
|
|
50
|
+
rake db:migrate
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Usage
|
|
54
|
+
-----
|
|
55
|
+
|
|
56
|
+
Add the method call `has_friends` to your model.
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
class User < ActiveRecord::Base
|
|
60
|
+
has_friends
|
|
61
|
+
end
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
john = User.find_by_login 'john'
|
|
66
|
+
mary = User.find_by_login 'mary'
|
|
67
|
+
paul = User.find_by_login 'paul'
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
# john wants to be friend with mary
|
|
72
|
+
# always return a friendship object
|
|
73
|
+
john.be_friends_with(mary)
|
|
74
|
+
|
|
75
|
+
# are they friends?
|
|
76
|
+
john.friends?(mary)
|
|
77
|
+
|
|
78
|
+
# get the friendship object
|
|
79
|
+
john.friendship_for(mary)
|
|
80
|
+
|
|
81
|
+
# mary accepts john's request if it exists;
|
|
82
|
+
# makes a friendship request otherwise.
|
|
83
|
+
mary.be_friends_with(john)
|
|
84
|
+
|
|
85
|
+
# check if paul is mary's friend
|
|
86
|
+
mary.friends?(paul)
|
|
87
|
+
|
|
88
|
+
# check if an user is the current user, so it can
|
|
89
|
+
# be differently presented
|
|
90
|
+
mary.friends.each {|friend| friend.is?(current_user) }
|
|
91
|
+
|
|
92
|
+
# if you're dealing with a friendship object,
|
|
93
|
+
# the following methods are available
|
|
94
|
+
friendship.accept!
|
|
95
|
+
|
|
96
|
+
# if you're using has_paginate plugin, you can use it:
|
|
97
|
+
mary.friends.paginate(:page => 3, :limit => 10)
|
|
98
|
+
|
|
99
|
+
# the be_friends_with method returns 2 params: friendship object and status.
|
|
100
|
+
# the friendship object will be present only when the friendship is created
|
|
101
|
+
# (that is, when is requested for the first time)
|
|
102
|
+
# STATUS_ALREADY_FRIENDS # => users are already friends
|
|
103
|
+
# STATUS_ALREADY_REQUESTED # => user has already requested friendship
|
|
104
|
+
# STATUS_IS_YOU # => user is trying add himself as friend
|
|
105
|
+
# STATUS_FRIEND_IS_REQUIRED # => friend argument is missing
|
|
106
|
+
# STATUS_FRIENDSHIP_ACCEPTED # => friendship has been accepted
|
|
107
|
+
# STATUS_REQUESTED # => friendship has been requested
|
|
108
|
+
|
|
109
|
+
friendship, status = mary.be_friends_with(john)
|
|
110
|
+
|
|
111
|
+
if status == Friends::STATUS_REQUESTED
|
|
112
|
+
# the friendship has been requested
|
|
113
|
+
Mailer.deliver_friendship_request(friendship)
|
|
114
|
+
elsif status == Friends::STATUS_ALREADY_FRIENDS
|
|
115
|
+
# they're already friends
|
|
116
|
+
else
|
|
117
|
+
# ...
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
NOTE: You should have a User model. You should also have a `friends_count` column
|
|
121
|
+
on your model. Otherwise, this won't work! Create a new migration with `script/generate migration add_friends_count_to_user`:
|
|
122
|
+
|
|
123
|
+
class AddFriendsCountToUser < ActiveRecord::Migration
|
|
124
|
+
def self.up
|
|
125
|
+
add_column :users, :friends_count, :integer, :default => 0, :null => false
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def self.down
|
|
129
|
+
remove_column :users, :friends_count
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
## Contributing
|
|
137
|
+
|
|
138
|
+
1. Fork it ( https://github.com/TiagoTi/has_friends/fork )
|
|
139
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
140
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
141
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
142
|
+
5. Create a new Pull Request
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
LICENSE:
|
|
147
|
+
--------
|
|
148
|
+
|
|
149
|
+
(The MIT License)
|
|
150
|
+
|
|
151
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
152
|
+
a copy of this software and associated documentation files (the
|
|
153
|
+
'Software'), to deal in the Software without restriction, including
|
|
154
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
155
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
156
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
157
|
+
the following conditions:
|
|
158
|
+
|
|
159
|
+
The above copyright notice and this permission notice shall be
|
|
160
|
+
included in all copies or substantial portions of the Software.
|
|
161
|
+
|
|
162
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
163
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
164
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
165
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
166
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
167
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
168
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'has_friends_ti2/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "has_friends_ti2"
|
|
8
|
+
spec.version = HasFriends::VERSION
|
|
9
|
+
spec.authors = ["Tiago D Cipriano"]
|
|
10
|
+
spec.email = ["tiago@ti2.net.br"]
|
|
11
|
+
spec.summary = "Lib to implement friends"
|
|
12
|
+
#spec.description = %q{TODO: Write a longer description. Optional.}
|
|
13
|
+
#spec.homepage = ""
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
17
|
+
#spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
+
#spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
#spec.add_development_dependency "bundler", "~> 1.7"
|
|
22
|
+
#spec.add_development_dependency "rake", "~> 10.0"
|
|
23
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
class Friendship < ActiveRecord::Base
|
|
2
|
+
# constants
|
|
3
|
+
STATUS_ALREADY_FRIENDS = 1
|
|
4
|
+
STATUS_ALREADY_REQUESTED = 2
|
|
5
|
+
STATUS_IS_YOU = 3
|
|
6
|
+
STATUS_FRIEND_IS_REQUIRED = 4
|
|
7
|
+
STATUS_FRIENDSHIP_ACCEPTED = 5
|
|
8
|
+
STATUS_REQUESTED = 6
|
|
9
|
+
|
|
10
|
+
# scopes
|
|
11
|
+
scope :pending, -> {where(:status => 'pending')}
|
|
12
|
+
scope :accepted, -> {where(:status => 'accepted')}
|
|
13
|
+
scope :requested, -> {where(:status => 'requested')}
|
|
14
|
+
|
|
15
|
+
# associations
|
|
16
|
+
belongs_to :user
|
|
17
|
+
belongs_to :friend, :class_name => 'User', :foreign_key => 'friend_id'
|
|
18
|
+
|
|
19
|
+
# callback
|
|
20
|
+
after_destroy do |f|
|
|
21
|
+
User.decrement_counter(:friends_count, f.user_id)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def pending?
|
|
25
|
+
status == 'pending'
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def accepted?
|
|
29
|
+
status == 'accepted'
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def requested?
|
|
33
|
+
status == 'requested'
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def accept!
|
|
37
|
+
User.increment_counter(:friends_count, user.id) unless accepted?
|
|
38
|
+
update_attribute(:status, 'accepted')
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
module HasFriends
|
|
2
|
+
|
|
3
|
+
module Friends
|
|
4
|
+
module ClassMethods
|
|
5
|
+
def has_friends
|
|
6
|
+
has_many :friendships
|
|
7
|
+
has_many :friends, :through => :friendships, :source => :friend#,-> { where( status: 'accepted')} #"friendships.status = 'accepted'"
|
|
8
|
+
after_destroy :destroy_all_friendships
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
module InstanceMethods
|
|
13
|
+
def be_friends_with friend
|
|
14
|
+
# no user object
|
|
15
|
+
return nil, Friendship::STATUS_FRIEND_IS_REQUIRED unless friend
|
|
16
|
+
|
|
17
|
+
# should not create friendship if user is trying to add himself
|
|
18
|
+
return nil, Friendship::STATUS_IS_YOU if is?(friend)
|
|
19
|
+
|
|
20
|
+
# should not create friendship if users are already friends
|
|
21
|
+
return nil, Friendship::STATUS_ALREADY_FRIENDS if friends?(friend)
|
|
22
|
+
|
|
23
|
+
# retrieve the friendship request
|
|
24
|
+
friendship = self.friendship_for(friend)
|
|
25
|
+
|
|
26
|
+
# let's check if user has already a friendship request or have removed
|
|
27
|
+
request = friend.friendship_for(self)
|
|
28
|
+
|
|
29
|
+
# friendship has already been requested
|
|
30
|
+
return nil, Friendship::STATUS_ALREADY_REQUESTED if friendship && friendship.requested?
|
|
31
|
+
|
|
32
|
+
# friendship is pending so accept it
|
|
33
|
+
if friendship && friendship.pending?
|
|
34
|
+
friendship.accept!
|
|
35
|
+
request.accept!
|
|
36
|
+
|
|
37
|
+
return friendship, Friendship::STATUS_FRIENDSHIP_ACCEPTED
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# we didn't find a friendship, so let's create one!
|
|
41
|
+
friendship = self.friendships.create(:friend_id => friend.id, :status => 'requested')
|
|
42
|
+
|
|
43
|
+
# we didn't find a friendship request, so let's create it!
|
|
44
|
+
request = friend.friendships.create(:friend_id => id, :status => 'pending')
|
|
45
|
+
|
|
46
|
+
return friendship, Friendship::STATUS_REQUESTED
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def friends?(friend)
|
|
51
|
+
friendship = friendship_for(friend)
|
|
52
|
+
friendship && friendship.accepted?
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def friendship_for(friend)
|
|
56
|
+
friendships.where(:friend_id => friend.id).first
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def is?(friend)
|
|
60
|
+
self.id == friend.id
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
def destroy_all_friendships
|
|
65
|
+
Friendship.delete_all({:user_id => id})
|
|
66
|
+
Friendship.delete_all({:friend_id => id})
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def self.included(receiver)
|
|
71
|
+
receiver.extend HasFriends::Friends::ClassMethods
|
|
72
|
+
receiver.send :include, HasFriends::Friends::InstanceMethods
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
end
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
# unset models used for testing purposes
|
|
4
|
+
Object.unset_class('User')
|
|
5
|
+
|
|
6
|
+
class User < ActiveRecord::Base
|
|
7
|
+
has_friends
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
describe 'has_friends' do
|
|
11
|
+
|
|
12
|
+
before do
|
|
13
|
+
User.delete_all
|
|
14
|
+
|
|
15
|
+
@vader = User.create(:login => "darth_vader")
|
|
16
|
+
@luke = User.create(:login => "luke_skywalker")
|
|
17
|
+
@leia = User.create(:login => "princess_leia")
|
|
18
|
+
@han_solo = User.create(:login => "han_solo")
|
|
19
|
+
@yoda = User.create(:login => "yoda")
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe 'methods' do
|
|
23
|
+
it "should respond to has_friends method" do
|
|
24
|
+
expect(User).to respond_to :has_friends
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "should respond to be_friends_with method" do
|
|
28
|
+
expect( @vader ).to respond_to :be_friends_with
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "should respond to friends? method" do
|
|
32
|
+
expect( @vader ).to respond_to :friends?
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
describe "friends" do
|
|
37
|
+
before(:each) do
|
|
38
|
+
create_friendship @vader, @luke
|
|
39
|
+
create_friendship @vader, @leia
|
|
40
|
+
create_friendship @luke, @leia
|
|
41
|
+
create_friendship @luke, @yoda
|
|
42
|
+
create_friendship @leia, @han_solo
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "should order vader's friends" do
|
|
46
|
+
# => princess_leia, luke_skywalker
|
|
47
|
+
expect(@vader.friends.all).to include( @leia, @luke )
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "should return luke's friends" do
|
|
51
|
+
expect(@luke.friends).to include(@vader, @leia, @yoda)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "should return leia's frieds" do
|
|
55
|
+
expect(@leia.friends).to include(@vader, @luke, @han_solo)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "should return yoda's friends" do
|
|
59
|
+
expect(@yoda.friends).to include @luke
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it "should return solo's friends" do
|
|
63
|
+
expect(@han_solo.friends).to include @leia
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "should increment counter" do
|
|
67
|
+
@vader.reload
|
|
68
|
+
expect(@vader.friends_count).to eql 2
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it "should decrement counter" do
|
|
72
|
+
friendship = @vader.friendship_for(@luke)
|
|
73
|
+
friendship.destroy
|
|
74
|
+
|
|
75
|
+
@vader.reload
|
|
76
|
+
expect(@vader.friends_count).to eql 1
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "should be @vader" do
|
|
80
|
+
expect(@vader.is?(@vader) ).to be_truthy
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it "should not be @vader" do
|
|
84
|
+
expect( @vader.is?(@leia) ).to_not be_truthy
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
describe "friendship request" do
|
|
89
|
+
it "should return nil and 'friend is required' status" do
|
|
90
|
+
friendship, status = @vader.be_friends_with(nil)
|
|
91
|
+
|
|
92
|
+
expect(friendship).to be_nil
|
|
93
|
+
expect(status).to eql Friendship::STATUS_FRIEND_IS_REQUIRED
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it "should return nil and 'is you' status" do
|
|
97
|
+
friendship, status = @vader.be_friends_with(@vader)
|
|
98
|
+
|
|
99
|
+
expect(friendship).to be_nil
|
|
100
|
+
expect(status).to eql Friendship::STATUS_IS_YOU
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
it "should return nil and 'already friends status" do
|
|
104
|
+
@vader.be_friends_with(@luke)
|
|
105
|
+
@luke.be_friends_with(@vader)
|
|
106
|
+
friendship, status = @vader.be_friends_with(@luke)
|
|
107
|
+
|
|
108
|
+
expect(friendship).to be_nil
|
|
109
|
+
expect(status).to eql Friendship::STATUS_ALREADY_FRIENDS
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it "should return nil and 'already requested' status" do
|
|
113
|
+
@vader.be_friends_with(@luke)
|
|
114
|
+
friendship, status = @vader.be_friends_with(@luke)
|
|
115
|
+
|
|
116
|
+
expect(friendship).to be_nil
|
|
117
|
+
expect(status).to eql Friendship::STATUS_ALREADY_REQUESTED
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
it "should return friendship and 'accepted friendship' status" do
|
|
121
|
+
@vader.be_friends_with(@luke)
|
|
122
|
+
friendship, status = @luke.be_friends_with(@vader)
|
|
123
|
+
|
|
124
|
+
expect(friendship).to be_kind_of Friendship
|
|
125
|
+
expect(status).to eql Friendship::STATUS_FRIENDSHIP_ACCEPTED
|
|
126
|
+
expect(@vader).to be_friends(@luke)
|
|
127
|
+
expect(@luke).to be_friends(@vader)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
it "should create friendships" do
|
|
131
|
+
expect {
|
|
132
|
+
@vader.be_friends_with(@luke)
|
|
133
|
+
|
|
134
|
+
expect(@vader.friendships.count).to eql 1
|
|
135
|
+
expect(@luke.friendships.count).to eql 1
|
|
136
|
+
}.to change(Friendship, :count).by(2)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
describe Friendship do
|
|
141
|
+
it "should be pending status" do
|
|
142
|
+
@friendship = Friendship.new(:status => 'pending')
|
|
143
|
+
expect(@friendship).to be_pending
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
it "should be accepted status" do
|
|
147
|
+
@friendship = Friendship.new(:status => 'accepted')
|
|
148
|
+
expect(@friendship).to be_accepted
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
it "should be requested status" do
|
|
152
|
+
@friendship = Friendship.new(:status => 'requested')
|
|
153
|
+
expect(@friendship).to be_requested
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
describe "pagination" do
|
|
158
|
+
before(:each) do
|
|
159
|
+
pending unless Object.const_defined?('Paginate')
|
|
160
|
+
|
|
161
|
+
create_friendship @vader, @luke
|
|
162
|
+
create_friendship @vader, @leia
|
|
163
|
+
create_friendship @vader, @han_solo
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
it "should paginate friends" do
|
|
167
|
+
#expect(@vader.friends.paginate(:limit => 1).to_a).to include(@luke, @leia)
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
private
|
|
172
|
+
def create_friendship(user1, user2)
|
|
173
|
+
user1.be_friends_with(user2)
|
|
174
|
+
user2.be_friends_with(user1)
|
|
175
|
+
end
|
|
176
|
+
end
|
data/spec/schema.rb
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
|
2
|
+
begin
|
|
3
|
+
drop_table :users
|
|
4
|
+
drop_table :friendships
|
|
5
|
+
rescue
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
create_table :users do |t|
|
|
9
|
+
t.string :login
|
|
10
|
+
t.integer :friends_count, :default => 0, :null => false
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
create_table :friendships do |t|
|
|
14
|
+
t.references :user, :friend
|
|
15
|
+
t.string :status
|
|
16
|
+
t.timestamps null:false
|
|
17
|
+
end
|
|
18
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
require "active_record"
|
|
2
|
+
#require "spec"
|
|
3
|
+
require File.dirname(__FILE__) + "/../lib/has_friends_ti2"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
|
7
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
|
8
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
|
9
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
|
10
|
+
# files.
|
|
11
|
+
#
|
|
12
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
|
13
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
|
14
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
|
15
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
|
16
|
+
# a separate helper file that requires the additional dependencies and performs
|
|
17
|
+
# the additional setup, and require it from the spec files that actually need
|
|
18
|
+
# it.
|
|
19
|
+
#
|
|
20
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
|
21
|
+
# users commonly want.
|
|
22
|
+
#
|
|
23
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
|
24
|
+
RSpec.configure do |config|
|
|
25
|
+
# rspec-expectations config goes here. You can use an alternate
|
|
26
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
|
27
|
+
# assertions if you prefer.
|
|
28
|
+
config.expect_with :rspec do |expectations|
|
|
29
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
|
30
|
+
# and `failure_message` of custom matchers include text for helper methods
|
|
31
|
+
# defined using `chain`, e.g.:
|
|
32
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
|
33
|
+
# # => "be bigger than 2 and smaller than 4"
|
|
34
|
+
# ...rather than:
|
|
35
|
+
# # => "be bigger than 2"
|
|
36
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
|
40
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
|
41
|
+
config.mock_with :rspec do |mocks|
|
|
42
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
|
43
|
+
# a real object. This is generally recommended, and will default to
|
|
44
|
+
# `true` in RSpec 4.
|
|
45
|
+
mocks.verify_partial_doubles = true
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The settings below are suggested to provide a good initial experience
|
|
49
|
+
# with RSpec, but feel free to customize to your heart's content.
|
|
50
|
+
=begin
|
|
51
|
+
# These two settings work together to allow you to limit a spec run
|
|
52
|
+
# to individual examples or groups you care about by tagging them with
|
|
53
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
|
54
|
+
# get run.
|
|
55
|
+
config.filter_run :focus
|
|
56
|
+
config.run_all_when_everything_filtered = true
|
|
57
|
+
|
|
58
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
|
59
|
+
# recommended. For more details, see:
|
|
60
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
|
61
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
|
62
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
|
63
|
+
config.disable_monkey_patching!
|
|
64
|
+
|
|
65
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
|
66
|
+
# be too noisy due to issues in dependencies.
|
|
67
|
+
config.warnings = true
|
|
68
|
+
|
|
69
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
|
70
|
+
# file, and it's useful to allow more verbose output when running an
|
|
71
|
+
# individual spec file.
|
|
72
|
+
if config.files_to_run.one?
|
|
73
|
+
# Use the documentation formatter for detailed output,
|
|
74
|
+
# unless a formatter has already been configured
|
|
75
|
+
# (e.g. via a command-line flag).
|
|
76
|
+
config.default_formatter = 'doc'
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Print the 10 slowest examples and example groups at the
|
|
80
|
+
# end of the spec run, to help surface which specs are running
|
|
81
|
+
# particularly slow.
|
|
82
|
+
config.profile_examples = 10
|
|
83
|
+
|
|
84
|
+
# Run specs in random order to surface order dependencies. If you find an
|
|
85
|
+
# order dependency and want to debug it, you can fix the order by providing
|
|
86
|
+
# the seed, which is printed after each run.
|
|
87
|
+
# --seed 1234
|
|
88
|
+
config.order = :random
|
|
89
|
+
|
|
90
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
|
91
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
|
92
|
+
# test failures related to randomization by passing the same `--seed` value
|
|
93
|
+
# as the one that triggered the failure.
|
|
94
|
+
Kernel.srand config.seed
|
|
95
|
+
=end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
|
99
|
+
|
|
100
|
+
load("schema.rb")
|
|
101
|
+
|
|
102
|
+
class Object
|
|
103
|
+
def self.unset_class(*args)
|
|
104
|
+
class_eval do
|
|
105
|
+
args.each do |klass|
|
|
106
|
+
eval(klass) rescue nil
|
|
107
|
+
remove_const(klass) if const_defined?(klass)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
alias :doing :lambda
|
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: has_friends_ti2
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.22
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Tiago D Cipriano
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-05-11 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
- tiago@ti2.net.br
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- ".gitignore"
|
|
21
|
+
- ".rspec"
|
|
22
|
+
- Gemfile
|
|
23
|
+
- LICENSE.txt
|
|
24
|
+
- README.md
|
|
25
|
+
- Rakefile
|
|
26
|
+
- has_friends_ti2.gemspec
|
|
27
|
+
- lib/has_friends_ti2.rb
|
|
28
|
+
- lib/has_friends_ti2/friendship.rb
|
|
29
|
+
- lib/has_friends_ti2/has_friends.rb
|
|
30
|
+
- lib/has_friends_ti2/version.rb
|
|
31
|
+
- spec/has_friends_spec.rb
|
|
32
|
+
- spec/schema.rb
|
|
33
|
+
- spec/spec_helper.rb
|
|
34
|
+
homepage:
|
|
35
|
+
licenses:
|
|
36
|
+
- MIT
|
|
37
|
+
metadata: {}
|
|
38
|
+
post_install_message:
|
|
39
|
+
rdoc_options: []
|
|
40
|
+
require_paths:
|
|
41
|
+
- lib
|
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '0'
|
|
52
|
+
requirements: []
|
|
53
|
+
rubyforge_project:
|
|
54
|
+
rubygems_version: 2.2.2
|
|
55
|
+
signing_key:
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: Lib to implement friends
|
|
58
|
+
test_files: []
|