mongoid_socializer_actions 1.2.8 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/app/models/follow.rb +12 -0
- data/lib/mongoid_socializer_actions.rb +3 -0
- data/lib/mongoid_socializer_actions/followable.rb +25 -0
- data/lib/mongoid_socializer_actions/follower.rb +121 -0
- data/lib/mongoid_socializer_actions/version.rb +1 -1
- data/spec/mongoid/models/album.rb +1 -0
- data/spec/mongoid/models/photo.rb +1 -0
- data/spec/mongoid/models/user.rb +1 -0
- data/spec/mongoid/mongoid_socializer_actions/follows_spec.rb +120 -0
- data/spec/spec_helper.rb +2 -1
- metadata +52 -50
- data/.rvmrc +0 -1
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dac3c7add84b70827b1b3a544a95af49ba0ae805
|
4
|
+
data.tar.gz: d47ebf21a0ffff88f0d467478db54bb6007b154a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e755c87fad0900ad37e3b031518727a4559b53caa49d704cd49caf46a4f7b34d265be6cf5a2bfe9b38a100509539e91535366b9b166227c59a9534428f5b8c6
|
7
|
+
data.tar.gz: 058b7b9156bdd6d0739f7f02aa444461b2e89672e17b5f8b589b994397e2ea9443e57aaf1c7e1a34f18bc18581e7ba7761440c72f8d06a86658784cd55dbb29c
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Mongoid
|
2
|
+
class Follow
|
3
|
+
include Mongoid::Document
|
4
|
+
include Mongoid::Timestamps::Created
|
5
|
+
|
6
|
+
validates :follower, presence: true
|
7
|
+
validates :followable, presence: true
|
8
|
+
|
9
|
+
belongs_to :follower, :class_name => Socializer.user_class_name, :inverse_of => :follows
|
10
|
+
belongs_to :followable, :polymorphic => true
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Followable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do |base|
|
6
|
+
base.field :followers_count, :type => Integer, :default => 0
|
7
|
+
base.has_many :follows, :class_name => 'Mongoid::Follow', :as => :followable, :dependent => :destroy
|
8
|
+
base.has_and_belongs_to_many :followers, :class_name => Socializer.user_class_name, :inverse_of => nil
|
9
|
+
end
|
10
|
+
|
11
|
+
# know if self is liked by model
|
12
|
+
#
|
13
|
+
# Example:
|
14
|
+
# => @photo.liked_by?(@john)
|
15
|
+
# => true
|
16
|
+
|
17
|
+
def followed_by?(follower)
|
18
|
+
follower_ids.include?(follower.id)
|
19
|
+
end
|
20
|
+
|
21
|
+
def followers_with_followers_eager_loaded
|
22
|
+
follows.includes(:follower)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Follower
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do |base|
|
6
|
+
base.field :follows_count, :type => Integer, :default => 0
|
7
|
+
base.has_many :follows, :class_name => 'Mongoid::Follow', :inverse_of => :follower, :dependent => :destroy
|
8
|
+
end
|
9
|
+
|
10
|
+
# follow a model
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
# => @john.follow(@page)
|
14
|
+
def follow(model)
|
15
|
+
unless self.followed?(model)
|
16
|
+
model.before_followed_by(self) if model.respond_to?('before_followed_by')
|
17
|
+
model.follows.create!(follower: self)
|
18
|
+
model.followers << self
|
19
|
+
model.inc(:followers_count, 1)
|
20
|
+
model.after_followed_by(self) if model.respond_to?('after_followed_by')
|
21
|
+
self.before_follow(model) if self.respond_to?('before_follow')
|
22
|
+
self.inc(:follows_count, 1)
|
23
|
+
self.after_follow(model) if self.respond_to?('after_follow')
|
24
|
+
return true
|
25
|
+
else
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# unfollow a model
|
31
|
+
#
|
32
|
+
# Example:
|
33
|
+
# => @john.unfollow(@page)
|
34
|
+
def unfollow(model)
|
35
|
+
if self.id != model.id && self.followed?(model)
|
36
|
+
|
37
|
+
model.reload
|
38
|
+
self.reload
|
39
|
+
|
40
|
+
model.before_unfollowed_by(self) if model.respond_to?('before_unfollowed_by')
|
41
|
+
|
42
|
+
follows.where(:followable_type => model.class.name, :followable_id => model.id).destroy
|
43
|
+
model.followers.delete(self)
|
44
|
+
|
45
|
+
model.inc(:followers_count, -1)
|
46
|
+
model.after_unfollowed_by(self) if model.respond_to?('after_unfollowed_by')
|
47
|
+
self.before_unfollow(model) if self.respond_to?('before_unfollow')
|
48
|
+
|
49
|
+
self.inc(:follows_count, -1)
|
50
|
+
self.after_unfollow(model) if self.respond_to?('after_unfollow')
|
51
|
+
|
52
|
+
return true
|
53
|
+
else
|
54
|
+
return false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# know if user is already following model
|
59
|
+
#
|
60
|
+
# Example:
|
61
|
+
# => @john.followed?(@page)
|
62
|
+
# => true
|
63
|
+
def followed?(model)
|
64
|
+
model.follower_ids.include?(self.id)
|
65
|
+
end
|
66
|
+
|
67
|
+
# get follows count by model
|
68
|
+
#
|
69
|
+
# Example:
|
70
|
+
# => @john.follows_count_by_model("Photo")
|
71
|
+
# => 1
|
72
|
+
def follows_count_by_model(model)
|
73
|
+
self.follows.where(:followable_type => model).count
|
74
|
+
end
|
75
|
+
|
76
|
+
# view all selfs follows
|
77
|
+
#
|
78
|
+
# Example:
|
79
|
+
# => @john.follows_objects
|
80
|
+
# => [@photo]
|
81
|
+
def followed_objects
|
82
|
+
get_followed_objects_of_kind
|
83
|
+
end
|
84
|
+
|
85
|
+
# view all selfs follows by model
|
86
|
+
#
|
87
|
+
# Example:
|
88
|
+
# => @john.get_followed_objects_of_kind('Photo')
|
89
|
+
# => [@photo]
|
90
|
+
def get_followed_objects_of_kind(model = nil)
|
91
|
+
if model
|
92
|
+
user_follows = follows.where(followable_type: model)
|
93
|
+
extract_follows_from(user_follows, model)
|
94
|
+
else
|
95
|
+
followable_types = follows.map(&:followable_type).uniq
|
96
|
+
followable_types.collect do |followable_type|
|
97
|
+
user_follows = follows.select{ |follow| follow.followable_type == followable_type }
|
98
|
+
extract_follows_from(user_follows, followable_type)
|
99
|
+
end.flatten
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def extract_follows_from(user_follows, followable_type)
|
104
|
+
return [] unless user_follows.present?
|
105
|
+
followable_ids = user_follows.map(&:followable_id)
|
106
|
+
followable_type.constantize.find(followable_ids)
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def method_missing(missing_method, *args, &block)
|
112
|
+
if missing_method.to_s =~ /^(.+)_follows_count$/
|
113
|
+
follows_count_by_model($1.camelize)
|
114
|
+
elsif missing_method.to_s =~ /^followed_(.+)$/
|
115
|
+
get_followed_objects_of_kind($1.singularize.camelize)
|
116
|
+
else
|
117
|
+
super
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/spec/mongoid/models/user.rb
CHANGED
@@ -0,0 +1,120 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Mongoid::Follower do
|
4
|
+
describe User do
|
5
|
+
before :each do
|
6
|
+
@john = User.create(name: 'john')
|
7
|
+
@jashua = User.create(name: 'jashua')
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should have no follows" do
|
11
|
+
[@john, @jashua].each {|u| u.followed_objects.should be_empty}
|
12
|
+
end
|
13
|
+
|
14
|
+
describe Photo do
|
15
|
+
before :each do
|
16
|
+
@photo1 = Photo.create
|
17
|
+
@photo2 = Photo.create
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should have no followers" do
|
21
|
+
[@photo1, @photo2].each { |t| t.followers.should be_empty }
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should be followable" do
|
25
|
+
expect{ @john.follow(@photo1) }.to change(Mongoid::Follow, :count).by(1)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be followed by follower" do
|
29
|
+
@john.follow(@photo1)
|
30
|
+
@photo1.reload.followed_by?(@john).should be_true
|
31
|
+
@john.followed?(@photo1).should be_true
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should be unfollowed by follower" do
|
35
|
+
@john.follow(@photo1)
|
36
|
+
@john.unfollow(@photo1)
|
37
|
+
@photo1.reload.followed_by?(@john).should be_false
|
38
|
+
@john.followed?(@photo1).should be_false
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should not be followd by others" do
|
42
|
+
@photo1.followed_by?(@jashua).should_not be_true
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should have the followr as follower" do
|
46
|
+
@john.follow(@photo1)
|
47
|
+
@photo1.followers.should include @john
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should not have others as followr" do
|
51
|
+
@photo1.followers.should_not include @jashua
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should be followable by multiple followrs" do
|
55
|
+
@jashua.follow(@photo1).should be_true
|
56
|
+
@john.follow(@photo1).should be_true
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should be increase follows_count" do
|
60
|
+
expect{ @jashua.follow(@photo1) }.to change(@jashua, :follows_count).by(1)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should be increase follows_count" do
|
64
|
+
expect{ @jashua.follow(@photo1) }.to change(@photo1, :followers_count).by(1)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should be followd by multiple followrs" do
|
68
|
+
@jashua.follow(@photo1)
|
69
|
+
@john.follow(@photo1)
|
70
|
+
@photo1.followers.should include @john, @jashua
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should have the correct followers count" do
|
74
|
+
@jashua.follow(@photo1)
|
75
|
+
@john.follow(@photo1)
|
76
|
+
@photo1.followers_count.should be 2
|
77
|
+
@jashua.follows_count.should be 1
|
78
|
+
@john.follows_count.should be 1
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should be unlikable" do
|
82
|
+
@jashua.follow(@photo1)
|
83
|
+
@jashua.unfollow(@photo1).should be_true
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should not be unlikable by unfollowr" do
|
87
|
+
@jashua.unfollow(@photo1).should be_false
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should not include former followr" do
|
91
|
+
@photo1.followers.should_not include @jashua
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should not be included in former follows" do
|
95
|
+
@jashua.followed_objects.should_not include @photo1
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "get follows by model" do
|
100
|
+
before :each do
|
101
|
+
@photo1 = Photo.create
|
102
|
+
@photo2 = Photo.create
|
103
|
+
@album1 = Album.create
|
104
|
+
@album2 = Album.create
|
105
|
+
@john.follow(@photo1)
|
106
|
+
@john.follow(@photo2)
|
107
|
+
@john.follow(@album1)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should return photo follows count" do
|
111
|
+
@john.photo_follows_count.should == 2
|
112
|
+
@john.album_follows_count.should == 1
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should return followd photos " do
|
116
|
+
@john.followed_photos.should == [@photo1, @photo2]
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -10,8 +10,9 @@ models_folder = File.join(File.dirname(__FILE__), 'mongoid/models')
|
|
10
10
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
11
11
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
12
12
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'app'))
|
13
|
+
require 'pry-rails'
|
13
14
|
|
14
|
-
Mongoid.load! '
|
15
|
+
Mongoid.load! File.expand_path('../config/mongoid.yml', __FILE__), :test
|
15
16
|
|
16
17
|
require 'mongoid_socializer_actions'
|
17
18
|
require 'rspec'
|
metadata
CHANGED
@@ -1,60 +1,65 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_socializer_actions
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
6
|
+
authors:
|
7
7
|
- Sreehari B
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2013-11-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
16
14
|
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
17
20
|
type: :runtime
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- -
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version:
|
24
|
-
|
25
|
-
- !ruby/object:Gem::Dependency
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
26
28
|
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.2'
|
27
34
|
type: :runtime
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- -
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version:
|
34
|
-
version:
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.2'
|
35
41
|
description: Like, comment, share mongoid documents.
|
36
|
-
email:
|
42
|
+
email:
|
37
43
|
- sreehari@activesphere.com
|
38
44
|
executables: []
|
39
|
-
|
40
45
|
extensions: []
|
41
|
-
|
42
46
|
extra_rdoc_files: []
|
43
|
-
|
44
|
-
files:
|
47
|
+
files:
|
45
48
|
- .gitignore
|
46
49
|
- .rspec
|
47
|
-
- .rvmrc
|
48
50
|
- Gemfile
|
49
51
|
- Rakefile
|
50
52
|
- Readme.md
|
51
53
|
- app/models/comment.rb
|
54
|
+
- app/models/follow.rb
|
52
55
|
- app/models/like.rb
|
53
56
|
- app/models/share.rb
|
54
57
|
- lib/mongoid_socializer_actions.rb
|
55
58
|
- lib/mongoid_socializer_actions/commentable.rb
|
56
59
|
- lib/mongoid_socializer_actions/commenter.rb
|
57
60
|
- lib/mongoid_socializer_actions/config.rb
|
61
|
+
- lib/mongoid_socializer_actions/followable.rb
|
62
|
+
- lib/mongoid_socializer_actions/follower.rb
|
58
63
|
- lib/mongoid_socializer_actions/helper.rb
|
59
64
|
- lib/mongoid_socializer_actions/likeable.rb
|
60
65
|
- lib/mongoid_socializer_actions/liker.rb
|
@@ -68,45 +73,42 @@ files:
|
|
68
73
|
- spec/mongoid/models/user.rb
|
69
74
|
- spec/mongoid/mongoid_socializer_actions/comments_spec.rb
|
70
75
|
- spec/mongoid/mongoid_socializer_actions/config_spec.rb
|
76
|
+
- spec/mongoid/mongoid_socializer_actions/follows_spec.rb
|
71
77
|
- spec/mongoid/mongoid_socializer_actions/likes_spec.rb
|
72
78
|
- spec/mongoid/mongoid_socializer_actions/shares_spec.rb
|
73
79
|
- spec/spec_helper.rb
|
74
|
-
has_rdoc: true
|
75
80
|
homepage: https://github.com/sreehari/mongoid_socializer_actions
|
76
81
|
licenses: []
|
77
|
-
|
82
|
+
metadata: {}
|
78
83
|
post_install_message:
|
79
84
|
rdoc_options: []
|
80
|
-
|
81
|
-
require_paths:
|
85
|
+
require_paths:
|
82
86
|
- lib
|
83
87
|
- app
|
84
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
-
requirements:
|
86
|
-
- -
|
87
|
-
- !ruby/object:Gem::Version
|
88
|
-
version:
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
version: "0"
|
95
|
-
version:
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
96
98
|
requirements: []
|
97
|
-
|
98
99
|
rubyforge_project:
|
99
|
-
rubygems_version: 1.
|
100
|
+
rubygems_version: 2.1.10
|
100
101
|
signing_key:
|
101
|
-
specification_version:
|
102
|
+
specification_version: 4
|
102
103
|
summary: Ability to comment, like, share, tag mongoid documents
|
103
|
-
test_files:
|
104
|
+
test_files:
|
104
105
|
- spec/config/mongoid.yml
|
105
106
|
- spec/mongoid/models/album.rb
|
106
107
|
- spec/mongoid/models/photo.rb
|
107
108
|
- spec/mongoid/models/user.rb
|
108
109
|
- spec/mongoid/mongoid_socializer_actions/comments_spec.rb
|
109
110
|
- spec/mongoid/mongoid_socializer_actions/config_spec.rb
|
111
|
+
- spec/mongoid/mongoid_socializer_actions/follows_spec.rb
|
110
112
|
- spec/mongoid/mongoid_socializer_actions/likes_spec.rb
|
111
113
|
- spec/mongoid/mongoid_socializer_actions/shares_spec.rb
|
112
114
|
- spec/spec_helper.rb
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use ruby-1.9.3-p125@mongoid_socializer_actions --create
|