mongoid_follow 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +39 -21
- data/lib/mongoid_follow/followee.rb +34 -5
- data/lib/mongoid_follow/follower.rb +50 -8
- data/lib/mongoid_follow/version.rb +1 -1
- data/mongoid_follow.gemspec +2 -2
- data/spec/models/group.rb +2 -0
- data/spec/models/user.rb +2 -0
- data/spec/specs/follow_spec.rb +83 -10
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -1,57 +1,75 @@
|
|
1
|
-
= Follow
|
1
|
+
= Follow feature for Rails3 with Mongoid
|
2
2
|
|
3
3
|
== Installation
|
4
4
|
|
5
5
|
In Gemfile:
|
6
|
-
gem 'mongoid_follow
|
6
|
+
gem 'mongoid_follow'
|
7
|
+
|
8
|
+
== Legend (to ease complications)
|
9
|
+
|
10
|
+
"follower" is a model who follows
|
11
|
+
"followee" is a model who would be followed
|
7
12
|
|
8
13
|
== To use
|
9
14
|
|
10
|
-
To make mongoid_follow usable you need to include Mongoid::Followee into your document
|
11
|
-
Meanwhile, you also need to include Mongoid::Follower in your follower model:
|
15
|
+
To make mongoid_follow usable you need to include Mongoid::Followee into your document who would be followed then you also need to include Mongoid::Follower in your follower model:
|
12
16
|
|
13
17
|
class User
|
14
18
|
include Mongoid::Document
|
19
|
+
|
15
20
|
include Mongoid::Followee
|
16
21
|
include Mongoid::Follower
|
17
22
|
end
|
18
23
|
|
19
24
|
class Group
|
20
25
|
include Mongoid::Document
|
26
|
+
|
21
27
|
include Mongoid::Followee
|
22
28
|
end
|
23
29
|
|
24
30
|
You can then follow a model using:
|
25
31
|
|
26
|
-
@bonnie = User.
|
27
|
-
@
|
32
|
+
@bonnie = User.create
|
33
|
+
@clyde = User.create
|
34
|
+
|
35
|
+
@bonnie.follow(@clyde)
|
36
|
+
@bonnie.unfollow(@clyde)
|
37
|
+
|
38
|
+
You can also see whether a model is a follower of another model or if a model is a followee of another model:
|
39
|
+
|
40
|
+
@clyde.follower?(@bonnie)
|
41
|
+
@bonnie.followee?(@clyde)
|
42
|
+
|
43
|
+
You can also be a follower of other models
|
28
44
|
|
29
|
-
|
30
|
-
|
45
|
+
@gang = Group.create
|
46
|
+
@bonnie.follow(@group)
|
31
47
|
|
32
|
-
|
48
|
+
@gang.follower?(@bonnie)
|
49
|
+
@bonnie.follows?(@gang)
|
33
50
|
|
34
|
-
|
35
|
-
current_user.followee?(@clyde)
|
51
|
+
* Any bug or issue, please send me an email to aeguintu@gmail.com
|
36
52
|
|
37
|
-
|
53
|
+
== For development
|
38
54
|
|
39
|
-
|
40
|
-
|
55
|
+
gem install 'mongoid'
|
56
|
+
gem install 'bson_ext'
|
57
|
+
gem install 'database_cleaner'
|
58
|
+
gem install 'rspec'
|
41
59
|
|
42
|
-
|
60
|
+
rake spec/specs/follow_spec_rb
|
43
61
|
|
44
62
|
== TODO
|
45
63
|
|
46
|
-
* finish up todo's
|
47
|
-
* count of followers/followees
|
48
|
-
* common followers (or maybe followees)
|
49
|
-
* maybe... (un)authorization of who can follow (model)
|
50
|
-
* most/least followed/following #FINISHED
|
64
|
+
* finish up todo's (list of followers and followees) ==FINISHED
|
65
|
+
* count of followers/followees ==FINISHED
|
66
|
+
* common followers (or maybe followees) ==FINISHED
|
51
67
|
|
52
68
|
== Thanks
|
53
69
|
|
54
|
-
|
70
|
+
Super thanks:
|
71
|
+
to mongoid_followable.
|
72
|
+
to Tristan Peralta.
|
55
73
|
|
56
74
|
== Copyright
|
57
75
|
|
@@ -3,10 +3,11 @@ module Mongoid
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do |base|
|
6
|
+
base.field :fferc, :type => Integer, :default => 0
|
6
7
|
base.has_many :followers, :class_name => 'Follow', :as => :follower, :dependent => :destroy
|
7
8
|
end
|
8
9
|
|
9
|
-
#
|
10
|
+
# know if self is followed by model
|
10
11
|
#
|
11
12
|
# Example:
|
12
13
|
# >> @clyde.follower?(@bonnie)
|
@@ -15,13 +16,41 @@ module Mongoid
|
|
15
16
|
0 < self.followers.find(:all, conditions: {ff_id: model.id}).limit(1).count
|
16
17
|
end
|
17
18
|
|
19
|
+
# get followees count
|
20
|
+
#
|
21
|
+
# Example:
|
22
|
+
# >> @bonnie.followees_count
|
23
|
+
# => 1
|
24
|
+
def followers_count
|
25
|
+
self.fferc
|
26
|
+
end
|
27
|
+
|
18
28
|
# view all selfs followers
|
19
29
|
#
|
20
30
|
# Example:
|
21
|
-
# >> @clyde.
|
22
|
-
# =>
|
23
|
-
def
|
24
|
-
|
31
|
+
# >> @clyde.all_followers
|
32
|
+
# => [@bonnie, @alec]
|
33
|
+
def all_followers
|
34
|
+
get_followers_of(self)
|
35
|
+
end
|
36
|
+
|
37
|
+
# view all common followers of self against model
|
38
|
+
#
|
39
|
+
# Example:
|
40
|
+
# >> @clyde.common_followers_with(@gang)
|
41
|
+
# => [@bonnie, @alec]
|
42
|
+
def common_followers_with(model)
|
43
|
+
model_followers = get_followers_of(model)
|
44
|
+
self_followers = get_followers_of(self)
|
45
|
+
|
46
|
+
self_followers & model_followers
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def get_followers_of(me)
|
51
|
+
me.followers.collect do |f|
|
52
|
+
f.ff_type.constantize.find(f.ff_id)
|
53
|
+
end
|
25
54
|
end
|
26
55
|
end
|
27
56
|
end
|
@@ -3,6 +3,7 @@ module Mongoid
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do |base|
|
6
|
+
base.field :ffeec, :type => Integer, :default => 0
|
6
7
|
base.has_many :followees, :class_name => 'Follow', :as => :followee, :dependent => :destroy
|
7
8
|
end
|
8
9
|
|
@@ -11,9 +12,15 @@ module Mongoid
|
|
11
12
|
# Example:
|
12
13
|
# >> @bonnie.follow(@clyde)
|
13
14
|
def follow(model)
|
14
|
-
|
15
|
+
if self.id != model.id && !self.follows?(model)
|
16
|
+
model.followers.create!(:ff_type => self.class.name, :ff_id => self.id)
|
17
|
+
model.inc(:fferc, 1)
|
15
18
|
|
16
|
-
|
19
|
+
self.followees.create!(:ff_type => model.class.name, :ff_id => model.id)
|
20
|
+
self.inc(:ffeec, 1)
|
21
|
+
else
|
22
|
+
return false
|
23
|
+
end
|
17
24
|
end
|
18
25
|
|
19
26
|
# unfollow a model
|
@@ -21,12 +28,18 @@ module Mongoid
|
|
21
28
|
# Example:
|
22
29
|
# >> @bonnie.unfollow(@clyde)
|
23
30
|
def unfollow(model)
|
24
|
-
|
31
|
+
if self.id != model.id && self.follows?(model)
|
32
|
+
model.followers.where(:ff_type => self.class.name, :ff_id => self.id).destroy
|
33
|
+
model.inc(:fferc, -1)
|
25
34
|
|
26
|
-
|
35
|
+
self.followees.where(:ff_type => model.class.name, :ff_id => model.id).destroy
|
36
|
+
self.inc(:ffeec, -1)
|
37
|
+
else
|
38
|
+
return false
|
39
|
+
end
|
27
40
|
end
|
28
41
|
|
29
|
-
#
|
42
|
+
# know if self is already following model
|
30
43
|
#
|
31
44
|
# Example:
|
32
45
|
# >> @bonnie.follows?(@clyde)
|
@@ -35,12 +48,41 @@ module Mongoid
|
|
35
48
|
0 < self.followees.find(:all, conditions: {ff_id: model.id}).limit(1).count
|
36
49
|
end
|
37
50
|
|
51
|
+
# get followees count
|
52
|
+
#
|
53
|
+
# Example:
|
54
|
+
# >> @bonnie.followees_count
|
55
|
+
# => 1
|
56
|
+
def followees_count
|
57
|
+
self.ffeec
|
58
|
+
end
|
59
|
+
|
38
60
|
# view all selfs followees
|
39
61
|
#
|
40
62
|
# Example:
|
41
|
-
# >> @alec.
|
42
|
-
|
43
|
-
|
63
|
+
# >> @alec.all_followees
|
64
|
+
# => [@bonnie]
|
65
|
+
def all_followees
|
66
|
+
get_followees_of(self)
|
67
|
+
end
|
68
|
+
|
69
|
+
# view all common followees of self against model
|
70
|
+
#
|
71
|
+
# Example:
|
72
|
+
# >> @clyde.common_followees_with(@gang)
|
73
|
+
# => [@bonnie, @alec]
|
74
|
+
def common_followees_with(model)
|
75
|
+
model_followees = get_followees_of(model)
|
76
|
+
self_followees = get_followees_of(self)
|
77
|
+
|
78
|
+
self_followees & model_followees
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
def get_followees_of(me)
|
83
|
+
me.followees.collect do |f|
|
84
|
+
f.ff_type.constantize.find(f.ff_id)
|
85
|
+
end
|
44
86
|
end
|
45
87
|
end
|
46
88
|
end
|
data/mongoid_follow.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.authors = ["Alec Guintu"]
|
10
10
|
s.email = ["animerei12@gmail.com"]
|
11
11
|
s.homepage = "https://github.com/alecguintu/mongoid_follow"
|
12
|
-
s.summary = %q{ Add basic "follow" features to rails3
|
13
|
-
s.description = %q{
|
12
|
+
s.summary = %q{ Add basic "follow" features to rails3 + mongoid }
|
13
|
+
s.description = %q{ Gem to add basic "follow" features if you're using rails3 with mongoid }
|
14
14
|
|
15
15
|
s.rubyforge_project = "mongoid_follow"
|
16
16
|
|
data/spec/models/group.rb
CHANGED
data/spec/models/user.rb
CHANGED
data/spec/specs/follow_spec.rb
CHANGED
@@ -5,17 +5,11 @@ describe Mongoid::Follower do
|
|
5
5
|
describe User do
|
6
6
|
|
7
7
|
before do
|
8
|
-
@bonnie = User.
|
9
|
-
@
|
8
|
+
@bonnie = User.create(:name => 'Bonnie')
|
9
|
+
@clyde = User.create(:name => 'Clyde')
|
10
|
+
@alec = User.create(:name => 'Alec')
|
10
11
|
|
11
|
-
@
|
12
|
-
@clyde.save
|
13
|
-
|
14
|
-
@alec = User.new
|
15
|
-
@alec.save
|
16
|
-
|
17
|
-
@gang = Group.new
|
18
|
-
@gang.save
|
12
|
+
@gang = Group.create(:name => 'Gang')
|
19
13
|
end
|
20
14
|
|
21
15
|
it "should have no follows or followers" do
|
@@ -33,6 +27,16 @@ describe Mongoid::Follower do
|
|
33
27
|
@clyde.follower?(@bonnie).should be_true
|
34
28
|
end
|
35
29
|
|
30
|
+
it "should decline to follow self" do
|
31
|
+
@bonnie.follow(@bonnie).should be_false
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should decline two follows" do
|
35
|
+
@bonnie.follow(@clyde)
|
36
|
+
|
37
|
+
@bonnie.follow(@clyde).should be_false
|
38
|
+
end
|
39
|
+
|
36
40
|
it "can unfollow another User" do
|
37
41
|
@bonnie.follows?(@clyde).should be_false
|
38
42
|
@clyde.follower?(@bonnie).should be_false
|
@@ -46,11 +50,80 @@ describe Mongoid::Follower do
|
|
46
50
|
@clyde.follower?(@bonnie).should be_false
|
47
51
|
end
|
48
52
|
|
53
|
+
it "should decline unfollow of non-followed User" do
|
54
|
+
@bonnie.unfollow(@clyde).should be_false
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should decline unfollow of self" do
|
58
|
+
@bonnie.unfollow(@bonnie).should be_false
|
59
|
+
end
|
60
|
+
|
49
61
|
it "can follow a group" do
|
50
62
|
@bonnie.follow(@gang)
|
51
63
|
|
52
64
|
@bonnie.follows?(@gang).should be_true
|
53
65
|
@gang.follower?(@bonnie).should be_true
|
54
66
|
end
|
67
|
+
|
68
|
+
it "should increment / decrement counters" do
|
69
|
+
@clyde.followers_count.should == 0
|
70
|
+
|
71
|
+
@bonnie.follow(@clyde)
|
72
|
+
|
73
|
+
@bonnie.followees_count.should == 1
|
74
|
+
@clyde.followers_count.should == 1
|
75
|
+
|
76
|
+
@alec.follow(@clyde)
|
77
|
+
@clyde.followers_count.should == 2
|
78
|
+
@bonnie.followers_count.should == 0
|
79
|
+
|
80
|
+
@alec.unfollow(@clyde)
|
81
|
+
@alec.followees_count.should == 0
|
82
|
+
@clyde.followers_count.should == 1
|
83
|
+
|
84
|
+
@bonnie.unfollow(@clyde)
|
85
|
+
@bonnie.followees_count.should == 0
|
86
|
+
@clyde.followers_count.should == 0
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should list all followers" do
|
90
|
+
@bonnie.follow(@clyde)
|
91
|
+
# @clyde.all_followers.should == [@bonnie] # spec has an error on last #all_followers when this is called
|
92
|
+
|
93
|
+
@alec.follow(@clyde)
|
94
|
+
@clyde.all_followers.should == [@bonnie, @alec]
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should list all followee" do
|
98
|
+
@bonnie.follow(@clyde)
|
99
|
+
# @bonnie.all_followees.should == [@clyde] # spec has an error on last #all_followees when this is called
|
100
|
+
|
101
|
+
@bonnie.follow(@gang)
|
102
|
+
@bonnie.all_followees.should == [@clyde, @gang]
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should have common followers" do
|
106
|
+
@bonnie.follow(@clyde)
|
107
|
+
@bonnie.follow(@gang)
|
108
|
+
|
109
|
+
@gang.common_followers_with(@clyde).should == [@bonnie]
|
110
|
+
|
111
|
+
@alec.follow(@clyde)
|
112
|
+
@alec.follow(@gang)
|
113
|
+
|
114
|
+
@clyde.common_followers_with(@gang).should == [@bonnie, @alec]
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should have common followees" do
|
118
|
+
@bonnie.follow(@gang)
|
119
|
+
@alec.follow(@gang)
|
120
|
+
|
121
|
+
@alec.common_followees_with(@bonnie).should == [@gang]
|
122
|
+
|
123
|
+
@bonnie.follow(@clyde)
|
124
|
+
@alec.follow(@clyde)
|
125
|
+
|
126
|
+
@bonnie.common_followees_with(@alec).should == [@gang, @clyde]
|
127
|
+
end
|
55
128
|
end
|
56
129
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: mongoid_follow
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Alec Guintu
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-11-03 00:00:00 +08:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -57,7 +57,7 @@ dependencies:
|
|
57
57
|
version: "0"
|
58
58
|
type: :development
|
59
59
|
version_requirements: *id004
|
60
|
-
description: "
|
60
|
+
description: " Gem to add basic \"follow\" features if you're using rails3 with mongoid "
|
61
61
|
email:
|
62
62
|
- animerei12@gmail.com
|
63
63
|
executables: []
|
@@ -109,7 +109,7 @@ rubyforge_project: mongoid_follow
|
|
109
109
|
rubygems_version: 1.6.2
|
110
110
|
signing_key:
|
111
111
|
specification_version: 3
|
112
|
-
summary: Add basic "follow" features to rails3
|
112
|
+
summary: Add basic "follow" features to rails3 + mongoid
|
113
113
|
test_files:
|
114
114
|
- spec/models/group.rb
|
115
115
|
- spec/models/user.rb
|