mongo_followable 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.idea/encodings.xml +5 -0
- data/.idea/misc.xml +5 -0
- data/.idea/modules.xml +9 -0
- data/.idea/mongo_followable.iml +17 -0
- data/.idea/vcs.xml +7 -0
- data/README.rdoc +9 -3
- data/lib/mongo_followable/followable.rb +37 -19
- data/lib/mongo_followable/follower.rb +45 -23
- data/lib/mongo_followable/railtie.rb +9 -0
- data/lib/mongo_followable/version.rb +1 -1
- data/lib/mongo_followable.rb +2 -9
- metadata +82 -59
data/.idea/encodings.xml
ADDED
data/.idea/misc.xml
ADDED
data/.idea/modules.xml
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project version="4">
|
3
|
+
<component name="ProjectModuleManager">
|
4
|
+
<modules>
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/mongo_followable.iml" filepath="$PROJECT_DIR$/.idea/mongo_followable.iml" />
|
6
|
+
</modules>
|
7
|
+
</component>
|
8
|
+
</project>
|
9
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<module type="RUBY_MODULE" version="4">
|
3
|
+
<component name="NewModuleRootManager">
|
4
|
+
<content url="file://$MODULE_DIR$" />
|
5
|
+
<orderEntry type="inheritedJdk" />
|
6
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
7
|
+
<orderEntry type="library" scope="PROVIDED" name="builder (v3.0.0, RVM: ruby-1.9.3-p125) [gem]" level="application" />
|
8
|
+
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.1.3, RVM: ruby-1.9.3-p125) [gem]" level="application" />
|
9
|
+
<orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.1.3, RVM: ruby-1.9.3-p125) [gem]" level="application" />
|
10
|
+
<orderEntry type="library" scope="PROVIDED" name="i18n (v0.6.0, RVM: ruby-1.9.3-p125) [gem]" level="application" />
|
11
|
+
<orderEntry type="library" scope="PROVIDED" name="mongo (v1.5.2, RVM: ruby-1.9.3-p125) [gem]" level="application" />
|
12
|
+
<orderEntry type="library" scope="PROVIDED" name="mongoid (v2.4.7, RVM: ruby-1.9.3-p125) [gem]" level="application" />
|
13
|
+
<orderEntry type="library" scope="PROVIDED" name="multi_json (v1.0.4, RVM: ruby-1.9.3-p125) [gem]" level="application" />
|
14
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec (v2.7.0, RVM: ruby-1.9.3-p125) [gem]" level="application" />
|
15
|
+
</component>
|
16
|
+
</module>
|
17
|
+
|
data/.idea/vcs.xml
ADDED
data/README.rdoc
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
Now works for both Mongoid and Mongo_Mapper!
|
4
4
|
|
5
|
-
see http://www.thesika.info/articles/mongo_followable_update for details
|
6
|
-
|
7
5
|
== Installation
|
8
6
|
|
9
7
|
In console:
|
@@ -25,6 +23,12 @@ To make model followable you need to include Mongo::Followable into your model;
|
|
25
23
|
include Mongo::Followable
|
26
24
|
end
|
27
25
|
|
26
|
+
Now you can set configuration for mongo_followable in environment/*.rb:
|
27
|
+
|
28
|
+
# Note: for current version, you can only set the config once(first time the application is created).
|
29
|
+
# This should be fixed in next version.
|
30
|
+
config.mongo_followable = { :authorization => false, :history => false } # this is default value
|
31
|
+
|
28
32
|
Now you can set authorization:
|
29
33
|
current_user.set_authorization('user', 'game') # now current_user cannot follow User and Game model
|
30
34
|
current_user.unset_authorization('User', 'Game')
|
@@ -81,8 +85,10 @@ Getting a model's followers/followees by type is also possible:
|
|
81
85
|
@group.followers_by_type("user")
|
82
86
|
@user.followees_by_type("group")
|
83
87
|
|
84
|
-
|
88
|
+
Dealing with model names:
|
85
89
|
|
90
|
+
@group.followers_by_type("user")
|
91
|
+
@group.followers_by_type("User")
|
86
92
|
@group.followers_by_type("user_post") # both are fine
|
87
93
|
@user.followees_by_type("GroupPost")
|
88
94
|
|
@@ -4,12 +4,24 @@ module Mongo
|
|
4
4
|
|
5
5
|
included do |base|
|
6
6
|
if defined?(Mongoid)
|
7
|
-
|
8
|
-
|
7
|
+
if CONFIG[:authorization]
|
8
|
+
base.field :cannot_followed, :type => Array, :default => []
|
9
|
+
end
|
10
|
+
|
11
|
+
if CONFIG[:history]
|
12
|
+
base.field :followed_history, :type => Array, :default => []
|
13
|
+
end
|
14
|
+
|
9
15
|
base.has_many :followers, :class_name => "Follow", :as => :followable, :dependent => :destroy
|
10
16
|
elsif defined?(MongoMapper)
|
11
|
-
|
12
|
-
|
17
|
+
if CONFIG[:authorization]
|
18
|
+
base.key :cannot_followed, :type => Array, :default => []
|
19
|
+
end
|
20
|
+
|
21
|
+
if CONFIG[:history]
|
22
|
+
base.key :followed_history, :type => Array, :default => []
|
23
|
+
end
|
24
|
+
|
13
25
|
base.many :followers, :class_name => "Follow", :as => :followable, :dependent => :destroy
|
14
26
|
end
|
15
27
|
end
|
@@ -89,19 +101,21 @@ module Mongo
|
|
89
101
|
# >> @ruby.set_authorization('user')
|
90
102
|
# => true
|
91
103
|
|
92
|
-
|
93
|
-
|
104
|
+
if CONFIG[:authorization]
|
105
|
+
define_method(:set_authorization) do |*models|
|
106
|
+
models.each do |model|
|
94
107
|
|
95
|
-
|
108
|
+
self.cannot_followed << model.safe_capitalize
|
109
|
+
end
|
110
|
+
self.save
|
96
111
|
end
|
97
|
-
self.save
|
98
|
-
end
|
99
112
|
|
100
|
-
|
101
|
-
|
102
|
-
|
113
|
+
define_method(:unset_authorization) do |*models|
|
114
|
+
models.each do |model|
|
115
|
+
self.cannot_followed -= [model.safe_capitalize]
|
116
|
+
end
|
117
|
+
self.save
|
103
118
|
end
|
104
|
-
self.save
|
105
119
|
end
|
106
120
|
|
107
121
|
# see if this model is followee of some model
|
@@ -140,7 +154,9 @@ module Mongo
|
|
140
154
|
end
|
141
155
|
|
142
156
|
models.each do |model|
|
143
|
-
|
157
|
+
term = CONFIG[:authorization] ? (self.cannot_followed.include?(model.class.name) or model.cannot_follow.include?(self.class.name)) : false
|
158
|
+
|
159
|
+
unless model == self or !self.followee_of?(model) or !model.follower_of?(self) or term
|
144
160
|
model.followees.by_model(self).first.destroy
|
145
161
|
self.followers.by_model(model).first.destroy
|
146
162
|
end
|
@@ -189,12 +205,14 @@ module Mongo
|
|
189
205
|
# >> @ruby.ever_followed
|
190
206
|
# => [@jim]
|
191
207
|
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
208
|
+
if CONFIG[:history]
|
209
|
+
define_method(:ever_followed) do
|
210
|
+
follow = []
|
211
|
+
self.followed_history.each do |h|
|
212
|
+
follow << h.split('_')[0].constantize.find(h.split('_')[1])
|
213
|
+
end
|
214
|
+
follow
|
196
215
|
end
|
197
|
-
follow
|
198
216
|
end
|
199
217
|
|
200
218
|
# return if there is any common followers
|
@@ -4,12 +4,24 @@ module Mongo
|
|
4
4
|
|
5
5
|
included do |base|
|
6
6
|
if defined?(Mongoid)
|
7
|
-
|
8
|
-
|
7
|
+
if CONFIG[:authorization]
|
8
|
+
base.field :cannot_follow, :type => Array, :default => []
|
9
|
+
end
|
10
|
+
|
11
|
+
if CONFIG[:history]
|
12
|
+
base.field :follow_history, :type => Array, :default => []
|
13
|
+
end
|
14
|
+
|
9
15
|
base.has_many :followees, :class_name => "Follow", :as => :following, :dependent => :destroy
|
10
16
|
elsif defined?(MongoMapper)
|
11
|
-
|
12
|
-
|
17
|
+
if CONFIG[:authorization]
|
18
|
+
base.key :cannot_follow, :type => Array, :default => []
|
19
|
+
end
|
20
|
+
|
21
|
+
if CONFIG[:history]
|
22
|
+
base.key :follow_history, :type => Array, :default => []
|
23
|
+
end
|
24
|
+
|
13
25
|
base.many :followees, :class_name => "Follow", :as => :following, :dependent => :destroy
|
14
26
|
end
|
15
27
|
end
|
@@ -89,20 +101,22 @@ module Mongo
|
|
89
101
|
# >> @jim.set_authorization('group', 'user')
|
90
102
|
# => true
|
91
103
|
|
92
|
-
|
93
|
-
|
94
|
-
|
104
|
+
if CONFIG[:authorization]
|
105
|
+
define_method(:set_authorization) do |*models|
|
106
|
+
models.each do |model|
|
107
|
+
self.cannot_follow << model.safe_capitalize
|
108
|
+
end
|
109
|
+
self.save
|
95
110
|
end
|
96
|
-
self.save
|
97
|
-
end
|
98
111
|
|
99
|
-
|
112
|
+
#unset which mongoid user cannot follow
|
100
113
|
|
101
|
-
|
102
|
-
|
103
|
-
|
114
|
+
define_method(:unset_authorization) do |*models|
|
115
|
+
models.each do |model|
|
116
|
+
self.cannot_follow -= [model.safe_capitalize]
|
117
|
+
end
|
118
|
+
self.save
|
104
119
|
end
|
105
|
-
self.save
|
106
120
|
end
|
107
121
|
|
108
122
|
# see if this model is follower of some model
|
@@ -153,12 +167,18 @@ module Mongo
|
|
153
167
|
end
|
154
168
|
|
155
169
|
models.each do |model|
|
156
|
-
|
170
|
+
term = CONFIG[:authorization] ? (self.cannot_follow.include?(model.class.name) or model.cannot_followed.include?(self.class.name)) : false
|
171
|
+
|
172
|
+
unless model == self or self.follower_of?(model) or model.followee_of?(self) or term
|
157
173
|
model.followers.create!(:f_type => self.class.name, :f_id => self.id.to_s)
|
158
|
-
model.followed_history << self.class.name + '_' + self.id.to_s
|
159
|
-
model.save
|
160
174
|
self.followees.create!(:f_type => model.class.name, :f_id => model.id.to_s)
|
161
|
-
|
175
|
+
|
176
|
+
if CONFIG[:history]
|
177
|
+
model.followed_history << self.class.name + '_' + self.id.to_s
|
178
|
+
self.follow_history << model.class.name + '_' + model.id.to_s
|
179
|
+
end
|
180
|
+
|
181
|
+
model.save
|
162
182
|
self.save
|
163
183
|
end
|
164
184
|
end
|
@@ -211,12 +231,14 @@ module Mongo
|
|
211
231
|
# >> @jim.ever_follow
|
212
232
|
# => [@ruby]
|
213
233
|
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
234
|
+
if CONFIG[:history]
|
235
|
+
define_method(:ever_follow) do
|
236
|
+
follow = []
|
237
|
+
self.follow_history.each do |h|
|
238
|
+
follow << h.split('_')[0].constantize.find(h.split('_')[1])
|
239
|
+
end
|
240
|
+
follow
|
218
241
|
end
|
219
|
-
follow
|
220
242
|
end
|
221
243
|
|
222
244
|
# return if there is any common followees
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module Mongo
|
2
|
+
CONFIG = { :authorization => false, :history => false }
|
3
|
+
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
config.after_initialize do
|
6
|
+
CONFIG.merge!(Rails.application.config.mongo_followable) if Rails.application.config.respond_to?(:mongo_followable)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
data/lib/mongo_followable.rb
CHANGED
@@ -1,12 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
def initialize(msg, info)
|
4
|
-
@info = info
|
5
|
-
super(msg)
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
class NoMongoidOrMongoMapperError < FollowableError; end
|
1
|
+
if defined?(Rails)
|
2
|
+
require File.join(File.dirname(__FILE__), "mongo_followable/railtie")
|
10
3
|
end
|
11
4
|
|
12
5
|
if defined?(Mongoid) or defined?(MongoMapper)
|
metadata
CHANGED
@@ -1,83 +1,109 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_followable
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
4
5
|
prerelease:
|
5
|
-
version: 0.2.5
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Jie Fan
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-05-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
16
15
|
name: rspec
|
17
|
-
|
18
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
19
17
|
none: false
|
20
|
-
requirements:
|
18
|
+
requirements:
|
21
19
|
- - ~>
|
22
|
-
- !ruby/object:Gem::Version
|
20
|
+
- !ruby/object:Gem::Version
|
23
21
|
version: 2.7.0
|
24
22
|
type: :development
|
25
|
-
version_requirements: *id001
|
26
|
-
- !ruby/object:Gem::Dependency
|
27
|
-
name: mongoid
|
28
23
|
prerelease: false
|
29
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
25
|
none: false
|
31
|
-
requirements:
|
26
|
+
requirements:
|
32
27
|
- - ~>
|
33
|
-
- !ruby/object:Gem::Version
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.7.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: mongoid
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
34
37
|
version: 2.4.0
|
35
38
|
type: :development
|
36
|
-
version_requirements: *id002
|
37
|
-
- !ruby/object:Gem::Dependency
|
38
|
-
name: mongo_mapper
|
39
39
|
prerelease: false
|
40
|
-
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
|
-
requirements:
|
42
|
+
requirements:
|
43
43
|
- - ~>
|
44
|
-
- !ruby/object:Gem::Version
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.4.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: mongo_mapper
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
45
53
|
version: 0.10.0
|
46
54
|
type: :development
|
47
|
-
version_requirements: *id003
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: bson_ext
|
50
55
|
prerelease: false
|
51
|
-
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
57
|
none: false
|
53
|
-
requirements:
|
58
|
+
requirements:
|
54
59
|
- - ~>
|
55
|
-
- !ruby/object:Gem::Version
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.10.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: bson_ext
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
56
69
|
version: 1.5.0
|
57
70
|
type: :development
|
58
|
-
version_requirements: *id004
|
59
|
-
- !ruby/object:Gem::Dependency
|
60
|
-
name: database_cleaner
|
61
71
|
prerelease: false
|
62
|
-
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.5.0
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: database_cleaner
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
63
81
|
none: false
|
64
|
-
requirements:
|
82
|
+
requirements:
|
65
83
|
- - ~>
|
66
|
-
- !ruby/object:Gem::Version
|
84
|
+
- !ruby/object:Gem::Version
|
67
85
|
version: 0.7.0
|
68
86
|
type: :development
|
69
|
-
|
70
|
-
|
71
|
-
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 0.7.0
|
94
|
+
description: ! ' Mongo Followable adds following feature to mongoid/mongo_mapper '
|
95
|
+
email:
|
72
96
|
- ustc.flyingfox@gmail.com
|
73
97
|
executables: []
|
74
|
-
|
75
98
|
extensions: []
|
76
|
-
|
77
99
|
extra_rdoc_files: []
|
78
|
-
|
79
|
-
files:
|
100
|
+
files:
|
80
101
|
- .gitignore
|
102
|
+
- .idea/encodings.xml
|
103
|
+
- .idea/misc.xml
|
104
|
+
- .idea/modules.xml
|
105
|
+
- .idea/mongo_followable.iml
|
106
|
+
- .idea/vcs.xml
|
81
107
|
- .rakeTasks
|
82
108
|
- Gemfile
|
83
109
|
- LICENSE.txt
|
@@ -88,6 +114,7 @@ files:
|
|
88
114
|
- lib/mongo_followable/core_ext/string.rb
|
89
115
|
- lib/mongo_followable/followable.rb
|
90
116
|
- lib/mongo_followable/follower.rb
|
117
|
+
- lib/mongo_followable/railtie.rb
|
91
118
|
- lib/mongo_followable/version.rb
|
92
119
|
- mongo_followable.gemspec
|
93
120
|
- spec/mongo/followable_spec.rb
|
@@ -100,30 +127,26 @@ files:
|
|
100
127
|
- spec/spec_helper.rb
|
101
128
|
homepage: https://github.com/lastomato/mongo_followable
|
102
129
|
licenses: []
|
103
|
-
|
104
130
|
post_install_message:
|
105
131
|
rdoc_options: []
|
106
|
-
|
107
|
-
require_paths:
|
132
|
+
require_paths:
|
108
133
|
- lib
|
109
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
135
|
none: false
|
111
|
-
requirements:
|
112
|
-
- -
|
113
|
-
- !ruby/object:Gem::Version
|
114
|
-
version:
|
115
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ! '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
141
|
none: false
|
117
|
-
requirements:
|
118
|
-
- -
|
119
|
-
- !ruby/object:Gem::Version
|
120
|
-
version:
|
142
|
+
requirements:
|
143
|
+
- - ! '>='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
121
146
|
requirements: []
|
122
|
-
|
123
147
|
rubyforge_project: mongo_followable
|
124
|
-
rubygems_version: 1.8.
|
148
|
+
rubygems_version: 1.8.22
|
125
149
|
signing_key:
|
126
150
|
specification_version: 3
|
127
151
|
summary: adds following feature to mongoid/mongo_mapper
|
128
152
|
test_files: []
|
129
|
-
|