mongoid-likeable 1.0 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # mongoid-likeable
2
2
 
3
- Add like to your Mongoid documents
3
+ Add like to your Mongoid documents.
4
+ This is a fork of `tombell/mongoid-voteable` for people who only need likes.
4
5
 
5
- ## Installation
6
+ **Note** Breaking changes from version 1.0 to 2.0. Check out our wiki for an update script
7
+
8
+ ## Installation
6
9
 
7
10
  Install the gem
8
11
 
@@ -10,7 +13,7 @@ Install the gem
10
13
 
11
14
  or add the gem to your `Gemfile`
12
15
 
13
- gem 'mongoid-likeable', '~> 0.1.0'
16
+ gem 'mongoid-likeable'
14
17
 
15
18
  ## Usage
16
19
 
@@ -31,17 +34,16 @@ You can then like by simply using the `like` method on the model.
31
34
  @story = Story.first
32
35
  @user = User.where(name: 'Simon')
33
36
 
34
- @story.like @user # you like it
37
+ @story.like @user # you like it
35
38
  @story.unlike @user # you don't like it anymore
36
39
  ```
37
40
 
38
- You also have access to a couple of helpful methods.
41
+ You also have access to a helpful method.
39
42
 
40
43
  ```ruby
41
44
  @story.liked? @user # true if the user has already liked this
42
-
43
- @story.likes_count # total number of likes
44
45
  ```
46
+ Likes are stored in a field called `likes`, so you can sort models by like count.
45
47
 
46
48
  **Note** if your users are not stored in a Mongo collection or the ID field is
47
49
  not called `_id` you can still pass the ID in as the second parameter instead.
@@ -6,12 +6,14 @@ module Mongoid
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
- field :likers, :type => Array, :default => []
9
+ field :likes, type: Integer, default: 0
10
+ field :likers, type: Array, default: []
10
11
  end
11
12
 
12
13
  def like(liker)
13
14
  id = liker_id(liker)
14
15
  unless liked?(id)
16
+ self.inc :likes, 1
15
17
  self.push :likers, id
16
18
  end
17
19
  end
@@ -19,6 +21,7 @@ module Mongoid
19
21
  def unlike(liker)
20
22
  id = liker_id(liker)
21
23
  if liked?(id)
24
+ self.inc :likes, -1
22
25
  self.pull :likers, id
23
26
  end
24
27
  end
@@ -28,10 +31,6 @@ module Mongoid
28
31
  likers.include?(id)
29
32
  end
30
33
 
31
- def like_count
32
- likers.count
33
- end
34
-
35
34
  private
36
35
 
37
36
  def liker_id(liker)
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "mongoid-likeable"
6
- s.version = "1.0"
6
+ s.version = "2.0"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Diowa"]
9
9
  s.email = ["dev@diowa.com"]
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
13
13
 
14
14
  s.rubyforge_project = "mongoid-likeable"
15
15
 
16
- s.add_dependency "mongoid", "~> 3.0.5"
16
+ s.add_dependency "mongoid", "~> 3.0.11"
17
17
 
18
18
  s.add_development_dependency "database_cleaner", "~> 0.8.0"
19
19
  s.add_development_dependency "rake", "~> 0.9.2.2"
@@ -13,6 +13,10 @@ class TestMongoidLikeable < MiniTest::Unit::TestCase
13
13
  DatabaseCleaner.clean
14
14
  end
15
15
 
16
+ def test_add_likes_field_to_document
17
+ refute_nil Story.fields['likes']
18
+ end
19
+
16
20
  def test_add_likers_field_to_document
17
21
  refute_nil Story.fields['likers']
18
22
  end
@@ -32,38 +36,33 @@ class TestMongoidLikeable < MiniTest::Unit::TestCase
32
36
  refute_nil story.respond_to?('liked?')
33
37
  end
34
38
 
35
- def test_defines_like_count_method
36
- story = Story.new
37
- refute_nil story.respond_to?('like_count')
38
- end
39
-
40
39
  def test_tracks_number_of_likers
41
40
  @story.like @simon
42
41
  @story.like @emily
43
- assert_equal 2, @story.like_count
42
+ assert_equal 2, @story.likes
44
43
  end
45
44
 
46
45
  def test_can_like_by_user_id
47
46
  @story.like @simon._id
48
- assert_equal 1, @story.like_count
47
+ assert_equal 1, @story.likes
49
48
  end
50
49
 
51
50
  def test_can_like_by_user
52
51
  @story.like @simon
53
- assert_equal 1, @story.like_count
52
+ assert_equal 1, @story.likes
54
53
  end
55
54
 
56
55
  def test_can_unlike_by_user
57
56
  @story.like @simon
58
- assert_equal 1, @story.like_count
57
+ assert_equal 1, @story.likes
59
58
  @story.unlike @simon
60
- assert_equal 0, @story.like_count
59
+ assert_equal 0, @story.likes
61
60
  end
62
61
 
63
62
  def test_limits_one_like_per_liker
64
63
  @story.like @simon
65
64
  @story.like @simon
66
- assert_equal 1, @story.like_count
65
+ assert_equal 1, @story.likes
67
66
  end
68
67
 
69
68
  def test_knows_who_has_liked
@@ -76,12 +75,12 @@ class TestMongoidLikeable < MiniTest::Unit::TestCase
76
75
  @story.like @simon
77
76
  @story.like @emily
78
77
  story = Story.where(:name => 'Mongoid Rocks').first
79
- assert_equal 2, story.like_count
78
+ assert_equal 2, story.likes
80
79
  assert story.liked? @simon
81
80
  assert story.liked? @emily
82
81
  story.unlike @simon
83
82
  story = Story.where(:name => 'Mongoid Rocks').first
84
- assert_equal 1, story.like_count
83
+ assert_equal 1, story.likes
85
84
  refute story.liked? @simon
86
85
  assert story.liked? @emily
87
86
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-likeable
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: '2.0'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-08 00:00:00.000000000 Z
12
+ date: 2012-11-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 3.0.5
21
+ version: 3.0.11
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 3.0.5
29
+ version: 3.0.11
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: database_cleaner
32
32
  requirement: !ruby/object:Gem::Requirement