mongoid-likes 0.1.9
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.
- checksums.yaml +7 -0
- data/Gemfile +15 -0
- data/LICENSE +21 -0
- data/README.md +119 -0
- data/Rakefile +9 -0
- data/spec/likes_spec.rb +81 -0
- data/spec/models/post.rb +12 -0
- data/spec/models/user.rb +13 -0
- data/spec/spec_helper.rb +40 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9bb8a570b83cd3821ae4449e7167cea43b928136
|
4
|
+
data.tar.gz: 20bb924b84b48ad15efd9e6ec448d49536a1027f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 75fec1396329796f18ea737c66fdf0a7d55ed20cdd28b087fe4f6f92c2c4b1e0cf8e062eeb76d996773f903be3f05c37f52845e2764cb5804b13e11bacd0d64e
|
7
|
+
data.tar.gz: 5784e2cba8ff7f01c7dcf90cfab72cdcbd261941c70db1714d497735b38f8b0a68bd90f6eccaf21a2233e10cc9561864f65038f356e4590750cc8add65b3c1ec
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in mongoid_likes.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use a debugger
|
14
|
+
# gem 'byebug', group: [:development, :test]
|
15
|
+
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 dingxizheng
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
# mongoid-likes
|
2
|
+
|
3
|
+
Forked form https://github.com/diowa/mongoid-likeable.git
|
4
|
+
|
5
|
+
This Gem extends your Mongoid user model to have like and dislike abilities
|
6
|
+
to any likeable models
|
7
|
+
|
8
|
+
## How to install
|
9
|
+
|
10
|
+
Install the gem
|
11
|
+
|
12
|
+
$ gem install mongoid-likes
|
13
|
+
|
14
|
+
or add the gem to your `Gemfile`
|
15
|
+
|
16
|
+
gem 'mongoid-likes'
|
17
|
+
|
18
|
+
|
19
|
+
## How to use
|
20
|
+
|
21
|
+
Include the `Mongoid::Likeable` module into the models you want to like
|
22
|
+
|
23
|
+
Include the `Mongoid::Liker` module into the user model
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class User
|
27
|
+
include Mongoid::Document
|
28
|
+
include Mongoid::Liker
|
29
|
+
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class Post
|
36
|
+
include Mongoid::Document
|
37
|
+
include Mongoid::Likeable
|
38
|
+
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
Now you can `like` or `dislike` your `posts`.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
@user.like @post # user likes a post
|
46
|
+
|
47
|
+
@user.unlike @post # user unlikes a post liked before
|
48
|
+
|
49
|
+
@user.dislike @post # user dislikes a post
|
50
|
+
|
51
|
+
@user.undislike @post # user undislike a post disliked before
|
52
|
+
```
|
53
|
+
|
54
|
+
Note that a likeable resource can not be liked and dislied by the same user at the same time
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
@user.like @post_one #=> user likes a post
|
58
|
+
|
59
|
+
@user.dislike @post_one #=> will return false
|
60
|
+
```
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
@user.dislike @post_one #=> user dislikes a post
|
64
|
+
|
65
|
+
@user.like @post_one #=> will return false
|
66
|
+
```
|
67
|
+
|
68
|
+
To get how many likes or dislikes a resource have
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
@post.likes #=> number of likes current post have
|
72
|
+
@post.dislikes #=> number of dislikes current post have
|
73
|
+
```
|
74
|
+
|
75
|
+
To check if user liked of disliked a resource
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
@post.liked_by? @user
|
79
|
+
|
80
|
+
@user.liked? @post
|
81
|
+
|
82
|
+
@post.disliked_by? @user
|
83
|
+
|
84
|
+
@user.disliked? @user
|
85
|
+
```
|
86
|
+
|
87
|
+
To get a resource's `likers` and `dislikers`
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
@post.likers #=> results in Mongoid::Criteria
|
91
|
+
@post.lkiers.where(....)
|
92
|
+
|
93
|
+
@post.dislikers #=> results in Mongoid::Criteria
|
94
|
+
@post.dislkiers.where(....)
|
95
|
+
```
|
96
|
+
|
97
|
+
To get specified `type` of objects user liked or disliked
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
@user.all_liked :post #=> results in Mongoid::Criteria
|
101
|
+
|
102
|
+
@user.all_liked :blabla #=> return nil, since model 'blabla' does not exist
|
103
|
+
|
104
|
+
@user.all_disliked :post #=> results in Mongoid::Criteria
|
105
|
+
```
|
106
|
+
|
107
|
+
|
108
|
+
## Contributing
|
109
|
+
any issue reports and conributions are welcome
|
110
|
+
|
111
|
+
|
112
|
+
## Authors
|
113
|
+
|
114
|
+
+ http://github.com/dingxizheng
|
115
|
+
+ http://dingxizheng.com
|
116
|
+
|
117
|
+
## Copyright and license
|
118
|
+
|
119
|
+
Copyright 2016 dingxizheng under MIT LICENSE.
|
data/Rakefile
ADDED
data/spec/likes_spec.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# @Author: dingxizheng
|
3
|
+
# @Date: 2016-01-21 15:38:08
|
4
|
+
# @Last Modified by: mover
|
5
|
+
# @Last Modified time: 2016-01-22 14:34:16
|
6
|
+
|
7
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
8
|
+
|
9
|
+
describe Mongoid::Liker do
|
10
|
+
|
11
|
+
before(:each) do
|
12
|
+
@user = User.create({
|
13
|
+
:name => "ding"
|
14
|
+
})
|
15
|
+
end
|
16
|
+
|
17
|
+
before(:each) do
|
18
|
+
@posts = []
|
19
|
+
@posts << Post.create({ :title => "post 1" })
|
20
|
+
@posts << Post.create({ :title => "post 2" })
|
21
|
+
@posts << Post.create({ :title => "post 3" })
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return 0 likes" do
|
25
|
+
expect(@user.likes).to eq 0
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return 1 likes" do
|
29
|
+
@user.like(@posts[0])
|
30
|
+
expect(@posts[0].likes).to eq 1
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should return 1 likes" do
|
34
|
+
@user.like(@posts[1])
|
35
|
+
expect(@posts[1].likes).to eq 1
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return the liked post1" do
|
39
|
+
@user.like(@posts[1])
|
40
|
+
expect(@posts[1].likers.first).to eq @user
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return the numember of likes" do
|
44
|
+
@user.like(@posts[1])
|
45
|
+
@user.unlike(@posts[1])
|
46
|
+
expect(@posts[1].likes).to eq 0
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should return the numember of likes" do
|
50
|
+
@user.like(@posts[1])
|
51
|
+
@user.unlike(@posts[1])
|
52
|
+
expect(@posts[1].likes).to eq 0
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should return true" do
|
56
|
+
@user.like(@posts[2])
|
57
|
+
expect(@posts[2].liked_by? @user).to eq true
|
58
|
+
expect(@user.liked? @posts[2]).to eq true
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should return true when post1 is disliked by user" do
|
62
|
+
@user.dislike(@posts[1])
|
63
|
+
@user.like(@posts[1])
|
64
|
+
expect(@posts[1].liked_by? @user).to eq false
|
65
|
+
expect(@user.disliked? @posts[1]).to eq true
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should return a list of disliked posts" do
|
69
|
+
@user.dislike(@posts[0])
|
70
|
+
@user.dislike(@posts[1])
|
71
|
+
expect(@user.all_disliked('post').count).to eq 2
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should return a list of liked posts" do
|
75
|
+
@user.like(@posts[0])
|
76
|
+
@user.like(@posts[1])
|
77
|
+
@user.dislike(@posts[1])
|
78
|
+
expect(@user.all_liked('post').count).to eq 2
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
data/spec/models/post.rb
ADDED
data/spec/models/user.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# @Author: mover
|
3
|
+
# @Date: 2016-01-21 15:35:58
|
4
|
+
# @Last Modified by: dingxizheng
|
5
|
+
# @Last Modified time: 2016-01-21 15:36:49
|
6
|
+
|
7
|
+
class User
|
8
|
+
include Mongoid::Document
|
9
|
+
include Mongoid::Likeable
|
10
|
+
include Mongoid::Liker
|
11
|
+
|
12
|
+
field :name, type: String
|
13
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# @Author: dingxizheng
|
3
|
+
# @Date: 2016-01-21 14:33:10
|
4
|
+
# @Last Modified by: mover
|
5
|
+
# @Last Modified time: 2016-01-22 14:10:09
|
6
|
+
|
7
|
+
require 'mongoid'
|
8
|
+
require 'database_cleaner'
|
9
|
+
|
10
|
+
Dir["#{File.dirname(__FILE__)}/../lib/**/*.rb"].each { |file|
|
11
|
+
require file
|
12
|
+
}
|
13
|
+
|
14
|
+
Mongo::Logger.logger.level = ::Logger::FATAL
|
15
|
+
Mongoid.configure do |config|
|
16
|
+
config.connect_to "mongoid_likes_test"
|
17
|
+
config.options = { :raise_not_found_error => false }
|
18
|
+
end
|
19
|
+
|
20
|
+
Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |file|
|
21
|
+
require file
|
22
|
+
}
|
23
|
+
|
24
|
+
DatabaseCleaner.orm = :mongoid
|
25
|
+
|
26
|
+
RSpec.configure do |config|
|
27
|
+
|
28
|
+
config.before(:all) do
|
29
|
+
DatabaseCleaner.strategy = :truncation
|
30
|
+
end
|
31
|
+
|
32
|
+
config.before(:each) do
|
33
|
+
DatabaseCleaner.start
|
34
|
+
end
|
35
|
+
|
36
|
+
config.after(:each) do
|
37
|
+
DatabaseCleaner.clean
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-likes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.9
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Xizheng Ding
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bson_ext
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: database_cleaner
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: adding like and dislike behaviors to Mongoid user model
|
70
|
+
email:
|
71
|
+
- dingxizheng@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- Gemfile
|
77
|
+
- LICENSE
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- spec/likes_spec.rb
|
81
|
+
- spec/models/post.rb
|
82
|
+
- spec/models/user.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
homepage: https://github.com/dingxizheng/mongoid_likes
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project: mongoid-likes
|
104
|
+
rubygems_version: 2.4.2
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: adding like and dislike behaviors to Mongoid user model
|
108
|
+
test_files:
|
109
|
+
- spec/likes_spec.rb
|
110
|
+
- spec/models/post.rb
|
111
|
+
- spec/models/user.rb
|
112
|
+
- spec/spec_helper.rb
|