mongoid_socializer_actions 1.0.0 → 1.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 CHANGED
@@ -25,7 +25,8 @@ This gem has been tested with [MongoID](http://mongoid.org/) version 3.0.21
25
25
 
26
26
  ## Usage
27
27
 
28
- Mongoid Likes provides two modules that you can mix in your model objects like that:
28
+ ##LIKES
29
+ Add the liker module in User model and likable in Photo, Album etc.
29
30
 
30
31
  class User
31
32
  include Mongoid::Document
@@ -39,13 +40,15 @@ Mongoid Likes provides two modules that you can mix in your model objects like t
39
40
  include Mongoid::Likeable
40
41
  end
41
42
 
42
- You can now like objects like this:
43
+ You can now like, unlike objects like this:
43
44
 
44
45
  user = User.create
45
46
  photo = Photo.create
46
47
 
47
48
  user.like(photo)
48
49
 
50
+ user.unlike(photo)
51
+
49
52
  You can query for likes like that:
50
53
 
51
54
  photo.likers
@@ -70,10 +73,60 @@ Also likes are polymorphic, so let's assume you have a second class `Album` that
70
73
  user.album_likes_count
71
74
  # => 1
72
75
 
73
- You get the idea. Have a look at the specs to see some more examples.
76
+ ## COMMENTS
77
+ Add the Commenter module in User model and Commentable in Photo, Album etc.
78
+
79
+ class User
80
+ include Mongoid::Document
81
+
82
+ include Mongoid::Commenter
83
+ end
84
+
85
+ class Photo
86
+ include Mongoid::Document
87
+
88
+ include Mongoid::Commentable
89
+ end
90
+
91
+ You can now comment objects like this:
92
+
93
+ user = User.create
94
+ photo = Photo.create
95
+
96
+ user.comment_on(photo, "Beautiful")
97
+
98
+ Load the commets with eager loaded user(Enable [Identity map](http://mongoid.org/en/mongoid/docs/identity_map.html))
99
+
100
+ photo.comments_with_eager_loaded_commenter
101
+
102
+ You can query for comments like that:
103
+
104
+ photo.comments
105
+ # => [comment]
106
+
107
+ photo.commenters
108
+ # => [user]
109
+
110
+ user.delete_comment(comment_id)
111
+ # => true
112
+
113
+ user.edit_comment(comment_id, "Not Beautiful")
114
+ # => true
115
+
116
+ user.comments_of(photo)
117
+ # => [comment]
118
+
119
+ user.photo_comments_count
120
+ # => 1
121
+
122
+ photo.comments_count
123
+ # => 1
124
+
125
+ photo.commenters
126
+ # => [user]
127
+
74
128
 
75
129
  # TODOs
76
130
 
77
- - Implement commentable
78
131
  - Implement sharable
79
132
  - Implement taggable
@@ -7,9 +7,23 @@ module Mongoid
7
7
  base.has_many :comments, :class_name => 'Mongoid::Comment', :as => :commentable, :dependent => :destroy
8
8
  end
9
9
 
10
+ # get commenters
11
+ #
12
+ # Example:
13
+ # => @photo.commenters
14
+ # => [@sree, @hari]
10
15
  def commenters
11
16
  commenter_ids = comments.distinct('commenter_id')
12
17
  User.find(commenter_ids)
13
18
  end
19
+
20
+ # Load comments with commenter
21
+ #
22
+ # Example:
23
+ # => @photo.comments_with_eager_loaded_commenter
24
+ # => [@sree, @hari]
25
+ def comments_with_eager_loaded_commenter
26
+ comments.includes(:commenter)
27
+ end
14
28
  end
15
29
  end
@@ -4,11 +4,9 @@ module Mongoid
4
4
 
5
5
  included do |base|
6
6
  base.field :likers_count, :type => Integer, :default => 0
7
- base.has_many :likes, :class_name => 'Mongoid::Like', :as => :likable, :dependent => :destroy do
8
- def liked_by?(model_id)
9
- where(liker_id: model_id).exists?
10
- end
11
- end
7
+ base.field :liker_ids, :type => Array, :default => []
8
+ base.has_many :likes, :class_name => 'Mongoid::Like', :as => :likable, :dependent => :destroy
9
+ base.has_and_belongs_to_many :likers, :class_name => 'User', :inverse_of => nil
12
10
  end
13
11
 
14
12
  # know if self is liked by model
@@ -17,18 +15,8 @@ module Mongoid
17
15
  # => @photo.liked_by?(@john)
18
16
  # => true
19
17
 
20
- def liked_by?(model)
21
- self.likes.liked_by?(model.id)
22
- end
23
-
24
- # view all selfs likers
25
- #
26
- # Example:
27
- # => @photo.likers
28
- # => [@john, @jashua]
29
- def likers
30
- ids = likes.collect{ |like| like.liker_id }
31
- ids.present? ? User.find(ids) : []
18
+ def liked_by?(liker)
19
+ liker_ids.include?(liker.id)
32
20
  end
33
21
  end
34
22
  end
@@ -15,11 +15,10 @@ module Mongoid
15
15
  unless self.liked?(model)
16
16
  model.before_liked_by(self) if model.respond_to?('before_liked_by')
17
17
  likes << model.likes.create!
18
+ model.likers << self
18
19
  model.inc(:likers_count, 1)
19
20
  model.after_liked_by(self) if model.respond_to?('after_liked_by')
20
-
21
21
  self.before_like(model) if self.respond_to?('before_like')
22
- # self.likes_assoc.create!(:like_type => model.class.name, :like_id => model.id)
23
22
  self.inc(:likes_count, 1)
24
23
  self.after_like(model) if self.respond_to?('after_like')
25
24
  return true
@@ -40,12 +39,14 @@ module Mongoid
40
39
  self.reload
41
40
 
42
41
  model.before_unliked_by(self) if model.respond_to?('before_unliked_by')
42
+
43
43
  likes.where(:likable_type => model.class.name, :likable_id => model.id).destroy
44
+ model.likers.delete(self)
45
+
44
46
  model.inc(:likers_count, -1)
45
47
  model.after_unliked_by(self) if model.respond_to?('after_unliked_by')
46
-
47
48
  self.before_unlike(model) if self.respond_to?('before_unlike')
48
- # self.likes_assoc.where(:like_type => model.class.name, :like_id => model.id).destroy
49
+
49
50
  self.inc(:likes_count, -1)
50
51
  self.after_unlike(model) if self.respond_to?('after_unlike')
51
52
 
@@ -55,13 +56,13 @@ module Mongoid
55
56
  end
56
57
  end
57
58
 
58
- # know if self is already liking model
59
+ # know if user is already liking model
59
60
  #
60
61
  # Example:
61
62
  # => @john.liked?(@photos)
62
63
  # => true
63
64
  def liked?(model)
64
- self.likes.where(likable_id: model.id, likable_type: model.class.to_s).exists?
65
+ model.liker_ids.include?(self.id)
65
66
  end
66
67
 
67
68
  # get likes count by model
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module MongoidSocializerActions
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -53,7 +53,7 @@ describe Mongoid::Commenter do
53
53
  @john.comments_count.should be 1
54
54
  end
55
55
 
56
- it "should be unlikable" do
56
+ it "should be uncommentable" do
57
57
  comment = @jashua.comment_on(@photo1, 'Beautiful')
58
58
  expect{ @jashua.delete_comment(comment) }.to change { Mongoid::Comment.count }.by(-1)
59
59
  end
@@ -68,12 +68,16 @@ describe Mongoid::Commenter do
68
68
  before :each do
69
69
  @photo1 = Photo.create
70
70
  @c1 = @john.comment_on(@photo1, "Beautiful")
71
- @c2 = @john.comment_on(@photo1, "Excellemt")
71
+ @c2 = @jashua.comment_on(@photo1, "Excellemt")
72
72
  end
73
73
 
74
74
  it "should return photo comments" do
75
75
  @john.comments_of(@photo1).should include @c1, @c2
76
76
  end
77
+
78
+ it "return comments with eager loaded commenters" do
79
+ @photo1.comments_with_eager_loaded_commenter.should include @c1, @c2
80
+ end
77
81
  end
78
82
  end
79
83
  end
@@ -27,7 +27,15 @@ describe Mongoid::Liker do
27
27
 
28
28
  it "should be liked by liker" do
29
29
  @john.like(@photo1)
30
- @photo1.liked_by?(@john).should be_true
30
+ @photo1.reload.liked_by?(@john).should be_true
31
+ @john.liked?(@photo1).should be_true
32
+ end
33
+
34
+ it "should be unliked by liker" do
35
+ @john.like(@photo1)
36
+ @john.unlike(@photo1)
37
+ @photo1.reload.liked_by?(@john).should be_false
38
+ @john.liked?(@photo1).should be_false
31
39
  end
32
40
 
33
41
  it "should not be liked by others" do
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: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-08 00:00:00.000000000 Z
12
+ date: 2013-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
16
- requirement: &2152550520 !ruby/object:Gem::Requirement
16
+ requirement: &2156352400 !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: *2152550520
24
+ version_requirements: *2156352400
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &2152549980 !ruby/object:Gem::Requirement
27
+ requirement: &2156351880 !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: *2152549980
35
+ version_requirements: *2156351880
36
36
  description: Add liking, commentable, sharabel, tagabel ability to Mongoid documents.
37
37
  email:
38
38
  - sreehari@activesphere.com
@@ -51,7 +51,6 @@ files:
51
51
  - lib/mongoid_socializer_actions.rb
52
52
  - lib/mongoid_socializer_actions/commentable.rb
53
53
  - lib/mongoid_socializer_actions/commenter.rb
54
- - lib/mongoid_socializer_actions/comments_spec.rb
55
54
  - lib/mongoid_socializer_actions/helper.rb
56
55
  - lib/mongoid_socializer_actions/likeable.rb
57
56
  - lib/mongoid_socializer_actions/liker.rb
File without changes