mongoid_socializer_actions 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -125,8 +125,42 @@ You can query for comments like that:
125
125
  photo.commenters
126
126
  # => [user]
127
127
 
128
+ ## SHARES
129
+ Add the Sharer module in User model and Sharable in Photo, Album etc.
130
+
131
+ class User
132
+ include Mongoid::Document
133
+
134
+ include Mongoid::Sharer
135
+ end
136
+
137
+ class Photo
138
+ include Mongoid::Document
139
+
140
+ include Mongoid::Sharable
141
+ end
142
+
143
+ You can now comment objects like this:
144
+
145
+ user = User.create
146
+ photo = Photo.create
147
+
148
+ user.share(photo)
149
+
150
+ You can query for comments like that:
151
+
152
+ photo.sharers
153
+ # => [user]
154
+
155
+ user.unshare(photo)
156
+ # => true
157
+
158
+ user.photo_shares_count
159
+ # => 1
160
+
161
+ photo.shares_count
162
+ # => 1
128
163
 
129
164
  # TODOs
130
165
 
131
- - Implement sharable
132
166
  - Implement taggable
@@ -0,0 +1,8 @@
1
+ module Mongoid
2
+ class Share
3
+ include Mongoid::Document
4
+
5
+ belongs_to :sharer, :class_name => 'User', :inverse_of => :shares
6
+ belongs_to :sharable, :polymorphic => true
7
+ end
8
+ end
@@ -4,7 +4,6 @@ module Mongoid
4
4
 
5
5
  included do |base|
6
6
  base.field :likers_count, :type => Integer, :default => 0
7
- base.field :liker_ids, :type => Array, :default => []
8
7
  base.has_many :likes, :class_name => 'Mongoid::Like', :as => :likable, :dependent => :destroy
9
8
  base.has_and_belongs_to_many :likers, :class_name => 'User', :inverse_of => nil
10
9
  end
@@ -0,0 +1,15 @@
1
+ module Mongoid
2
+ module Sharable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do |base|
6
+ base.field :shares_count, :type => Integer, :default => 0
7
+ base.has_many :shares, :class_name => 'Mongoid::Share', :as => :sharable, :dependent => :destroy
8
+ end
9
+
10
+ def sharers
11
+ sharer_ids = shares.distinct('sharer_id')
12
+ User.find(sharer_ids)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,107 @@
1
+ module Mongoid
2
+ module Sharer
3
+ extend ActiveSupport::Concern
4
+
5
+ included do |base|
6
+ base.has_many :shares, :class_name => 'Mongoid::Share', :inverse_of => :sharer, :dependent => :destroy
7
+ end
8
+
9
+ # share a model
10
+ #
11
+ # Example:
12
+ # => @john.share(@photo)
13
+ def share(model)
14
+ model.before_shared_by(self) if model.respond_to?('before_shared_by')
15
+ shares << model.shares.create!
16
+ model.inc(:shares_count, 1)
17
+ model.after_shared_by(self) if model.respond_to?('after_shared_by')
18
+ self.before_share(model) if self.respond_to?('before_share')
19
+ self.after_share(model) if self.respond_to?('after_share')
20
+ end
21
+
22
+ # unshare a model
23
+ #
24
+ # Example:
25
+ # => @john.unshare(@photo)
26
+ def unshare(model)
27
+ if shared?(model)
28
+ model.reload
29
+ self.reload
30
+
31
+ shares.where(:sharable_type => model.class.name, :sharable_id => model.id).destroy
32
+ model.inc(:shares_count, -1)
33
+
34
+ model.before_unshare_by(self) if model.respond_to?('before_unshare_by')
35
+ model.after_unshare_by(self) if model.respond_to?('after_unshare_by')
36
+ self.before_unshare(model) if self.respond_to?('before_unshare')
37
+ self.after_unshare(model) if self.respond_to?('after_unshare')
38
+ return true
39
+ else
40
+ return false
41
+ end
42
+ end
43
+
44
+ # know if user is already shared model
45
+ #
46
+ # Example:
47
+ # => @john.shared?(@photo)
48
+ # => true
49
+ def shared?(model)
50
+ shares.where(sharable_type: model.class.name, sharable_id: model.id).exists?
51
+ end
52
+
53
+ # get likes count by model
54
+ #
55
+ # Example:
56
+ # => @john.shares_count_by_model("Photo")
57
+ # => 1
58
+ def shares_count_by_model(model)
59
+ self.shares.where(:sharable_type => model).count
60
+ end
61
+
62
+ # view all selfs shares
63
+ #
64
+ # Example:
65
+ # => @john.liked_objects
66
+ # => [@photo]
67
+ def shared_objects
68
+ get_shared_objects_of_kind
69
+ end
70
+
71
+ # view all selfs likes by model
72
+ #
73
+ # Example:
74
+ # => @john.get_shared_objects_of_kind('Photo')
75
+ # => [@photo]
76
+ def get_shared_objects_of_kind(model = nil)
77
+ if model
78
+ user_shares = shares.where(sharable_type: model)
79
+ extract_shares_from(user_shares, model)
80
+ else
81
+ sharable_types = shares.map(&:sharable_type).uniq
82
+ sharable_types.collect do |sharable_type|
83
+ user_shares = shares.select{ |share| share.sharable_type == sharable_type }
84
+ extract_shares_from(user_likes, likable_type)
85
+ end.flatten
86
+ end
87
+ end
88
+
89
+ def extract_shares_from(user_shares, sharable_type)
90
+ return [] unless user_shares.present?
91
+ sharable_ids = user_shares.map(&:sharable_id)
92
+ sharable_type.constantize.find(sharable_ids)
93
+ end
94
+
95
+ private
96
+
97
+ def method_missing(missing_method, *args, &block)
98
+ if missing_method.to_s =~ /^(.+)_shares_count$/
99
+ shares_count_by_model($1.camelize)
100
+ elsif missing_method.to_s =~ /^shared_(.+)$/
101
+ get_shared_objects_of_kind($1.singularize.camelize)
102
+ else
103
+ super
104
+ end
105
+ end
106
+ end
107
+ end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module MongoidSocializerActions
3
- VERSION = "1.1.0"
3
+ VERSION = "1.2.0"
4
4
  end
5
5
  end
@@ -5,4 +5,8 @@ require 'models/like'
5
5
 
6
6
  require 'mongoid_socializer_actions/commentable'
7
7
  require 'mongoid_socializer_actions/commenter'
8
- require 'models/comment'
8
+ require 'models/comment'
9
+
10
+ require 'mongoid_socializer_actions/sharable'
11
+ require 'mongoid_socializer_actions/sharer'
12
+ require 'models/share'
@@ -2,4 +2,5 @@ class Album
2
2
  include Mongoid::Document
3
3
  include Mongoid::Likeable
4
4
  include Mongoid::Commentable
5
+ include Mongoid::Sharable
5
6
  end
@@ -2,4 +2,5 @@ class Photo
2
2
  include Mongoid::Document
3
3
  include Mongoid::Likeable
4
4
  include Mongoid::Commentable
5
+ include Mongoid::Sharable
5
6
  end
@@ -2,6 +2,7 @@ class User
2
2
  include Mongoid::Document
3
3
  include Mongoid::Liker
4
4
  include Mongoid::Commenter
5
+ include Mongoid::Sharer
5
6
 
6
7
  field :name, type: String
7
8
  end
@@ -0,0 +1,103 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Mongoid::Sharer 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 shares" do
11
+ [@john, @jashua].each {|u| u.shared_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 shares" do
21
+ [@photo1, @photo2].each { |p| p.shares.should be_empty }
22
+ end
23
+
24
+ it "should be sharable" do
25
+ expect{ @john.share(@photo1) }.to change(Mongoid::Share, :count).by(1)
26
+ end
27
+
28
+ it "should be shared by sharer" do
29
+ @john.share(@photo1)
30
+ @john.shared?(@photo1).should be_true
31
+ end
32
+
33
+ it "should be unshared by sharer" do
34
+ @john.share(@photo1)
35
+ @john.unshare(@photo1)
36
+ @john.shared?(@photo1).should be_false
37
+ end
38
+
39
+ it "should have the liker as sharer" do
40
+ @john.share(@photo1)
41
+ @photo1.sharers.should include @john
42
+ end
43
+
44
+ it "should not have others as sharer" do
45
+ @photo1.sharers.should_not include @jashua
46
+ end
47
+
48
+ it "should be increase likes_count" do
49
+ expect{ @jashua.share(@photo1) }.to change(@photo1, :shares_count).by(1)
50
+ end
51
+
52
+ it "should be shared by multiple sharers" do
53
+ @jashua.share(@photo1)
54
+ @john.share(@photo1)
55
+ @photo1.sharers.should include @john, @jashua
56
+ end
57
+
58
+ it "should have the correct sharers count" do
59
+ @jashua.share(@photo1)
60
+ @john.share(@photo1)
61
+ @photo1.shares_count.should be 2
62
+ end
63
+
64
+ it "should be unsharable" do
65
+ @jashua.share(@photo1)
66
+ @jashua.unshare(@photo1).should be_true
67
+ end
68
+
69
+ it "should not be unsharable by shere" do
70
+ @jashua.unshare(@photo1).should be_false
71
+ end
72
+
73
+ it "should not include former liker" do
74
+ @photo1.sharers.should_not include @jashua
75
+ end
76
+
77
+ it "should not be included in former likes" do
78
+ @jashua.shared_objects.should_not include @photo1
79
+ end
80
+ end
81
+
82
+ describe "get likes by model" do
83
+ before :each do
84
+ @photo1 = Photo.create
85
+ @photo2 = Photo.create
86
+ @album1 = Album.create
87
+ @album2 = Album.create
88
+ @john.share(@photo1)
89
+ @john.share(@photo2)
90
+ @john.share(@album1)
91
+ end
92
+
93
+ it "should return photo likes count" do
94
+ @john.photo_shares_count.should == 2
95
+ @john.album_shares_count.should == 1
96
+ end
97
+
98
+ it "should return liked photos " do
99
+ @john.shared_photos.should == [@photo1, @photo2]
100
+ end
101
+ end
102
+ end
103
+ end
data/spec/spec_helper.rb CHANGED
@@ -11,7 +11,7 @@ $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
13
 
14
- Mongoid.load! '/Users/sreehari/MyProjects/mongoid_likes/spec/config/mongoid.yml', :test
14
+ Mongoid.load! '/Users/sreehari/MyProjects/mongoid_socializer_actions/spec/config/mongoid.yml', :test
15
15
 
16
16
  require 'mongoid_socializer_actions'
17
17
  require 'rspec'
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.1.0
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2013-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
16
- requirement: &2156352400 !ruby/object:Gem::Requirement
16
+ requirement: &2160400880 !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: *2156352400
24
+ version_requirements: *2160400880
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &2156351880 !ruby/object:Gem::Requirement
27
+ requirement: &2160398860 !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: *2156351880
35
+ version_requirements: *2160398860
36
36
  description: Add liking, commentable, sharabel, tagabel ability to Mongoid documents.
37
37
  email:
38
38
  - sreehari@activesphere.com
@@ -48,12 +48,15 @@ files:
48
48
  - Readme.md
49
49
  - app/models/comment.rb
50
50
  - app/models/like.rb
51
+ - app/models/share.rb
51
52
  - lib/mongoid_socializer_actions.rb
52
53
  - lib/mongoid_socializer_actions/commentable.rb
53
54
  - lib/mongoid_socializer_actions/commenter.rb
54
55
  - lib/mongoid_socializer_actions/helper.rb
55
56
  - lib/mongoid_socializer_actions/likeable.rb
56
57
  - lib/mongoid_socializer_actions/liker.rb
58
+ - lib/mongoid_socializer_actions/sharable.rb
59
+ - lib/mongoid_socializer_actions/sharer.rb
57
60
  - lib/mongoid_socializer_actions/version.rb
58
61
  - mongoid_socializer_actions.gemspec
59
62
  - spec/config/mongoid.yml
@@ -62,6 +65,7 @@ files:
62
65
  - spec/mongoid/models/user.rb
63
66
  - spec/mongoid/mongoid_socializer_actions/comments_spec.rb
64
67
  - spec/mongoid/mongoid_socializer_actions/likes_spec.rb
68
+ - spec/mongoid/mongoid_socializer_actions/shares_spec.rb
65
69
  - spec/spec_helper.rb
66
70
  homepage: https://github.com/sreehari/mongoid_socializer_actions
67
71
  licenses: []
@@ -95,4 +99,5 @@ test_files:
95
99
  - spec/mongoid/models/user.rb
96
100
  - spec/mongoid/mongoid_socializer_actions/comments_spec.rb
97
101
  - spec/mongoid/mongoid_socializer_actions/likes_spec.rb
102
+ - spec/mongoid/mongoid_socializer_actions/shares_spec.rb
98
103
  - spec/spec_helper.rb