mongoid_socializer_actions 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Readme.md +5 -8
- data/lib/mongoid_socializer_actions/likeable.rb +2 -64
- data/lib/mongoid_socializer_actions/liker.rb +9 -32
- data/lib/mongoid_socializer_actions/version.rb +1 -1
- data/spec/mongoid/models/album.rb +4 -0
- data/spec/mongoid/mongoid_socializer_actions/likes_spec.rb +21 -0
- metadata +7 -5
data/Readme.md
CHANGED
@@ -48,31 +48,28 @@ You can now like objects like this:
|
|
48
48
|
|
49
49
|
You can query for likes like that:
|
50
50
|
|
51
|
-
photo.
|
51
|
+
photo.likers
|
52
52
|
# => [user]
|
53
53
|
|
54
54
|
photo.likers_count
|
55
55
|
# => 1
|
56
56
|
|
57
|
-
user.
|
57
|
+
user.liked_objects
|
58
58
|
# => [photo]
|
59
59
|
|
60
60
|
Also likes are polymorphic, so let's assume you have a second class `Album` that is including `Mongoid::Likeable` you can do something like this:
|
61
61
|
|
62
62
|
album = Album.create
|
63
63
|
user.like(album)
|
64
|
-
user.
|
64
|
+
user.liked_objects
|
65
65
|
# => [photo, album]
|
66
66
|
|
67
|
-
user.
|
67
|
+
user.liked_ablums
|
68
68
|
# => [album]
|
69
69
|
|
70
|
-
user.
|
70
|
+
user.album_likes_count
|
71
71
|
# => 1
|
72
72
|
|
73
|
-
user.all_photo_likes
|
74
|
-
# => [photo]
|
75
|
-
|
76
73
|
You get the idea. Have a look at the specs to see some more examples.
|
77
74
|
|
78
75
|
# TODOs
|
@@ -14,83 +14,21 @@ module Mongoid
|
|
14
14
|
# know if self is liked by model
|
15
15
|
#
|
16
16
|
# Example:
|
17
|
-
# => @photo.
|
17
|
+
# => @photo.liked_by?(@john)
|
18
18
|
# => true
|
19
|
-
def liker?(model)
|
20
|
-
self.likers_assoc.liked_by?(model.id)
|
21
|
-
end
|
22
19
|
|
23
20
|
def liked_by?(model)
|
24
21
|
self.likes.liked_by?(model.id)
|
25
22
|
end
|
26
23
|
|
27
|
-
# get likers count
|
28
|
-
# Note: this is a cache counter
|
29
|
-
#
|
30
|
-
# Example:
|
31
|
-
# => @photo.likers_count
|
32
|
-
# => 1
|
33
|
-
# def likers_count
|
34
|
-
# self.liked_count_field
|
35
|
-
# end
|
36
|
-
|
37
|
-
# get likers count by model
|
38
|
-
#
|
39
|
-
# Example:
|
40
|
-
# => @photo.likers_count_by_model(User)
|
41
|
-
# => 1
|
42
|
-
def likers_count_by_model(model)
|
43
|
-
self.likers_assoc.where(:like_type => model.to_s).count
|
44
|
-
end
|
45
|
-
|
46
24
|
# view all selfs likers
|
47
25
|
#
|
48
26
|
# Example:
|
49
|
-
# => @photo.
|
27
|
+
# => @photo.likers
|
50
28
|
# => [@john, @jashua]
|
51
29
|
def likers
|
52
30
|
ids = likes.collect{ |like| like.liker_id }
|
53
31
|
ids.present? ? User.find(ids) : []
|
54
32
|
end
|
55
|
-
|
56
|
-
# view all selfs likers by model
|
57
|
-
#
|
58
|
-
# Example:
|
59
|
-
# => @photo.all_likers_by_model
|
60
|
-
# => [@john]
|
61
|
-
def all_likers_by_model(model)
|
62
|
-
get_likers_of(self, model)
|
63
|
-
end
|
64
|
-
|
65
|
-
# view all common likers of self against model
|
66
|
-
#
|
67
|
-
# Example:
|
68
|
-
# => @photo.common_likers_with(@gang)
|
69
|
-
# => [@john, @jashua]
|
70
|
-
def common_likers_with(model)
|
71
|
-
model_likers = get_likers_of(model)
|
72
|
-
self_likers = get_likers_of(self)
|
73
|
-
|
74
|
-
self_likers & model_likers
|
75
|
-
end
|
76
|
-
|
77
|
-
private
|
78
|
-
def get_likers_of(me, model = nil)
|
79
|
-
likers = !model ? me.likers_assoc : me.likers_assoc.where(:like_type => model.to_s)
|
80
|
-
|
81
|
-
likers.collect do |like|
|
82
|
-
like.like_type.constantize.where(_id: like.like_id).first
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def method_missing(missing_method, *args, &block)
|
87
|
-
if missing_method.to_s =~ /^(.+)_likers_count$/
|
88
|
-
likers_count_by_model($1.camelize)
|
89
|
-
elsif missing_method.to_s =~ /^all_(.+)_likers$/
|
90
|
-
all_likers_by_model($1.camelize)
|
91
|
-
else
|
92
|
-
super
|
93
|
-
end
|
94
|
-
end
|
95
33
|
end
|
96
34
|
end
|
@@ -58,35 +58,25 @@ module Mongoid
|
|
58
58
|
# know if self is already liking model
|
59
59
|
#
|
60
60
|
# Example:
|
61
|
-
# => @john.
|
61
|
+
# => @john.liked?(@photos)
|
62
62
|
# => true
|
63
63
|
def liked?(model)
|
64
64
|
self.likes.where(likable_id: model.id, likable_type: model.class.to_s).exists?
|
65
65
|
end
|
66
66
|
|
67
|
-
# get likes count
|
68
|
-
# Note: this is a cache counter
|
69
|
-
#
|
70
|
-
# Example:
|
71
|
-
# => @john.likes_count
|
72
|
-
# => 1
|
73
|
-
# def likes_count
|
74
|
-
# self.likes_count_field
|
75
|
-
# end
|
76
|
-
|
77
67
|
# get likes count by model
|
78
68
|
#
|
79
69
|
# Example:
|
80
|
-
# => @john.
|
70
|
+
# => @john.likes_count_by_model("Photo")
|
81
71
|
# => 1
|
82
72
|
def likes_count_by_model(model)
|
83
|
-
self.
|
73
|
+
self.likes.where(:likable_type => model).count
|
84
74
|
end
|
85
75
|
|
86
76
|
# view all selfs likes
|
87
77
|
#
|
88
78
|
# Example:
|
89
|
-
# => @john.
|
79
|
+
# => @john.liked_objects
|
90
80
|
# => [@photo]
|
91
81
|
def liked_objects
|
92
82
|
get_liked_objects_of_kind
|
@@ -95,12 +85,12 @@ module Mongoid
|
|
95
85
|
# view all selfs likes by model
|
96
86
|
#
|
97
87
|
# Example:
|
98
|
-
# => @john.
|
88
|
+
# => @john.get_liked_objects_of_kind('Photo')
|
99
89
|
# => [@photo]
|
100
90
|
def get_liked_objects_of_kind(model = nil)
|
101
91
|
if model
|
102
|
-
user_likes = likes.where(likable_type: model
|
103
|
-
extract_likes_from(user_likes, model
|
92
|
+
user_likes = likes.where(likable_type: model)
|
93
|
+
extract_likes_from(user_likes, model)
|
104
94
|
else
|
105
95
|
likable_types = likes.map(&:likable_type).uniq
|
106
96
|
likable_types.collect do |likable_type|
|
@@ -116,26 +106,13 @@ module Mongoid
|
|
116
106
|
likable_type.constantize.find(likable_ids)
|
117
107
|
end
|
118
108
|
|
119
|
-
# view all common likes of self against model
|
120
|
-
#
|
121
|
-
# Example:
|
122
|
-
# => @john.common_likes_with(@jashua)
|
123
|
-
# => [@photo1, @photo2]
|
124
|
-
def common_likes_with(model)
|
125
|
-
model_likes = get_likes_of(model)
|
126
|
-
self_likes = get_likes_of(self)
|
127
|
-
|
128
|
-
self_likes & model_likes
|
129
|
-
end
|
130
|
-
|
131
109
|
private
|
132
110
|
|
133
|
-
|
134
111
|
def method_missing(missing_method, *args, &block)
|
135
112
|
if missing_method.to_s =~ /^(.+)_likes_count$/
|
136
113
|
likes_count_by_model($1.camelize)
|
137
|
-
elsif missing_method.to_s =~ /^
|
138
|
-
|
114
|
+
elsif missing_method.to_s =~ /^liked_(.+)$/
|
115
|
+
get_liked_objects_of_kind($1.singularize.camelize)
|
139
116
|
else
|
140
117
|
super
|
141
118
|
end
|
@@ -87,5 +87,26 @@ describe Mongoid::Liker do
|
|
87
87
|
@jashua.liked_objects.should_not include @photo1
|
88
88
|
end
|
89
89
|
end
|
90
|
+
|
91
|
+
describe "get likes by model" do
|
92
|
+
before :each do
|
93
|
+
@photo1 = Photo.create
|
94
|
+
@photo2 = Photo.create
|
95
|
+
@album1 = Album.create
|
96
|
+
@album2 = Album.create
|
97
|
+
@john.like(@photo1)
|
98
|
+
@john.like(@photo2)
|
99
|
+
@john.like(@album1)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should return photo likes count" do
|
103
|
+
@john.photo_likes_count.should == 2
|
104
|
+
@john.album_likes_count.should == 1
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should return liked photos " do
|
108
|
+
@john.liked_photos.should == [@photo1, @photo2]
|
109
|
+
end
|
110
|
+
end
|
90
111
|
end
|
91
112
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_socializer_actions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2013-02-08 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152294160 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152294160
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activesupport
|
27
|
-
requirement: &
|
27
|
+
requirement: &2152293620 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '3.2'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2152293620
|
36
36
|
description: Add liking, commentable, sharabel, tagabel ability to Mongoid documents.
|
37
37
|
email:
|
38
38
|
- sreehari@activesphere.com
|
@@ -54,6 +54,7 @@ files:
|
|
54
54
|
- lib/mongoid_socializer_actions/version.rb
|
55
55
|
- mongoid_socializer_actions.gemspec
|
56
56
|
- spec/config/mongoid.yml
|
57
|
+
- spec/mongoid/models/album.rb
|
57
58
|
- spec/mongoid/models/photo.rb
|
58
59
|
- spec/mongoid/models/user.rb
|
59
60
|
- spec/mongoid/mongoid_socializer_actions/likes_spec.rb
|
@@ -85,6 +86,7 @@ specification_version: 3
|
|
85
86
|
summary: Ability to comment, like, share, tag mongoid documents
|
86
87
|
test_files:
|
87
88
|
- spec/config/mongoid.yml
|
89
|
+
- spec/mongoid/models/album.rb
|
88
90
|
- spec/mongoid/models/photo.rb
|
89
91
|
- spec/mongoid/models/user.rb
|
90
92
|
- spec/mongoid/mongoid_socializer_actions/likes_spec.rb
|