connections 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +13 -8
- data/lib/connections/connectable.rb +1 -1
- data/lib/connections/connector.rb +1 -1
- data/lib/connections/version.rb +1 -1
- data/test/connections_test.rb +58 -62
- data/test/dummy/log/test.log +1005 -0
- metadata +44 -62
data/README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# Connections
|
2
2
|
|
3
|
-
Most social apps
|
4
|
-
|
3
|
+
Most social apps need some kind of follow/like/watch/stalk/etc
|
4
|
+
features that connects one model to another.
|
5
5
|
|
6
|
-
Instead of having to recreate this functionality for every project
|
6
|
+
Instead of having to recreate this functionality for every project I
|
7
7
|
decided to create a gem that easily allows you to add these features
|
8
8
|
using any naming you prefer.
|
9
9
|
|
10
|
+
[![Build Status](https://secure.travis-ci.org/balvig/connections.png?branch=master)](http://travis-ci.org/balvig/connections)
|
11
|
+
|
10
12
|
## Installation
|
11
13
|
|
12
14
|
Add the gem to the gemfile:
|
@@ -20,7 +22,8 @@ keeping track of what is connected to what.
|
|
20
22
|
|
21
23
|
## Usage
|
22
24
|
|
23
|
-
Use `connects_with`
|
25
|
+
Use `connects_with` to enable a model to connect to other models and
|
26
|
+
`connectable_with ` to allow a model to be connected to using
|
24
27
|
whatever naming scheme you prefer (follow, like, watch, etc)
|
25
28
|
|
26
29
|
class User < ActiveRecord::Base
|
@@ -32,14 +35,16 @@ whatever naming scheme you prefer (follow, like, watch, etc)
|
|
32
35
|
connectable_with :like
|
33
36
|
end
|
34
37
|
|
35
|
-
|
38
|
+
Depending on the naming scheme you choose Connections will try to figure out the
|
39
|
+
grammar and add appropriately named methods. For example in the case of
|
40
|
+
"Follow" and "Like" the following methods will be added:
|
36
41
|
|
37
42
|
# Follow
|
38
43
|
user.follow(other_user) # Creates a 'Follow' connection from user -> other_user
|
39
44
|
user.unfollow(other_user) # Removes 'Follow' connection between user -> other_user
|
40
45
|
user.toggle_follow(other_user) # Toggles the connection on/off (useful for toggle buttons)
|
41
46
|
user.follows?(other_user) # Returns true if the user if following other_user
|
42
|
-
user.
|
47
|
+
user.following(:user) # Returns a list of all the users the user is following
|
43
48
|
other_user.followers(:user) #Returns a list of all the user's followers
|
44
49
|
|
45
50
|
# Like
|
@@ -47,7 +52,7 @@ This will add following methods:
|
|
47
52
|
user.unlike(post)
|
48
53
|
user.toggle_like(post)
|
49
54
|
user.likes?(post)
|
50
|
-
user.
|
55
|
+
user.liking(:post)
|
51
56
|
post.likers(:user)
|
52
57
|
|
53
58
|
That's it! If you want to add extra functionality to the join model (such
|
@@ -60,7 +65,7 @@ project like this:
|
|
60
65
|
|
61
66
|
## Credits
|
62
67
|
|
63
|
-
This gem was inspired by [socialization](https://github.com/cmer/socialization) that although it didn't quite do what
|
68
|
+
This gem was inspired by [socialization](https://github.com/cmer/socialization) that although it didn't quite do what I needed is a pretty useful tool in itself.
|
64
69
|
|
65
70
|
|
66
71
|
## Copyright
|
@@ -11,7 +11,7 @@ module Connections
|
|
11
11
|
# user.followers(:user)
|
12
12
|
define_method :"#{t.to_s.sub(/e$/,'')}ers" do |class_name|
|
13
13
|
klass = class_name.to_s.classify.constantize
|
14
|
-
klass.joins(:connections).where("connections_connections.type = ? AND connectable_type = ? AND connectable_id = ?", t.to_s.classify, self.class.
|
14
|
+
klass.joins(:connections).where("connections_connections.type = ? AND connectable_type = ? AND connectable_id = ?", t.to_s.classify, self.class.base_class.to_s, self)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -44,7 +44,7 @@ module Connections
|
|
44
44
|
# user.following(:user)
|
45
45
|
define_method :"#{t.to_s.sub(/e$/,'')}ing" do |class_name|
|
46
46
|
klass = class_name.to_s.classify.constantize
|
47
|
-
klass.joins(:incoming_connections).where("connections_connections.type = ? AND connector_type = ? AND connector_id = ?", t.to_s.classify, self.class.
|
47
|
+
klass.joins(:incoming_connections).where("connections_connections.type = ? AND connector_type = ? AND connector_id = ?", t.to_s.classify, self.class.base_class.to_s, self)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
data/lib/connections/version.rb
CHANGED
data/test/connections_test.rb
CHANGED
@@ -8,78 +8,74 @@ class ConnectionsTest < ActiveSupport::TestCase
|
|
8
8
|
@post = Post.create!
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
test '#unfollow' do
|
19
|
-
@follower.follow(@user)
|
20
|
-
assert @follower.follows?(@user)
|
21
|
-
@follower.unfollow(@user)
|
22
|
-
assert !@follower.follows?(@user)
|
23
|
-
end
|
24
|
-
|
25
|
-
test '#toggle_follow' do
|
26
|
-
@follower.toggle_follow(@user)
|
27
|
-
assert @follower.follows?(@user)
|
28
|
-
@follower.toggle_follow(@user)
|
29
|
-
assert !@follower.follows?(@user)
|
30
|
-
end
|
11
|
+
# No explicit Follow class defined
|
12
|
+
test '#follow' do
|
13
|
+
assert !@follower.follows?(@user)
|
14
|
+
@follower.follow(@user)
|
15
|
+
assert @follower.follows?(@user)
|
16
|
+
end
|
31
17
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
18
|
+
test '#unfollow' do
|
19
|
+
@follower.follow(@user)
|
20
|
+
assert @follower.follows?(@user)
|
21
|
+
@follower.unfollow(@user)
|
22
|
+
assert !@follower.follows?(@user)
|
23
|
+
end
|
38
24
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
25
|
+
test '#toggle_follow' do
|
26
|
+
@follower.toggle_follow(@user)
|
27
|
+
assert @follower.follows?(@user)
|
28
|
+
@follower.toggle_follow(@user)
|
29
|
+
assert !@follower.follows?(@user)
|
30
|
+
end
|
43
31
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
32
|
+
test '#following' do
|
33
|
+
@follower.follow(@user)
|
34
|
+
@follower.follow(@post)
|
35
|
+
assert_equal [@user], @follower.following(:user)
|
36
|
+
assert_equal [@post], @follower.following(:post)
|
50
37
|
end
|
51
38
|
|
52
|
-
|
39
|
+
test '#follows' do
|
40
|
+
#TODO: What should happen if the Follow model isn't defined?
|
41
|
+
#assert !@user.respond_to?(:follows)
|
42
|
+
end
|
53
43
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
44
|
+
test '#followers' do
|
45
|
+
@follower.follow(@user)
|
46
|
+
@follower.follow(@post)
|
47
|
+
assert_equal [@follower], @user.followers(:user)
|
48
|
+
assert_equal [@follower], @post.followers(:user)
|
49
|
+
end
|
59
50
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
51
|
+
# Explicit Like class defined
|
52
|
+
test '#like' do
|
53
|
+
assert !@user.likes?(@post)
|
54
|
+
@user.like(@post)
|
55
|
+
assert @user.likes?(@post)
|
56
|
+
end
|
66
57
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
58
|
+
test '#unlike' do
|
59
|
+
@user.like(@post)
|
60
|
+
assert @user.likes?(@post)
|
61
|
+
@user.unlike(@post)
|
62
|
+
assert !@user.likes?(@post)
|
63
|
+
end
|
73
64
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
65
|
+
test '#toggle_like' do
|
66
|
+
@user.toggle_like(@post)
|
67
|
+
assert @user.likes?(@post)
|
68
|
+
@user.toggle_like(@post)
|
69
|
+
assert !@user.likes?(@post)
|
70
|
+
end
|
78
71
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
72
|
+
test '#liking' do
|
73
|
+
@user.like(@post)
|
74
|
+
assert_equal [@post], @user.liking(:post)
|
75
|
+
end
|
83
76
|
|
77
|
+
test '#likes' do
|
78
|
+
@user.like(@post)
|
79
|
+
assert_equal [@post], @user.likes.map(&:connectable)
|
84
80
|
end
|
85
81
|
end
|