mongoid-likeable 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2012, diowa
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
17
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+
24
+ mongoid-voteable is licensed under the MIT License - Copyright (c) 2012 Tom Bell
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # mongoid-likeable
2
+
3
+ Add like to your Mongoid documents
4
+
5
+ ## Installation
6
+
7
+ Install the gem
8
+
9
+ $ gem install mongoid-likeable
10
+
11
+ or add the gem to your `Gemfile`
12
+
13
+ gem 'mongoid-likeable', '~> 0.1.0'
14
+
15
+ ## Usage
16
+
17
+ Include the `Mongoid::Likeable` module into your models you want to like.
18
+
19
+ ```ruby
20
+ class Story
21
+ include Mongoid::Document
22
+ include Mongoid::Likeable
23
+
24
+ # ...
25
+ end
26
+ ```
27
+
28
+ You can then like by simply using the `like` method on the model.
29
+
30
+ ```ruby
31
+ @story = Story.first
32
+ @user = User.where(name: 'Simon')
33
+
34
+ @story.like @user # you like it
35
+ @story.unlike @user # you don't like it anymore
36
+ ```
37
+
38
+ You also have access to a couple of helpful methods.
39
+
40
+ ```ruby
41
+ @story.liked? @user # true if the user has already liked this
42
+
43
+ @story.likes_count # total number of likes
44
+ ```
45
+
46
+ **Note** if your users are not stored in a Mongo collection or the ID field is
47
+ not called `_id` you can still pass the ID in as the second parameter instead.
48
+
49
+ ```ruby
50
+ @story.like @user.id
51
+
52
+ @story.liked? @user.id
53
+ ```
54
+
55
+ ## License
56
+
57
+ mongoid-likeable is licensed under the BSD 2-Clause License
58
+ mongoid-voteable is licensed under the MIT License - Copyright (c) 2012 Tom Bell
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ task :default => :test
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/*_test.rb'
7
+ test.verbose = true
8
+ end
9
+
10
+ desc "Open an irb session preloaded with this library"
11
+ task :console do
12
+ sh "irb -rubygems -r ./lib/mongoid/likeable.rb"
13
+ end
@@ -0,0 +1,46 @@
1
+ require 'mongoid'
2
+
3
+ module Mongoid
4
+ module Likeable
5
+
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ field :likers, :type => Array, :default => []
10
+ end
11
+
12
+ def like(liker)
13
+ id = liker_id(liker)
14
+ unless liked?(id)
15
+ self.push :likers, id
16
+ end
17
+ end
18
+
19
+ def unlike(liker)
20
+ id = liker_id(liker)
21
+ if liked?(id)
22
+ self.pull :likers, id
23
+ end
24
+ end
25
+
26
+ def liked?(liker)
27
+ id = liker_id(liker)
28
+ likers.include?(id)
29
+ end
30
+
31
+ def like_count
32
+ likers.count
33
+ end
34
+
35
+ private
36
+
37
+ def liker_id(liker)
38
+ if liker.respond_to?(:_id)
39
+ liker._id
40
+ else
41
+ liker
42
+ end
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "mongoid-likeable"
6
+ s.version = "1.0"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Diowa"]
9
+ s.email = ["dev@diowa.com"]
10
+ s.homepage = "https://github.com/diowa/mongoid-likeable"
11
+ s.summary = "Add likes to your Mongoid documents"
12
+ s.description = s.summary
13
+
14
+ s.rubyforge_project = "mongoid-likeable"
15
+
16
+ s.add_dependency "mongoid", "~> 3.0.5"
17
+
18
+ s.add_development_dependency "database_cleaner", "~> 0.8.0"
19
+ s.add_development_dependency "rake", "~> 0.9.2.2"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,6 @@
1
+ class Story
2
+ include Mongoid::Document
3
+ include Mongoid::Likeable
4
+
5
+ field :name
6
+ end
@@ -0,0 +1,5 @@
1
+ class User
2
+ include Mongoid::Document
3
+
4
+ field :name
5
+ end
@@ -0,0 +1,88 @@
1
+ require 'test_helper'
2
+
3
+ class TestMongoidLikeable < MiniTest::Unit::TestCase
4
+ def setup
5
+ DatabaseCleaner.start
6
+
7
+ @simon = User.create name: 'Simon'
8
+ @emily = User.create name: 'Emily'
9
+ @story = Story.create name: 'Mongoid Rocks'
10
+ end
11
+
12
+ def teardown
13
+ DatabaseCleaner.clean
14
+ end
15
+
16
+ def test_add_likers_field_to_document
17
+ refute_nil Story.fields['likers']
18
+ end
19
+
20
+ def test_defines_like_method
21
+ story = Story.new
22
+ refute_nil story.respond_to?('like')
23
+ end
24
+
25
+ def test_defines_unlike_method
26
+ story = Story.new
27
+ refute_nil story.respond_to?('unlike')
28
+ end
29
+
30
+ def test_defines_liked_method
31
+ story = Story.new
32
+ refute_nil story.respond_to?('liked?')
33
+ end
34
+
35
+ def test_defines_like_count_method
36
+ story = Story.new
37
+ refute_nil story.respond_to?('like_count')
38
+ end
39
+
40
+ def test_tracks_number_of_likers
41
+ @story.like @simon
42
+ @story.like @emily
43
+ assert_equal 2, @story.like_count
44
+ end
45
+
46
+ def test_can_like_by_user_id
47
+ @story.like @simon._id
48
+ assert_equal 1, @story.like_count
49
+ end
50
+
51
+ def test_can_like_by_user
52
+ @story.like @simon
53
+ assert_equal 1, @story.like_count
54
+ end
55
+
56
+ def test_can_unlike_by_user
57
+ @story.like @simon
58
+ assert_equal 1, @story.like_count
59
+ @story.unlike @simon
60
+ assert_equal 0, @story.like_count
61
+ end
62
+
63
+ def test_limits_one_like_per_liker
64
+ @story.like @simon
65
+ @story.like @simon
66
+ assert_equal 1, @story.like_count
67
+ end
68
+
69
+ def test_knows_who_has_liked
70
+ @story.like @simon
71
+ assert @story.liked? @simon
72
+ refute @story.liked? @emily
73
+ end
74
+
75
+ def test_updates_collection_correctly
76
+ @story.like @simon
77
+ @story.like @emily
78
+ story = Story.where(:name => 'Mongoid Rocks').first
79
+ assert_equal 2, story.like_count
80
+ assert story.liked? @simon
81
+ assert story.liked? @emily
82
+ story.unlike @simon
83
+ story = Story.where(:name => 'Mongoid Rocks').first
84
+ assert_equal 1, story.like_count
85
+ refute story.liked? @simon
86
+ assert story.liked? @emily
87
+ end
88
+ end
@@ -0,0 +1,15 @@
1
+ $:.unshift(File.expand_path("../../lib", __FILE__))
2
+
3
+ require 'minitest/autorun'
4
+ require 'database_cleaner'
5
+
6
+ require 'mongoid'
7
+ require 'mongoid/likeable'
8
+
9
+ Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
10
+
11
+ Mongoid.connect_to('mongoid-likeable-test')
12
+ Mongoid.logger = Logger.new $stdout
13
+
14
+ DatabaseCleaner.orm = 'mongoid'
15
+ DatabaseCleaner.strategy = :truncation
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-likeable
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Diowa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.5
30
+ - !ruby/object:Gem::Dependency
31
+ name: database_cleaner
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.8.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.8.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.9.2.2
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.2.2
62
+ description: Add likes to your Mongoid documents
63
+ email:
64
+ - dev@diowa.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .travis.yml
71
+ - Gemfile
72
+ - LICENSE
73
+ - README.md
74
+ - Rakefile
75
+ - lib/mongoid/likeable.rb
76
+ - mongoid-likeable.gemspec
77
+ - test/models/story.rb
78
+ - test/models/user.rb
79
+ - test/mongoid_likeable_test.rb
80
+ - test/test_helper.rb
81
+ homepage: https://github.com/diowa/mongoid-likeable
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project: mongoid-likeable
101
+ rubygems_version: 1.8.24
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Add likes to your Mongoid documents
105
+ test_files:
106
+ - test/models/story.rb
107
+ - test/models/user.rb
108
+ - test/mongoid_likeable_test.rb
109
+ - test/test_helper.rb