mongoid_socializer_actions 0.1.0 → 1.0.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/app/models/comment.rb +15 -0
- data/app/models/like.rb +0 -3
- data/lib/mongoid_socializer_actions/commentable.rb +15 -0
- data/lib/mongoid_socializer_actions/commenter.rb +76 -0
- data/lib/mongoid_socializer_actions/comments_spec.rb +0 -0
- data/lib/mongoid_socializer_actions/version.rb +1 -1
- data/lib/mongoid_socializer_actions.rb +5 -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/comments_spec.rb +79 -0
- metadata +11 -5
@@ -0,0 +1,15 @@
|
|
1
|
+
module Mongoid
|
2
|
+
class Comment
|
3
|
+
include Mongoid::Document
|
4
|
+
include Mongoid::Timestamps
|
5
|
+
|
6
|
+
field :body, type: String
|
7
|
+
|
8
|
+
belongs_to :commenter, :class_name => 'User', :inverse_of => :comments
|
9
|
+
belongs_to :commentable, :polymorphic => true
|
10
|
+
|
11
|
+
def commentabled_obj
|
12
|
+
commentable_type.constantize.find(commentable_id)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/app/models/like.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Commentable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do |base|
|
6
|
+
base.field :comments_count, :type => Integer, :default => 0
|
7
|
+
base.has_many :comments, :class_name => 'Mongoid::Comment', :as => :commentable, :dependent => :destroy
|
8
|
+
end
|
9
|
+
|
10
|
+
def commenters
|
11
|
+
commenter_ids = comments.distinct('commenter_id')
|
12
|
+
User.find(commenter_ids)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Commenter
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do |base|
|
6
|
+
base.field :comments_count, :type => Integer, :default => 0
|
7
|
+
base.has_many :comments, :class_name => 'Mongoid::Comment', :inverse_of => :commenter, :dependent => :destroy
|
8
|
+
end
|
9
|
+
|
10
|
+
# Comment on a model
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
# => @john.comment_on(@photo, { with: "Beautiful" } )
|
14
|
+
def comment_on(model, comment_body)
|
15
|
+
model.before_commented_by(self) if model.respond_to?('before_commented_by')
|
16
|
+
comment = model.comments.create!(body: comment_body)
|
17
|
+
comments << comment
|
18
|
+
model.inc(:comments_count, 1)
|
19
|
+
self.inc(:comments_count, 1)
|
20
|
+
model.after_commented_by(self) if model.respond_to?('after_commented_by')
|
21
|
+
self.before_comment(model) if self.respond_to?('before_comment')
|
22
|
+
self.after_comment(model) if self.respond_to?('after_comment')
|
23
|
+
comment
|
24
|
+
end
|
25
|
+
|
26
|
+
# Delete comment a model
|
27
|
+
#
|
28
|
+
# Example:
|
29
|
+
# => @john.delete_comment(comment_id)
|
30
|
+
def delete_comment(model_id)
|
31
|
+
comment = Comment.where(id: model_id).first
|
32
|
+
if comment.present?
|
33
|
+
comment.commentabled_obj.inc(:comments_count, -1)
|
34
|
+
comment.destroy
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Edit comment a model
|
39
|
+
#
|
40
|
+
# Example:
|
41
|
+
# => @john.edit_comment(comment_id, comment_body)
|
42
|
+
def edit_comment(comment_id, comment_body)
|
43
|
+
comment = Comment.where(_id: comment_id).first
|
44
|
+
comment.update_attributes(body: comment_body) if comment.present?
|
45
|
+
end
|
46
|
+
|
47
|
+
def comments_of(model)
|
48
|
+
Comment.where(commentable_type: model.class, commentable_id: model.id)
|
49
|
+
end
|
50
|
+
|
51
|
+
# get comments count by model
|
52
|
+
#
|
53
|
+
# Example:
|
54
|
+
# => @john.comments_count_by_model("Photo")
|
55
|
+
# => 1
|
56
|
+
def comments_count_by_model(model)
|
57
|
+
self.comments.where(:likable_type => model).count
|
58
|
+
end
|
59
|
+
|
60
|
+
def get_comments_of_kind(model = nil)
|
61
|
+
model ? comments.where(likable_type: model) : comments
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def method_missing(missing_method, *args, &block)
|
67
|
+
if missing_method.to_s =~ /^(.+)_comments_count$/
|
68
|
+
comments_count_by_model($1.camelize)
|
69
|
+
elsif missing_method.to_s =~ /^(.+)_comments$/
|
70
|
+
get_comments_of_kind($1.singularize.camelize)
|
71
|
+
else
|
72
|
+
super
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
File without changes
|
@@ -1,4 +1,8 @@
|
|
1
1
|
require 'mongoid_socializer_actions/likeable'
|
2
2
|
require 'mongoid_socializer_actions/liker'
|
3
3
|
require 'mongoid_socializer_actions/helper'
|
4
|
-
require 'models/like'
|
4
|
+
require 'models/like'
|
5
|
+
|
6
|
+
require 'mongoid_socializer_actions/commentable'
|
7
|
+
require 'mongoid_socializer_actions/commenter'
|
8
|
+
require 'models/comment'
|
data/spec/mongoid/models/user.rb
CHANGED
@@ -0,0 +1,79 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Mongoid::Commenter 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
|
+
describe Photo do
|
11
|
+
before :each do
|
12
|
+
@photo1 = Photo.create
|
13
|
+
@photo2 = Photo.create
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should have no comments" do
|
17
|
+
[@photo1, @photo2].each { |t| t.comments.should be_empty }
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be commentable" do
|
21
|
+
expect{ @john.comment_on(@photo1, "Beautiful") }.to change(Mongoid::Comment, :count).by(1)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should be commented by commenter" do
|
25
|
+
@john.comment_on(@photo1, 'Beautiful')
|
26
|
+
@photo1.commenters.should include @john
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should not be commented by others" do
|
30
|
+
@photo1.commenters.should_not include @john
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should be commentable by multiple likers" do
|
34
|
+
expect do
|
35
|
+
@jashua.comment_on(@photo1, 'Beautiful')
|
36
|
+
@john.comment_on(@photo1, 'Not Beautiful')
|
37
|
+
end.to change { Mongoid::Comment.count }.by(2)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should be increase comments_count" do
|
41
|
+
expect{ @jashua.comment_on(@photo1, 'Beautiful') }.to change(@photo1, :comments_count).by(1)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should be increase comments_count" do
|
45
|
+
expect{ @jashua.comment_on(@photo1, 'Beautiful') }.to change(@jashua, :comments_count).by(1)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should have the correct comments count" do
|
49
|
+
@jashua.comment_on(@photo1, '')
|
50
|
+
@john.comment_on(@photo1, '')
|
51
|
+
@photo1.comments_count.should be 2
|
52
|
+
@jashua.comments_count.should be 1
|
53
|
+
@john.comments_count.should be 1
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should be unlikable" do
|
57
|
+
comment = @jashua.comment_on(@photo1, 'Beautiful')
|
58
|
+
expect{ @jashua.delete_comment(comment) }.to change { Mongoid::Comment.count }.by(-1)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "edit comment" do
|
62
|
+
comment = @jashua.comment_on(@photo1, 'Beautiful')
|
63
|
+
expect{ @jashua.edit_comment(comment, 'Not Beautiful') }.to change { comment.reload.body }.from('Beautiful').to('Not Beautiful')
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "get comment by model" do
|
68
|
+
before :each do
|
69
|
+
@photo1 = Photo.create
|
70
|
+
@c1 = @john.comment_on(@photo1, "Beautiful")
|
71
|
+
@c2 = @john.comment_on(@photo1, "Excellemt")
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should return photo comments" do
|
75
|
+
@john.comments_of(@photo1).should include @c1, @c2
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
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:
|
4
|
+
version: 1.0.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: &2152550520 !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: *2152550520
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activesupport
|
27
|
-
requirement: &
|
27
|
+
requirement: &2152549980 !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: *2152549980
|
36
36
|
description: Add liking, commentable, sharabel, tagabel ability to Mongoid documents.
|
37
37
|
email:
|
38
38
|
- sreehari@activesphere.com
|
@@ -46,8 +46,12 @@ files:
|
|
46
46
|
- Gemfile
|
47
47
|
- Rakefile
|
48
48
|
- Readme.md
|
49
|
+
- app/models/comment.rb
|
49
50
|
- app/models/like.rb
|
50
51
|
- lib/mongoid_socializer_actions.rb
|
52
|
+
- lib/mongoid_socializer_actions/commentable.rb
|
53
|
+
- lib/mongoid_socializer_actions/commenter.rb
|
54
|
+
- lib/mongoid_socializer_actions/comments_spec.rb
|
51
55
|
- lib/mongoid_socializer_actions/helper.rb
|
52
56
|
- lib/mongoid_socializer_actions/likeable.rb
|
53
57
|
- lib/mongoid_socializer_actions/liker.rb
|
@@ -57,6 +61,7 @@ files:
|
|
57
61
|
- spec/mongoid/models/album.rb
|
58
62
|
- spec/mongoid/models/photo.rb
|
59
63
|
- spec/mongoid/models/user.rb
|
64
|
+
- spec/mongoid/mongoid_socializer_actions/comments_spec.rb
|
60
65
|
- spec/mongoid/mongoid_socializer_actions/likes_spec.rb
|
61
66
|
- spec/spec_helper.rb
|
62
67
|
homepage: https://github.com/sreehari/mongoid_socializer_actions
|
@@ -89,5 +94,6 @@ test_files:
|
|
89
94
|
- spec/mongoid/models/album.rb
|
90
95
|
- spec/mongoid/models/photo.rb
|
91
96
|
- spec/mongoid/models/user.rb
|
97
|
+
- spec/mongoid/mongoid_socializer_actions/comments_spec.rb
|
92
98
|
- spec/mongoid/mongoid_socializer_actions/likes_spec.rb
|
93
99
|
- spec/spec_helper.rb
|