mongo_likeable 0.0.2 → 0.0.3

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.
@@ -23,7 +23,8 @@ After this,
23
23
  u = User.new
24
24
  u.like?(some_stuff)
25
25
 
26
- u.like(some_stuff)
26
+ u.like(some_stuff) # make sure you do this check before like/dislike
27
+ # cuz like and dislike the same thing at the same time seems strange
27
28
  u.like(*some_array_of_stuffs) # like an array of stuffs
28
29
 
29
30
  u.dislike(some_other_stuff)
@@ -34,11 +35,17 @@ After this,
34
35
  u.like_counts # display total counts
35
36
  u.dislike_counts
36
37
 
38
+ u.all_likers # display all objects who like/dislike u
39
+ u.all_dislikers
40
+
41
+ u.like_history # display like/dislike histories of u
42
+ u.dislike_history
43
+
37
44
  * Any bug or issue, please send me an email: ustc.flyingfox@gmail.com
38
45
 
39
46
  == TODO
40
47
 
41
- * like/dislike history.
48
+ * like/dislike history.(DONE)
42
49
 
43
50
  == Copyright
44
51
 
@@ -7,27 +7,45 @@ module Mongo
7
7
  base.field :likes, :type => Array, :default => []
8
8
  base.field :dislikes, :type => Array, :default => []
9
9
 
10
- # base.field :like_history, :type => Array, :default => []
11
- # base.field :dislike_history, :type => Array, :default => []
10
+ base.field :likers, :type => Array, :default => []
11
+ base.field :dislikers, :type => Array, :default => []
12
+
13
+ base.field :like_histories, :type => Array, :default => []
14
+ base.field :dislike_histories, :type => Array, :default => []
12
15
  elsif defined?(MongoMapper)
13
16
  base.key :likes, :type => Array, :default => []
14
17
  base.key :dislikes, :type => Array, :default => []
15
18
 
16
- # base.key :like_history, :type => Array, :default => []
17
- # base.key :dislike_history, :type => Array, :default => []
19
+ base.key :likers, :type => Array, :default => []
20
+ base.key :dislikers, :type => Array, :default => []
21
+
22
+ base.key :like_histories, :type => Array, :default => []
23
+ base.key :dislike_histories, :type => Array, :default => []
18
24
  end
19
25
  end
20
26
 
21
27
  def like(*models)
22
28
  self.likes |= simplify_instance(*models)
23
- # self.like_history |= simplify_instance(*models)
29
+ self.like_histories |= simplify_instance(*models)
30
+
31
+ models.each do |model|
32
+ model.likers |= simplify_instance(self)
33
+
34
+ model.save
35
+ end
24
36
 
25
37
  self.save
26
38
  end
27
39
 
28
40
  def dislike(*models)
29
41
  self.dislikes |= simplify_instance(*models)
30
- # self.dislike_history |= simplify_instance(*models)
42
+ self.dislike_histories |= simplify_instance(*models)
43
+
44
+ models.each do |model|
45
+ model.likers |= simplify_instance(self)
46
+
47
+ model.save
48
+ end
31
49
 
32
50
  self.save
33
51
  end
@@ -40,13 +58,21 @@ module Mongo
40
58
  self.dislikes.length
41
59
  end
42
60
 
43
- # def like_history
44
- # rebuild_instance(*self.like_history)
45
- # end
46
- #
47
- # def dislike_history
48
- # rebuild_instance(*self.dislike_history)
49
- # end
61
+ def all_likers
62
+ rebuild_instance(*self.likers)
63
+ end
64
+
65
+ def all_dislikers
66
+ rebuild_instance(*self.dislikers)
67
+ end
68
+
69
+ def like_history
70
+ rebuild_instance(*self.like_histories)
71
+ end
72
+
73
+ def dislike_history
74
+ rebuild_instance(*self.dislike_histories)
75
+ end
50
76
 
51
77
  def like?(model)
52
78
  self.likes.include? simplify_instance(model)[0]
@@ -61,6 +87,12 @@ module Mongo
61
87
  self.likes = []
62
88
  else
63
89
  self.likes -= simplify_instance(*models)
90
+
91
+ models.each do |model|
92
+ model.likers -= simplify_instance(self)
93
+
94
+ model.save
95
+ end
64
96
  end
65
97
 
66
98
  self.save
@@ -71,6 +103,12 @@ module Mongo
71
103
  self.dislikes = []
72
104
  else
73
105
  self.dislikes -= simplify_instance(*models)
106
+
107
+ models.each do |model|
108
+ model.likers -= simplify_instance(self)
109
+
110
+ model.save
111
+ end
74
112
  end
75
113
 
76
114
  self.save
@@ -1,3 +1,3 @@
1
1
  module MongoLikeable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -29,23 +29,30 @@ describe Mongo::Likeable do
29
29
  u.dislike?(@s).should be_false
30
30
 
31
31
  u.like(@v, @s, @t)
32
+ @v.all_likers.should == [u]
33
+ @s.all_likers.should == [u]
34
+ @t.all_likers.should == [u]
32
35
  u.like_counts.should == 3
33
36
  u.likes.should =~ [@v.class.name + '_' + @v.id.to_s, @s.class.name + '_' + @s.id.to_s, @t.class.name + '_' + @t.id.to_s]
34
- # u.like_history.should =~ [@v.class.name + '_' + @v.id.to_s, @s.class.name + '_' + @s.id.to_s, @t.class.name + '_' + @t.id.to_s]
37
+ u.like_history.should =~ [@v, @s, @t]
38
+
39
+ @v.like(@s)
40
+ @s.all_likers.should =~ [@v, u]
35
41
 
36
42
  u.dislike(@v, @s, @t)
37
43
  u.dislikes.should =~ [@v.class.name + '_' + @v.id.to_s, @s.class.name + '_' + @s.id.to_s, @t.class.name + '_' + @t.id.to_s]
38
- # u.dislike_history.should =~ [@v.class.name + '_' + @v.id.to_s, @s.class.name + '_' + @s.id.to_s, @t.class.name + '_' + @t.id.to_s]
44
+ u.dislike_history.should =~ [@v, @s, @t]
39
45
 
40
46
  u.clear_likes(@v, @s)
47
+ @v.all_likers.should == []
41
48
  u.like_counts.should == 1
42
49
  u.likes.should =~ [@t.class.name + '_' + @t.id.to_s]
43
- # u.like_history.should =~ [@v.class.name + '_' + @v.id.to_s, @s.class.name + '_' + @s.id.to_s, @t.class.name + '_' + @t.id.to_s]
50
+ u.like_history.should =~ [@v, @s, @t]
44
51
 
45
52
  u.clear_dislikes()
46
53
  u.dislike_counts.should == 0
47
54
  u.dislikes.should == []
48
- # u.dislike_history.should =~ [@v.class.name + '_' + @v.id.to_s, @s.class.name + '_' + @s.id.to_s, @t.class.name + '_' + @t.id.to_s]
55
+ u.dislike_history.should =~ [@v, @s, @t]
49
56
 
50
57
  end
51
58
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jie Fan
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-02-07 00:00:00 -05:00
17
+ date: 2012-02-12 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency