mongoid_followit 0.1.0
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/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rubocop.yml +11 -0
- data/.travis.yml +15 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +100 -0
- data/Rakefile +13 -0
- data/app/models/follow.rb +26 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/lib/mongoid_followit.rb +10 -0
- data/lib/mongoid_followit/followee.rb +49 -0
- data/lib/mongoid_followit/follower.rb +166 -0
- data/lib/mongoid_followit/queryable.rb +83 -0
- data/lib/mongoid_followit/version.rb +5 -0
- data/mongoid_followit.gemspec +30 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c2143d6a6e327d939ec74904d1051eed9f28a1a5
|
4
|
+
data.tar.gz: 9974bb0d8a10bf61702101cd4b4f54cea1e69060
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a72fdb2c3d213be5e4a13a486140cc2be67f1c3711bb0a8babfcef43c0fe35cb20498b148ab00bb65d4860510a59d589b7d53182e516a976194fc714b55d3aa
|
7
|
+
data.tar.gz: ada9e5773a4aaa67993bac1f4bf5a34f499974e03807fa6910db9dd7668eb5fd94ad4a5d7c3fcc88b6faaf8e55da81a261a446b8886c40b28fcfc489e25ca3fb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.3.0
|
4
|
+
addons:
|
5
|
+
code_climate:
|
6
|
+
repo_token: 2e57b8357cd36e72bbe2c24d5f0648256e540f97f086a5bf016dc586cb6bd21e
|
7
|
+
|
8
|
+
before_install: gem install bundler -v 1.11.2
|
9
|
+
|
10
|
+
services:
|
11
|
+
- mongodb
|
12
|
+
|
13
|
+
script:
|
14
|
+
- bundle install
|
15
|
+
- bundle exec rspec
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at lucastoc@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in mongoid_followit.gemspec
|
4
|
+
|
5
|
+
group :test do
|
6
|
+
gem 'codeclimate-test-reporter', require: false
|
7
|
+
gem 'rspec', '~> 3.0'
|
8
|
+
gem 'mongoid-rspec', '~> 3.0'
|
9
|
+
gem 'factory_girl', '~> 4.7'
|
10
|
+
gem 'database_cleaner', '~> 1.5'
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Lucas Medeiros
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Mongoid::Followit [](https://travis-ci.org/lucasmedeirosleite/mongoid-followit) [](https://codeclimate.com/github/lucasmedeirosleite/mongoid-followit) [](https://codeclimate.com/github/lucasmedeirosleite/mongoid-followit/coverage)
|
2
|
+
|
3
|
+
Add social capabilities to your models.
|
4
|
+
|
5
|
+
### Prerequisites
|
6
|
+
|
7
|
+
* Ruby >= 2.1
|
8
|
+
* Mongoid >= 4.0
|
9
|
+
|
10
|
+
### Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'mongoid_followit'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install mongoid_followit
|
25
|
+
|
26
|
+
### Usage
|
27
|
+
|
28
|
+
Take as an example the following model:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class Person
|
32
|
+
include Mongoid::Document
|
33
|
+
|
34
|
+
field :name, type: String
|
35
|
+
end
|
36
|
+
```
|
37
|
+
When including the module ``` Mongoid::Followit::Follower ```
|
38
|
+
|
39
|
+
The model starts working like this:
|
40
|
+
|
41
|
+
```
|
42
|
+
=> jedi = Profile.create(name: 'Jedi')
|
43
|
+
=> padawan = Profile.create(name: '')
|
44
|
+
=> person = Person.create(name: 'Obi Wan')
|
45
|
+
=> person.follow(jedi, padawan)
|
46
|
+
=> person.unfollow(padawan)
|
47
|
+
=> person.followees
|
48
|
+
```
|
49
|
+
|
50
|
+
The model to be followed MUST include the ```Mongoid::Followit::Followee``` module
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class Profile
|
54
|
+
include Mongoid::Document
|
55
|
+
include Mongoid::Followit::Followee
|
56
|
+
|
57
|
+
field :name, type: String
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
And the followee model starts working like this:
|
62
|
+
|
63
|
+
```
|
64
|
+
=> jedi.followers
|
65
|
+
```
|
66
|
+
|
67
|
+
### Callbacks
|
68
|
+
|
69
|
+
Every model that includes ```Mongoid::Followit::Follower``` can define callbacks for the #follow and #unfollow methods:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
class MyModel
|
73
|
+
include Mongoid::Document
|
74
|
+
include Mongoid::Followit::Follower
|
75
|
+
|
76
|
+
before_follow :do_something_before
|
77
|
+
before_unfollow :do_otherthing_before
|
78
|
+
|
79
|
+
after_follow :do_something_after
|
80
|
+
after_unfollow :do_otherthing_after
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
### TODO
|
85
|
+
|
86
|
+
Add more queryable methods.
|
87
|
+
|
88
|
+
### Development
|
89
|
+
|
90
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
91
|
+
|
92
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
93
|
+
|
94
|
+
### Contributing
|
95
|
+
|
96
|
+
Bug reports and pull requests are welcome on GitHub at [this repository](https://github.com/lucasmedeirosleite/mongoid_followit). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
97
|
+
|
98
|
+
### License
|
99
|
+
|
100
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
namespace :metrics do
|
9
|
+
desc 'Run application metrics application'
|
10
|
+
task :run do
|
11
|
+
system 'bundle exec rubycritic lib --path metrics/rubycritic'
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Follow
|
2
|
+
include Mongoid::Document
|
3
|
+
include Mongoid::Timestamps
|
4
|
+
|
5
|
+
field :followee_class, type: String
|
6
|
+
field :followee_id, type: String
|
7
|
+
field :follower_class, type: String
|
8
|
+
field :follower_id, type: String
|
9
|
+
|
10
|
+
def self.store_by!(followee:, follower:)
|
11
|
+
find_or_create_by!(
|
12
|
+
followee_class: followee.class.to_s,
|
13
|
+
followee_id: followee.id,
|
14
|
+
follower_class: follower.class.to_s,
|
15
|
+
follower_id: follower.id)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.destroy_by!(followee:, follower:)
|
19
|
+
find_by(
|
20
|
+
followee_class: followee.class.to_s,
|
21
|
+
followee_id: followee.id,
|
22
|
+
follower_class: follower.class.to_s,
|
23
|
+
follower_id: follower.id)
|
24
|
+
.try(:destroy)
|
25
|
+
end
|
26
|
+
end
|
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Followit
|
3
|
+
|
4
|
+
##
|
5
|
+
# Public: Module that add followee capabilities to a Mongoid model.
|
6
|
+
# Important: A model can only be followed if it is a Followee one.
|
7
|
+
#
|
8
|
+
# Examples
|
9
|
+
#
|
10
|
+
# class MyModel
|
11
|
+
# include Mongoid::Document
|
12
|
+
# include Mongoid::Followit::Followee
|
13
|
+
# end
|
14
|
+
module Followee
|
15
|
+
def self.included(base)
|
16
|
+
base.class_eval do
|
17
|
+
include Mongoid::Followit::Queryable
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# Public: Peform a query to return all Mongoid model followers.
|
23
|
+
#
|
24
|
+
# criteria(optional) - if true the return will be the type of
|
25
|
+
# Mongoid::Criteria
|
26
|
+
#
|
27
|
+
# Examples
|
28
|
+
#
|
29
|
+
# class Person
|
30
|
+
# include Mongoid::Document
|
31
|
+
# include Mongoid::Followee
|
32
|
+
#
|
33
|
+
# field :name, type: String
|
34
|
+
# validates_uniqueness_of :name
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# # => Person.find_by(name: 'Skywalker').followers
|
38
|
+
#
|
39
|
+
# Returns An Array of followers if criteria argument is false.
|
40
|
+
# Returns A Mongoid::Criteria of followers if criteria argument is true
|
41
|
+
# and followers are of only one type
|
42
|
+
# Raises HasTwoFollowerTypesError if criteria argument is true
|
43
|
+
# and model has two or more types of followers
|
44
|
+
def followers(criteria: false)
|
45
|
+
follow_collection_for_a(:follower, criteria: criteria)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Followit
|
3
|
+
|
4
|
+
##
|
5
|
+
# Public: Module that add follower capabilities to a Mongoid model.
|
6
|
+
#
|
7
|
+
# Examples
|
8
|
+
#
|
9
|
+
# class MyModel
|
10
|
+
# include Mongoid::Document
|
11
|
+
# include Mongoid::Followit::Follower
|
12
|
+
#
|
13
|
+
# before_follow :do_something_before
|
14
|
+
# before_unfollow :do_otherthing_before
|
15
|
+
#
|
16
|
+
# after_follow :do_something_after
|
17
|
+
# after_unfollow :do_otherthing_after
|
18
|
+
#
|
19
|
+
# end
|
20
|
+
module Follower
|
21
|
+
def self.included(base)
|
22
|
+
patch_class(base)
|
23
|
+
generate_callbacks(base)
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Public: Creates Follow entries, for the Followee models, representing
|
28
|
+
# the models that are being followed.
|
29
|
+
#
|
30
|
+
# *followees - A collection of Followee models to be followed.
|
31
|
+
#
|
32
|
+
# Examples
|
33
|
+
#
|
34
|
+
# # => person = Person.create!(name: 'Skywalker')
|
35
|
+
# # => profile = Profile.create!(name: 'Jedi')
|
36
|
+
# # => person.follow(profile)
|
37
|
+
#
|
38
|
+
# Returns nothing.
|
39
|
+
# Raises Runtime error if at least one of the models passed does not
|
40
|
+
# include the Mongoid::Followit::Followee module.
|
41
|
+
def follow(*followees)
|
42
|
+
perform_followable_action(:follow, followees)
|
43
|
+
end
|
44
|
+
|
45
|
+
##
|
46
|
+
# Public: Destroys the Follow entries, for the Followee models,
|
47
|
+
# making the Followee models to be unfollowed.
|
48
|
+
#
|
49
|
+
# *followees - A collection of Followee models to be unfollowed.
|
50
|
+
#
|
51
|
+
# Examples
|
52
|
+
#
|
53
|
+
# # => person.unfollow(jedi, padawan)
|
54
|
+
#
|
55
|
+
# Returns nothing.
|
56
|
+
# Raises Runtime error if at least one of the models passed does not
|
57
|
+
# include the Mongoid::Followit::Followee module.
|
58
|
+
def unfollow(*followees)
|
59
|
+
perform_followable_action(:unfollow, followees)
|
60
|
+
end
|
61
|
+
|
62
|
+
##
|
63
|
+
# Public: Peform a query to return all Mongoid model
|
64
|
+
# that model is following.
|
65
|
+
#
|
66
|
+
# criteria(optional) - if true the return will be the type of
|
67
|
+
# Mongoid::Criteria
|
68
|
+
#
|
69
|
+
# Examples
|
70
|
+
#
|
71
|
+
# class Person
|
72
|
+
# include Mongoid::Document
|
73
|
+
# include Mongoid::Follower
|
74
|
+
#
|
75
|
+
# field :name, type: String
|
76
|
+
# validates_uniqueness_of :name
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# # => Person.find_by(name: 'Skywalker').followees
|
80
|
+
#
|
81
|
+
# Returns An Array of followees if criteria argument is false.
|
82
|
+
# Returns A Mongoid::Criteria of followees if criteria argument is true
|
83
|
+
# and followees are of only one type
|
84
|
+
# Raises HasTwoFolloweeTypesError if criteria argument is true
|
85
|
+
# and model has two or more types of followees
|
86
|
+
def followees(criteria: false)
|
87
|
+
follow_collection_for_a(:followee, criteria: criteria)
|
88
|
+
end
|
89
|
+
|
90
|
+
private_class_method
|
91
|
+
|
92
|
+
##
|
93
|
+
# Internal: Class method that configures the Mongoid model which has
|
94
|
+
# Mongoid::Followit::Follower model included.
|
95
|
+
#
|
96
|
+
# base - Mongoid model class which will be patched.
|
97
|
+
#
|
98
|
+
# PS: This method is used inside the .included method inside this Module.
|
99
|
+
#
|
100
|
+
# Returns nothing.
|
101
|
+
def self.patch_class(base)
|
102
|
+
base.class_eval do
|
103
|
+
include Mongoid::Followit::Queryable
|
104
|
+
include ActiveSupport::Callbacks
|
105
|
+
define_callbacks :follow, :unfollow
|
106
|
+
before_destroy :destroy_follow_data
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
##
|
111
|
+
# Internal: Class method that generate callbacks
|
112
|
+
# for the #follow and #unfollow methods.
|
113
|
+
#
|
114
|
+
# base - Mongoid model class which will be patched.
|
115
|
+
#
|
116
|
+
# PS: This method is used inside the .included method inside this Module.
|
117
|
+
#
|
118
|
+
# Returns nothing.
|
119
|
+
def self.generate_callbacks(base)
|
120
|
+
%w(before after).each do |callback|
|
121
|
+
%w(follow unfollow).each do |action|
|
122
|
+
base.define_singleton_method("#{callback}_#{action}") do |*ar, &blo|
|
123
|
+
set_callback(action.to_sym, callback.to_sym, *ar, &blo)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
private
|
130
|
+
|
131
|
+
def perform_followable_action(action, followables)
|
132
|
+
warn_non_unfollowee_existence(followables)
|
133
|
+
run_callbacks(action) do
|
134
|
+
followables.each do |followable|
|
135
|
+
if :follow == action
|
136
|
+
Follow.store_by!(followee: followable, follower: self)
|
137
|
+
else
|
138
|
+
Follow.destroy_by!(followee: followable, follower: self)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def warn_non_unfollowee_existence(followees)
|
145
|
+
unless followees.any? { |f| f.is_a?(Mongoid::Followit::Followee) }
|
146
|
+
raise 'Object(s) must include a Mongoid::Followit::Followee'
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
##
|
151
|
+
# Internal: Before destroy filter used to clean Mongoid model related
|
152
|
+
# data from the Follow collection.
|
153
|
+
#
|
154
|
+
#
|
155
|
+
# PS: This method is used inside the .patch_class method
|
156
|
+
# inside this Module.
|
157
|
+
#
|
158
|
+
# Returns nothing.
|
159
|
+
def destroy_follow_data
|
160
|
+
followee_params = { followee_class: self.class.to_s, followee_id: id }
|
161
|
+
follower_params = { follower_class: self.class.to_s, follower_id: id }
|
162
|
+
Follow.or(followee_params, follower_params).destroy_all
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Followit
|
3
|
+
|
4
|
+
##
|
5
|
+
# Internal: Module that add query capabilities Follower/Followee model.
|
6
|
+
#
|
7
|
+
# Examples
|
8
|
+
#
|
9
|
+
# module Follower
|
10
|
+
# def self.included(base)
|
11
|
+
# base.class_eval do
|
12
|
+
# include Mongoid::Followit::Queryable
|
13
|
+
# end
|
14
|
+
# end
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# module Followee
|
18
|
+
# def self.included(base)
|
19
|
+
# base.class_eval do
|
20
|
+
# include Mongoid::Followit::Queryable
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
module Queryable
|
25
|
+
|
26
|
+
# Internal: Hash of options to build a query for the Follow collection.
|
27
|
+
FOLLOW_OPTIONS = {
|
28
|
+
followee: {
|
29
|
+
opposite_class: :follower_class,
|
30
|
+
opposite_id: :follower_id,
|
31
|
+
class: :followee_class,
|
32
|
+
id: :followee_id,
|
33
|
+
exception: 'HasTwoFolloweeTypesError'
|
34
|
+
},
|
35
|
+
follower: {
|
36
|
+
opposite_class: :followee_class,
|
37
|
+
opposite_id: :followee_id,
|
38
|
+
class: :follower_class,
|
39
|
+
id: :follower_id,
|
40
|
+
exception: 'HasTwoFollowerTypesError'
|
41
|
+
}
|
42
|
+
}.freeze
|
43
|
+
|
44
|
+
def follow_collection_for_a(behavior, criteria:)
|
45
|
+
options = query_options_for_a(behavior)
|
46
|
+
group_class = FOLLOW_OPTIONS[behavior][:class]
|
47
|
+
grouped = Follow.where(options).group_by { |f| f.send(group_class) }
|
48
|
+
if criteria
|
49
|
+
collection_as_criteria(grouped, behavior)
|
50
|
+
else
|
51
|
+
collection_as_array(grouped, behavior)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def query_options_for_a(behavior)
|
58
|
+
options_class = FOLLOW_OPTIONS[behavior][:opposite_class]
|
59
|
+
options_id = FOLLOW_OPTIONS[behavior][:opposite_id]
|
60
|
+
{
|
61
|
+
options_class => self.class,
|
62
|
+
options_id => id
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
def collection_as_criteria(grouped, behavior)
|
67
|
+
return Follow.none if grouped.empty?
|
68
|
+
raise FOLLOW_OPTIONS[behavior][:exception] if grouped.length > 1
|
69
|
+
klazz = grouped.keys.first
|
70
|
+
ids = grouped[klazz].map { |f| f.send(FOLLOW_OPTIONS[behavior][:id]) }
|
71
|
+
klazz.constantize.in(id: ids)
|
72
|
+
end
|
73
|
+
|
74
|
+
def collection_as_array(grouped, behavior)
|
75
|
+
behavior_class = FOLLOW_OPTIONS[behavior][:class]
|
76
|
+
behavior_id = FOLLOW_OPTIONS[behavior][:id]
|
77
|
+
grouped.values.flatten.map do |follow|
|
78
|
+
follow.send(behavior_class).constantize.find(follow.send(behavior_id))
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid_followit/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'mongoid_followit'
|
8
|
+
spec.version = Mongoid::Followit::VERSION
|
9
|
+
spec.authors = ['Lucas Medeiros']
|
10
|
+
spec.email = ['lucastoc@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{ Follow/Unfollow feature for MongoDB documents. }
|
13
|
+
spec.description = %q{ Follow/Unfollow feature for MongoDB documents. }
|
14
|
+
spec.homepage = 'https://github.com/lucasmedeirosleite'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|metrics)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'activesupport', '>= 4.0'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'mongoid', '>= 4.0.0'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'rubycritic', '~> 2.9'
|
29
|
+
spec.add_development_dependency 'pry-byebug', '~> 3.4.0'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_followit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lucas Medeiros
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mongoid
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.0.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.0.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubycritic
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.9'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.9'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry-byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 3.4.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 3.4.0
|
97
|
+
description: " Follow/Unfollow feature for MongoDB documents. "
|
98
|
+
email:
|
99
|
+
- lucastoc@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".rubocop.yml"
|
107
|
+
- ".travis.yml"
|
108
|
+
- CODE_OF_CONDUCT.md
|
109
|
+
- Gemfile
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- app/models/follow.rb
|
114
|
+
- bin/console
|
115
|
+
- bin/setup
|
116
|
+
- lib/mongoid_followit.rb
|
117
|
+
- lib/mongoid_followit/followee.rb
|
118
|
+
- lib/mongoid_followit/follower.rb
|
119
|
+
- lib/mongoid_followit/queryable.rb
|
120
|
+
- lib/mongoid_followit/version.rb
|
121
|
+
- mongoid_followit.gemspec
|
122
|
+
homepage: https://github.com/lucasmedeirosleite
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.6.4
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: Follow/Unfollow feature for MongoDB documents.
|
146
|
+
test_files: []
|