popular 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +34 -0
- data/lib/popular/popular.rb +17 -0
- data/lib/popular/version.rb +1 -1
- data/spec/popular/popular_model_spec.rb +33 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZGExM2NmNGUxNmI3ZTk5MjgwMDE2OTJiN2E3NWY0Njk4NTgxMjZiZg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OWY2NTBmYzRlMmI3NDIzMTU5YmI4MzVmMGVhNTg3MzdmZGQ2NzkyYg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NjYxZjRhZGI3YTI0NmNiY2Q1NDZhNWVjYzQ2MDdiOTEyYjY2YzJmMTZkZTEz
|
10
|
+
MzgwY2ZmNWNiYTUwZjk4ZjBiZTkxNzFlZjU4ZmUwNjJkNWRhZDI4ZDZiODc3
|
11
|
+
YjJhMmU4M2U1YTZjN2VlMzQ5NjE2NzI3YzcwOTNhN2FjMzI0MjY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OWMyZTZmM2U0Yjc3MmMyMTU0MGJlNGM5YmZiODQxNzZjZDdmYzkzNmFkMGY3
|
14
|
+
NGEzYjljYjU1MTk3NDczNmJjZjU1YjZmYWNhOTQxZmJiMTIyNjFjNDQ1Yzlh
|
15
|
+
NTA4NTAyZjhlNDEyOWRmNzMzZTZiMWFlYmUyZDE0YjMxMTAyN2E=
|
data/README.md
CHANGED
@@ -8,6 +8,10 @@
|
|
8
8
|
|
9
9
|
Popular is a friendship gem designed for Rails/ActiveRecord models.
|
10
10
|
|
11
|
+
- RubyGems: ( https://rubygems.org/gems/popular )
|
12
|
+
- Website: ( http://thejchap.github.io/popular )
|
13
|
+
- RDoc: ( http://rubydoc.info/github/thejchap/popular/master/frames )
|
14
|
+
|
11
15
|
## Installation
|
12
16
|
|
13
17
|
Add this line to your application's Gemfile:
|
@@ -61,6 +65,20 @@ end
|
|
61
65
|
@sam.mutual_friends_with? @jackson #=> true
|
62
66
|
```
|
63
67
|
|
68
|
+
### Aliases
|
69
|
+
|
70
|
+
In Popular, `befriend` is synonomous with `follow`, so if it better fits the context of your application, you can use
|
71
|
+
follow methods/relations instead. For example:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
@sam.follow @jackson
|
75
|
+
@sam.following? @jackson #=> true
|
76
|
+
|
77
|
+
@jackson.follow @sam
|
78
|
+
@sam.followers.include? @jackson #=> true
|
79
|
+
```
|
80
|
+
|
81
|
+
|
64
82
|
### Callbacks
|
65
83
|
|
66
84
|
Popular provides callbacks that are fired around friendship creation. Available callbacks are:
|
@@ -92,6 +110,22 @@ end
|
|
92
110
|
@justin.unfriend @jenny #=> ":("
|
93
111
|
```
|
94
112
|
|
113
|
+
## Related gems
|
114
|
+
|
115
|
+
If Popular isn't quite what you're looking for, here are some other useful gems in the same category:
|
116
|
+
|
117
|
+
- Amistad ( https://github.com/raw1z/amistad )
|
118
|
+
- Friendable ( https://github.com/yuki24/friendable )
|
119
|
+
|
120
|
+
*Disclaimer: I have not used either of the above gems*
|
121
|
+
|
122
|
+
|
123
|
+
## Resources
|
124
|
+
|
125
|
+
Popular was heavily inspired by this screencast: ( http://railscasts.com/episodes/163-self-referential-association?view=asciicast )
|
126
|
+
|
127
|
+
|
128
|
+
|
95
129
|
## Contributing
|
96
130
|
|
97
131
|
1. Fork it ( http://github.com/thejchap/popular/fork )
|
data/lib/popular/popular.rb
CHANGED
@@ -5,6 +5,11 @@ module Popular
|
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
included do |base|
|
8
|
+
has_many :followings, class_name: 'Popular::Friendship', as: :popular_model, dependent: :destroy
|
9
|
+
has_many :followeds, through: :followings, source: :friend, source_type: base
|
10
|
+
has_many :inverse_followings, class_name: 'Popular::Friendship', as: :friend, foreign_key: :friend_id
|
11
|
+
has_many :followers, through: :inverse_followings, source: :popular_model, source_type: base
|
12
|
+
|
8
13
|
has_many :friendships, class_name: 'Popular::Friendship', as: :popular_model, dependent: :destroy
|
9
14
|
has_many :friends, through: :friendships, source_type: base
|
10
15
|
has_many :inverse_friendships, class_name: 'Popular::Friendship', as: :friend, foreign_key: :friend_id
|
@@ -16,6 +21,18 @@ module Popular
|
|
16
21
|
[:before_unfriend, :after_unfriend, :before_befriend, :after_befriend].each do |callback|
|
17
22
|
send callback
|
18
23
|
end
|
24
|
+
|
25
|
+
aliases = {
|
26
|
+
befriend: [:follow],
|
27
|
+
befriend!: [:follow!],
|
28
|
+
unfriend: [:unfollow],
|
29
|
+
friends_with?: [:following?],
|
30
|
+
friended_by?: [:followed_by?]
|
31
|
+
}
|
32
|
+
|
33
|
+
aliases.each do |method, links|
|
34
|
+
links.each { |linked_method| alias_method( linked_method, method ) }
|
35
|
+
end
|
19
36
|
end
|
20
37
|
|
21
38
|
# Adds a friend to an instance's friend's list
|
data/lib/popular/version.rb
CHANGED
@@ -1,10 +1,19 @@
|
|
1
1
|
shared_examples "a popular model" do
|
2
2
|
|
3
3
|
describe 'mutual friendships' do
|
4
|
-
|
5
|
-
|
4
|
+
describe '.inverse_friends' do
|
5
|
+
it 'includes popular_models who have added an instance as a friend' do
|
6
|
+
another_popular_model.befriend popular_model
|
7
|
+
|
8
|
+
expect( popular_model ).to be_friended_by another_popular_model
|
9
|
+
end
|
6
10
|
|
7
|
-
|
11
|
+
it 'can be called with followers' do
|
12
|
+
another_popular_model.follow popular_model
|
13
|
+
|
14
|
+
expect( popular_model ).to be_followed_by another_popular_model
|
15
|
+
expect( popular_model.followers ).to match_array [another_popular_model]
|
16
|
+
end
|
8
17
|
end
|
9
18
|
|
10
19
|
it '#mutual_friends_with? returns false if instances are not mutual friends' do
|
@@ -20,6 +29,12 @@ shared_examples "a popular model" do
|
|
20
29
|
end
|
21
30
|
|
22
31
|
describe '.befriend!' do
|
32
|
+
it 'can be called with .follow!' do
|
33
|
+
popular_model.follow! another_popular_model
|
34
|
+
|
35
|
+
expect( popular_model).to be_following another_popular_model
|
36
|
+
end
|
37
|
+
|
23
38
|
it 'creates a one way friendship' do
|
24
39
|
popular_model.befriend! another_popular_model
|
25
40
|
|
@@ -42,6 +57,12 @@ shared_examples "a popular model" do
|
|
42
57
|
end
|
43
58
|
|
44
59
|
describe '.befriend' do
|
60
|
+
it 'can be called with .follow' do
|
61
|
+
popular_model.follow another_popular_model
|
62
|
+
|
63
|
+
expect( popular_model).to be_following another_popular_model
|
64
|
+
end
|
65
|
+
|
45
66
|
|
46
67
|
context 'successful' do
|
47
68
|
it 'triggers before_befriend callback' do
|
@@ -66,7 +87,7 @@ shared_examples "a popular model" do
|
|
66
87
|
end
|
67
88
|
|
68
89
|
describe '#friends_with?' do
|
69
|
-
it 'returns
|
90
|
+
it 'returns false if a popular_model is not friends with a given popular_model' do
|
70
91
|
expect( popular_model ).to_not be_friends_with another_popular_model
|
71
92
|
end
|
72
93
|
|
@@ -78,6 +99,14 @@ shared_examples "a popular model" do
|
|
78
99
|
end
|
79
100
|
|
80
101
|
describe '.unfriend' do
|
102
|
+
it 'can be called with .unfollow' do
|
103
|
+
[:follow, :unfollow].each do |method|
|
104
|
+
popular_model.send method, another_popular_model
|
105
|
+
end
|
106
|
+
|
107
|
+
expect( popular_model).to_not be_following another_popular_model
|
108
|
+
end
|
109
|
+
|
81
110
|
it 'destroys a friendship' do
|
82
111
|
[:befriend, :unfriend].each do |method|
|
83
112
|
popular_model.send method, another_popular_model
|