mongoid_followable 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.idea/misc.xml CHANGED
@@ -3,6 +3,6 @@
3
3
  <component name="DependencyValidationManager">
4
4
  <option name="SKIP_IMPORT_STATEMENTS" value="false" />
5
5
  </component>
6
- <component name="ProjectRootManager" version="2" project-jdk-name="ruby-1.9.2-p0" project-jdk-type="RUBY_SDK" />
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="RVM: ruby-1.9.2-p136" project-jdk-type="RUBY_SDK" />
7
7
  </project>
8
8
 
data/.idea/workspace.xml CHANGED
@@ -2,17 +2,13 @@
2
2
  <project version="4">
3
3
  <component name="ChangeListManager">
4
4
  <list default="true" id="90bdec60-890b-4083-b05d-2f665a1f4d70" name="Default" comment="">
5
- <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/app/models/follow.rb" />
6
- <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/lib/mongoid_followable/followable.rb" />
7
- <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/spec/models/group.rb" />
8
- <change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/mongoid_followable.iml" afterPath="$PROJECT_DIR$/.idea/mongoid_followable.iml" />
5
+ <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/lib/mongoid_followable/follower.rb" />
9
6
  <change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/misc.xml" afterPath="$PROJECT_DIR$/.idea/misc.xml" />
10
7
  <change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
11
8
  <change type="MODIFICATION" beforePath="$PROJECT_DIR$/lib/mongoid_followable.rb" afterPath="$PROJECT_DIR$/lib/mongoid_followable.rb" />
9
+ <change type="MODIFICATION" beforePath="$PROJECT_DIR$/lib/mongoid_followable/followable.rb" afterPath="$PROJECT_DIR$/lib/mongoid_followable/followable.rb" />
12
10
  <change type="MODIFICATION" beforePath="$PROJECT_DIR$/lib/mongoid_followable/version.rb" afterPath="$PROJECT_DIR$/lib/mongoid_followable/version.rb" />
13
- <change type="MODIFICATION" beforePath="$PROJECT_DIR$/mongoid_followable.gemspec" afterPath="$PROJECT_DIR$/mongoid_followable.gemspec" />
14
- <change type="MODIFICATION" beforePath="$PROJECT_DIR$/spec/mongoid/followable_spec.rb" afterPath="$PROJECT_DIR$/spec/mongoid/followable_spec.rb" />
15
- <change type="MODIFICATION" beforePath="$PROJECT_DIR$/spec/spec_helper.rb" afterPath="$PROJECT_DIR$/spec/spec_helper.rb" />
11
+ <change type="MODIFICATION" beforePath="$PROJECT_DIR$/spec/models/user.rb" afterPath="$PROJECT_DIR$/spec/models/user.rb" />
16
12
  </list>
17
13
  <ignored path="mongoid_followable.iws" />
18
14
  <ignored path=".idea/workspace.xml" />
@@ -35,7 +31,17 @@
35
31
  <favorites_list name="mongoid_followable" />
36
32
  </component>
37
33
  <component name="FileEditorManager">
38
- <leaf />
34
+ <leaf>
35
+ <file leaf-file-name="mongoid_followable.rb" pinned="false" current="true" current-in-tab="true">
36
+ <entry file="file://$PROJECT_DIR$/lib/mongoid_followable.rb">
37
+ <provider selected="true" editor-type-id="text-editor">
38
+ <state line="1" column="70" selection-start="145" selection-end="145" vertical-scroll-proportion="0.02173913">
39
+ <folding />
40
+ </state>
41
+ </provider>
42
+ </entry>
43
+ </file>
44
+ </leaf>
39
45
  </component>
40
46
  <component name="FindManager">
41
47
  <FindUsagesManager>
@@ -49,19 +55,20 @@
49
55
  <component name="IdeDocumentHistory">
50
56
  <option name="changedFiles">
51
57
  <list>
52
- <option value="$PROJECT_DIR$/spec/models/user.rb" />
53
58
  <option value="$PROJECT_DIR$/app/models/followee.rb" />
54
59
  <option value="$PROJECT_DIR$/app/models/follower.rb" />
55
60
  <option value="$PROJECT_DIR$/spec/models/group.rb" />
56
- <option value="$PROJECT_DIR$/lib/mongoid_followable/version.rb" />
57
61
  <option value="$PROJECT_DIR$/lib/mongoid_followable/follow.rb" />
58
62
  <option value="$PROJECT_DIR$/lib/generators/mongoid_followable/install_generator.rb" />
59
63
  <option value="$PROJECT_DIR$/spec/spec_helper.rb" />
60
- <option value="$PROJECT_DIR$/lib/mongoid_followable.rb" />
61
64
  <option value="$PROJECT_DIR$/app/models/follow.rb" />
62
65
  <option value="$PROJECT_DIR$/spec/mongoid/followable_spec.rb" />
63
- <option value="$PROJECT_DIR$/lib/mongoid_followable/followable.rb" />
64
66
  <option value="$PROJECT_DIR$/mongoid_followable.gemspec" />
67
+ <option value="$PROJECT_DIR$/lib/mongoid_followable/follower.rb" />
68
+ <option value="$PROJECT_DIR$/lib/mongoid_followable/followable.rb" />
69
+ <option value="$PROJECT_DIR$/lib/mongoid_followable.rb" />
70
+ <option value="$PROJECT_DIR$/spec/models/user.rb" />
71
+ <option value="$PROJECT_DIR$/lib/mongoid_followable/version.rb" />
65
72
  </list>
66
73
  </option>
67
74
  </component>
@@ -91,7 +98,6 @@
91
98
  <sortByType />
92
99
  </navigator>
93
100
  <panes>
94
- <pane id="Favorites" />
95
101
  <pane id="ProjectPane">
96
102
  <subPane>
97
103
  <PATH>
@@ -124,24 +130,6 @@
124
130
  <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
125
131
  </PATH_ELEMENT>
126
132
  </PATH>
127
- <PATH>
128
- <PATH_ELEMENT>
129
- <option name="myItemId" value="mongoid_followable" />
130
- <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
131
- </PATH_ELEMENT>
132
- <PATH_ELEMENT>
133
- <option name="myItemId" value="mongoid_followable" />
134
- <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
135
- </PATH_ELEMENT>
136
- <PATH_ELEMENT>
137
- <option name="myItemId" value="spec" />
138
- <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
139
- </PATH_ELEMENT>
140
- <PATH_ELEMENT>
141
- <option name="myItemId" value="mongoid" />
142
- <option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
143
- </PATH_ELEMENT>
144
- </PATH>
145
133
  <PATH>
146
134
  <PATH_ELEMENT>
147
135
  <option name="myItemId" value="mongoid_followable" />
@@ -212,6 +200,7 @@
212
200
  </PATH>
213
201
  </subPane>
214
202
  </pane>
203
+ <pane id="Favorites" />
215
204
  <pane id="Scope" />
216
205
  </panes>
217
206
  </component>
@@ -267,14 +256,14 @@
267
256
  <servers />
268
257
  </component>
269
258
  <component name="ToolWindowManager">
270
- <frame x="0" y="24" width="1280" height="752" extended-state="1" />
271
- <editor active="false" />
259
+ <frame x="1" y="24" width="1598" height="851" extended-state="1" />
260
+ <editor active="true" />
272
261
  <layout>
273
262
  <window_info id="Changes" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
274
263
  <window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
275
264
  <window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="1" side_tool="true" content_ui="tabs" />
276
265
  <window_info id="Dependency Viewer" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
277
- <window_info id="Project" active="true" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.1770335" sideWeight="0.67128986" order="0" side_tool="false" content_ui="tabs" />
266
+ <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.17643312" sideWeight="0.67128986" order="0" side_tool="false" content_ui="tabs" />
278
267
  <window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
279
268
  <window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
280
269
  <window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
@@ -335,63 +324,66 @@
335
324
  <state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0" />
336
325
  </provider>
337
326
  </entry>
338
- <entry file="file://$PROJECT_DIR$/spec/models/group.rb">
327
+ <entry file="file://$PROJECT_DIR$/spec/spec_helper.rb">
339
328
  <provider selected="true" editor-type-id="text-editor">
340
- <state line="2" column="29" selection-start="69" selection-end="69" vertical-scroll-proportion="0.0">
341
- <folding />
342
- </state>
329
+ <state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0" />
343
330
  </provider>
344
331
  </entry>
345
- <entry file="file://$PROJECT_DIR$/spec/models/user.rb">
332
+ <entry file="file://$PROJECT_DIR$/spec/mongoid/followable_spec.rb">
346
333
  <provider selected="true" editor-type-id="text-editor">
347
- <state line="3" column="9" selection-start="72" selection-end="72" vertical-scroll-proportion="0.0">
348
- <folding />
349
- </state>
334
+ <state line="71" column="31" selection-start="1675" selection-end="1675" vertical-scroll-proportion="1.7959528" />
350
335
  </provider>
351
336
  </entry>
352
- <entry file="file://$PROJECT_DIR$/lib/mongoid_followable.rb">
337
+ <entry file="file://$PROJECT_DIR$/mongoid_followable.gemspec">
338
+ <provider selected="true" editor-type-id="text-editor">
339
+ <state line="11" column="66" selection-start="437" selection-end="437" vertical-scroll-proportion="0.2782462" />
340
+ </provider>
341
+ </entry>
342
+ <entry file="file://$PROJECT_DIR$/app/models/follow.rb">
353
343
  <provider selected="true" editor-type-id="text-editor">
354
- <state line="1" column="21" selection-start="96" selection-end="96" vertical-scroll-proportion="0.02529511">
344
+ <state line="10" column="44" selection-start="315" selection-end="315" vertical-scroll-proportion="0.2173913">
355
345
  <folding />
356
346
  </state>
357
347
  </provider>
358
348
  </entry>
359
- <entry file="file://$PROJECT_DIR$/spec/spec_helper.rb">
349
+ <entry file="file://$PROJECT_DIR$/lib/mongoid_followable/followable.rb">
360
350
  <provider selected="true" editor-type-id="text-editor">
361
- <state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0">
351
+ <state line="15" column="27" selection-start="385" selection-end="385" vertical-scroll-proportion="0.0">
362
352
  <folding />
363
353
  </state>
364
354
  </provider>
365
355
  </entry>
366
- <entry file="file://$PROJECT_DIR$/spec/mongoid/followable_spec.rb">
356
+ <entry file="file://$PROJECT_DIR$/lib/mongoid_followable/follower.rb">
367
357
  <provider selected="true" editor-type-id="text-editor">
368
- <state line="71" column="31" selection-start="1675" selection-end="1675" vertical-scroll-proportion="1.7959528">
358
+ <state line="12" column="44" selection-start="299" selection-end="299" vertical-scroll-proportion="0.26086956">
369
359
  <folding />
370
360
  </state>
371
361
  </provider>
372
362
  </entry>
373
- <entry file="file://$PROJECT_DIR$/app/models/follow.rb">
363
+ <entry file="file://$PROJECT_DIR$/lib/mongoid_followable/version.rb">
374
364
  <provider selected="true" editor-type-id="text-editor">
375
- <state line="10" column="44" selection-start="315" selection-end="315" vertical-scroll-proportion="0.2529511">
365
+ <state line="2" column="20" selection-start="55" selection-end="55" vertical-scroll-proportion="0.04347826">
376
366
  <folding />
377
367
  </state>
378
368
  </provider>
379
369
  </entry>
380
- <entry file="file://$PROJECT_DIR$/lib/mongoid_followable/version.rb">
370
+ <entry file="file://$PROJECT_DIR$/spec/models/user.rb">
381
371
  <provider selected="true" editor-type-id="text-editor">
382
- <state line="2" column="20" selection-start="55" selection-end="55" vertical-scroll-proportion="0.05059022" />
372
+ <state line="3" column="27" selection-start="96" selection-end="96" vertical-scroll-proportion="0.06521739">
373
+ <folding />
374
+ </state>
383
375
  </provider>
384
376
  </entry>
385
- <entry file="file://$PROJECT_DIR$/mongoid_followable.gemspec">
377
+ <entry file="file://$PROJECT_DIR$/spec/models/group.rb">
386
378
  <provider selected="true" editor-type-id="text-editor">
387
- <state line="11" column="66" selection-start="437" selection-end="437" vertical-scroll-proportion="0.2782462">
379
+ <state line="2" column="29" selection-start="69" selection-end="69" vertical-scroll-proportion="0.04347826">
388
380
  <folding />
389
381
  </state>
390
382
  </provider>
391
383
  </entry>
392
- <entry file="file://$PROJECT_DIR$/lib/mongoid_followable/followable.rb">
384
+ <entry file="file://$PROJECT_DIR$/lib/mongoid_followable.rb">
393
385
  <provider selected="true" editor-type-id="text-editor">
394
- <state line="158" column="46" selection-start="3816" selection-end="3816" vertical-scroll-proportion="3.9966273">
386
+ <state line="1" column="70" selection-start="145" selection-end="145" vertical-scroll-proportion="0.02173913">
395
387
  <folding />
396
388
  </state>
397
389
  </provider>
data/README.rdoc CHANGED
@@ -5,7 +5,7 @@
5
5
  In console:
6
6
  gem install mongoid_followable
7
7
  or in Gemfile:
8
- gem 'mongoid_followable'
8
+ gem 'mongoid_followable', ">= 0.1.0"
9
9
 
10
10
  == Usage
11
11
 
@@ -14,33 +14,56 @@ To make model followable you need to include Mongoid::Followable into your docum
14
14
  include Mongoid::Document
15
15
  include Mongoid::Followable
16
16
  end
17
+
18
+ class Group
19
+ include Mongoid::Document
20
+ include Mongoid::Followable
21
+ end
22
+
17
23
  Now you can follow others in your controller:
18
24
  ...
19
- @user = some_other_user
20
- current_user.follow(@user)
25
+ @group = Group.new
26
+ @group.save
27
+
28
+ current_user.follow(@group)
21
29
 
22
- current_user.unfollow(@user)
30
+ current_user.unfollow(@group)
23
31
 
24
- current_user.follower_of?(@user)
25
- current_user.followee_of?(@user)
32
+ current_user.follower_of?(@group)
33
+ current_user.followee_of?(@group)
26
34
 
27
- current_user.follower_count
28
- current_user.followee_count
35
+ current_user.followers_count
36
+ current_user.followees_count
29
37
 
30
- User.followers_of(current_user)
31
- User.followees_of(current_user)
38
+ User.followers_of(@group)
39
+ User.followees_of(@group)
40
+
41
+ @group.all_followers
42
+ @user.all_followees
43
+
44
+ @group.followers_by_type("user")
45
+ @user.followees_by_type("group")
46
+
47
+ @group.followers_by_type("user")
48
+ @group.followers_count_by_type("user")
49
+ @user.followees_by_type("group")
50
+ @user.followees_count_by_type("group")
32
51
  ...
33
52
 
34
- * In current version, only following the same model is allowed
53
+ * Any bug or issue, please send me an email: ustc.flyingfox@gmail.com
35
54
 
36
55
  == TODO
37
56
 
38
- * inter-models followable
57
+ * inter-models followable #FINISHED#
39
58
  * divide into two parts: followable(being followed) and follower(following others)
40
59
  * following history/followed history
41
60
  * most/least followed/following
42
61
  * follow part of user like only names etc(is this necessary?)
43
62
 
63
+ == Thanks
64
+
65
+ Thanks the author(s) of acts_as_followable, you can find this gem here[https://github.com/xpepermint/acts_as_followable]
66
+
44
67
  == Copyright
45
68
 
46
69
  Copyright (c) Jie Fan. See LICENSE.txt for further details.
@@ -4,12 +4,11 @@ module Mongoid
4
4
 
5
5
  included do |base|
6
6
  base.has_many :followers, :class_name => "Follow", :as => :followable, :dependent => :destroy
7
- base.has_many :followees, :class_name => "Follow", :as => :following, :dependent => :destroy
8
7
  end
9
8
 
10
9
  module ClassMethods
11
10
 
12
- # get certain model's followers of this type
11
+ # get certain model's followees of this type
13
12
  #
14
13
  # Example:
15
14
  # >> @jim = User.new
@@ -18,39 +17,18 @@ module Mongoid
18
17
  # >> @ruby.save
19
18
  #
20
19
  # >> @jim.follow(@ruby)
21
- # >> User.followers_of(@ruby)
22
- # => [@jim]
20
+ # >> User.followees_of(@jim)
21
+ # => [@ruby]
23
22
  #
24
23
  # Arguments:
25
24
  # model: instance of some followable model
26
25
 
27
- def followers_of(model)
28
- model.followers_by_type(self.name)
29
- end
30
-
31
- # get certain model's followees of this type
32
- #
33
- # Example:
34
- # >> @jim.follow(@ruby)
35
- # >> Group.followees_of(@jim)
36
- # => [@ruby]
37
-
38
26
  def followees_of(model)
39
27
  model.followees_by_type(self.name)
40
28
  end
41
29
 
42
30
  end
43
31
 
44
- # see if this model is follower of some model
45
- #
46
- # Example:
47
- # >> @jim.follower_of?(@ruby)
48
- # => true
49
-
50
- def follower_of?(model)
51
- 0 < self.followees.by_model(model).limit(1).count * model.followers.by_model(self).limit(1).count
52
- end
53
-
54
32
  # see if this model is followee of some model
55
33
  #
56
34
  # Example:
@@ -71,16 +49,6 @@ module Mongoid
71
49
  rebuild_instances(self.followers)
72
50
  end
73
51
 
74
- # get all the followees of this model, same with classmethod followees_of
75
- #
76
- # Example:
77
- # >> @jim.all_followees
78
- # => [@ruby]
79
-
80
- def all_followees
81
- rebuild_instances(self.followees)
82
- end
83
-
84
52
  # get all the followers of this model in certain type
85
53
  #
86
54
  # Example:
@@ -91,38 +59,6 @@ module Mongoid
91
59
  rebuild_instances(self.followers.by_type(type))
92
60
  end
93
61
 
94
- # get all the followees of this model in certain type
95
- #
96
- # Example:
97
- # >> @ruby.followees_by_type("group")
98
- # => [@ruby]
99
-
100
- def followees_by_type(type)
101
- rebuild_instances(self.followees.by_type(type))
102
- end
103
-
104
- # follow some model
105
-
106
- def follow(*models)
107
- models.each do |model|
108
- unless model == self or self.follower_of?(model) or model.followee_of?(self)
109
- model.followers.create!(:f_type => self.class.name, :f_id => self.id.to_s)
110
- self.followees.create!(:f_type => model.class.name, :f_id => model.id.to_s)
111
- end
112
- end
113
- end
114
-
115
- # unfollow some model
116
-
117
- def unfollow(*models)
118
- models.each do |model|
119
- unless model == self or !self.follower_of?(model) or !model.followee_of?(self)
120
- model.followers.by_model(self).first.destroy
121
- self.followees.by_model(model).first.destroy
122
- end
123
- end
124
- end
125
-
126
62
  # get the number of followers
127
63
  #
128
64
  # Example:
@@ -133,16 +69,6 @@ module Mongoid
133
69
  self.followers.count
134
70
  end
135
71
 
136
- # get the number of followees
137
- #
138
- # Example:
139
- # >> @jim.followers_count
140
- # => 1
141
-
142
- def followees_count
143
- self.followees.count
144
- end
145
-
146
72
  # get the number of followers in certain type
147
73
  #
148
74
  # Example:
@@ -153,16 +79,6 @@ module Mongoid
153
79
  self.followers.by_type(type).count
154
80
  end
155
81
 
156
- # get the number of followees in certain type
157
- #
158
- # Example:
159
- # >> @jim.followees_count_by_type("group")
160
- # => 1
161
-
162
- def followees_count_by_type(type)
163
- self.followees.by_type(type).count
164
- end
165
-
166
82
  private
167
83
  def rebuild_instances(follows)
168
84
  follow_list = []
@@ -0,0 +1,113 @@
1
+ module Mongoid
2
+ module Follower
3
+ extend ActiveSupport::Concern
4
+
5
+ included do |base|
6
+ base.has_many :followees, :class_name => "Follow", :as => :following, :dependent => :destroy
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ # get certain model's followers of this type
12
+ #
13
+ # Example:
14
+ # >> @jim = User.new
15
+ # >> @ruby = Group.new
16
+ # >> @jim.save
17
+ # >> @ruby.save
18
+ #
19
+ # >> @jim.follow(@ruby)
20
+ # >> User.followers_of(@ruby)
21
+ # => [@jim]
22
+ #
23
+ # Arguments:
24
+ # model: instance of some followable model
25
+
26
+ def followers_of(model)
27
+ model.followers_by_type(self.name)
28
+ end
29
+
30
+ end
31
+
32
+ # see if this model is follower of some model
33
+ #
34
+ # Example:
35
+ # >> @jim.follower_of?(@ruby)
36
+ # => true
37
+
38
+ def follower_of?(model)
39
+ 0 < self.followees.by_model(model).limit(1).count * model.followers.by_model(self).limit(1).count
40
+ end
41
+
42
+ # get all the followees of this model, same with classmethod followees_of
43
+ #
44
+ # Example:
45
+ # >> @jim.all_followees
46
+ # => [@ruby]
47
+
48
+ def all_followees
49
+ rebuild_instances(self.followees)
50
+ end
51
+
52
+ # get all the followees of this model in certain type
53
+ #
54
+ # Example:
55
+ # >> @ruby.followees_by_type("group")
56
+ # => [@ruby]
57
+
58
+ def followees_by_type(type)
59
+ rebuild_instances(self.followees.by_type(type))
60
+ end
61
+
62
+ # follow some model
63
+
64
+ def follow(*models)
65
+ models.each do |model|
66
+ unless model == self or self.follower_of?(model) or model.followee_of?(self)
67
+ model.followers.create!(:f_type => self.class.name, :f_id => self.id.to_s)
68
+ self.followees.create!(:f_type => model.class.name, :f_id => model.id.to_s)
69
+ end
70
+ end
71
+ end
72
+
73
+ # unfollow some model
74
+
75
+ def unfollow(*models)
76
+ models.each do |model|
77
+ unless model == self or !self.follower_of?(model) or !model.followee_of?(self)
78
+ model.followers.by_model(self).first.destroy
79
+ self.followees.by_model(model).first.destroy
80
+ end
81
+ end
82
+ end
83
+
84
+ # get the number of followees
85
+ #
86
+ # Example:
87
+ # >> @jim.followers_count
88
+ # => 1
89
+
90
+ def followees_count
91
+ self.followees.count
92
+ end
93
+
94
+ # get the number of followers in certain type
95
+ #
96
+ # Example:
97
+ # >> @ruby.followers_count_by_type("user")
98
+ # => 1
99
+
100
+ def followees_count_by_type(type)
101
+ self.followees.by_type(type).count
102
+ end
103
+
104
+ private
105
+ def rebuild_instances(follows)
106
+ follow_list = []
107
+ follows.each do |follow|
108
+ follow_list << follow.f_type.capitalize.constantize.find(follow.f_id)
109
+ end
110
+ follow_list
111
+ end
112
+ end
113
+ end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Followable
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -1,2 +1,3 @@
1
1
  require File.join(File.dirname(__FILE__), "mongoid_followable/followable")
2
+ require File.join(File.dirname(__FILE__), "mongoid_followable/follower")
2
3
  require File.join(File.dirname(__FILE__), "../app/models/follow")
data/spec/models/user.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  class User
2
2
  include Mongoid::Document
3
3
  include Mongoid::Followable
4
+ include Mongoid::Follower
4
5
  end
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_followable
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 25
4
5
  prerelease:
5
- version: 0.1.0
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
6
11
  platform: ruby
7
12
  authors:
8
13
  - Jie Fan
@@ -21,6 +26,11 @@ dependencies:
21
26
  requirements:
22
27
  - - ~>
23
28
  - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 1
24
34
  version: 2.0.1
25
35
  type: :runtime
26
36
  version_requirements: *id001
@@ -32,6 +42,11 @@ dependencies:
32
42
  requirements:
33
43
  - - ~>
34
44
  - !ruby/object:Gem::Version
45
+ hash: 27
46
+ segments:
47
+ - 1
48
+ - 3
49
+ - 0
35
50
  version: 1.3.0
36
51
  type: :runtime
37
52
  version_requirements: *id002
@@ -43,6 +58,10 @@ dependencies:
43
58
  requirements:
44
59
  - - ~>
45
60
  - !ruby/object:Gem::Version
61
+ hash: 7
62
+ segments:
63
+ - 0
64
+ - 6
46
65
  version: "0.6"
47
66
  type: :development
48
67
  version_requirements: *id003
@@ -54,6 +73,10 @@ dependencies:
54
73
  requirements:
55
74
  - - ~>
56
75
  - !ruby/object:Gem::Version
76
+ hash: 15
77
+ segments:
78
+ - 2
79
+ - 6
57
80
  version: "2.6"
58
81
  type: :development
59
82
  version_requirements: *id004
@@ -84,6 +107,7 @@ files:
84
107
  - app/models/follow.rb
85
108
  - lib/mongoid_followable.rb
86
109
  - lib/mongoid_followable/followable.rb
110
+ - lib/mongoid_followable/follower.rb
87
111
  - lib/mongoid_followable/version.rb
88
112
  - mongoid_followable.gemspec
89
113
  - spec/models/group.rb
@@ -104,17 +128,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
128
  requirements:
105
129
  - - ">="
106
130
  - !ruby/object:Gem::Version
131
+ hash: 3
132
+ segments:
133
+ - 0
107
134
  version: "0"
108
135
  required_rubygems_version: !ruby/object:Gem::Requirement
109
136
  none: false
110
137
  requirements:
111
138
  - - ">="
112
139
  - !ruby/object:Gem::Version
140
+ hash: 3
141
+ segments:
142
+ - 0
113
143
  version: "0"
114
144
  requirements: []
115
145
 
116
146
  rubyforge_project: mongoid_followable
117
- rubygems_version: 1.6.2
147
+ rubygems_version: 1.4.1
118
148
  signing_key:
119
149
  specification_version: 3
120
150
  summary: adds following feature to models