acts_as_favoritor 4.0.3 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -19
- data/lib/acts_as_favoritor/favoritable.rb +28 -24
- data/lib/acts_as_favoritor/favoritor.rb +38 -31
- data/lib/acts_as_favoritor/favoritor_lib.rb +2 -6
- data/lib/acts_as_favoritor/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e0e21ccec6a8d67036d67b66db5851d554e046eb5a7d8ba592eba0a98b8ae48
|
4
|
+
data.tar.gz: 951039111774466e4bf8347d8e5de83af8960817b8bb934611a06b228b854198
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 390486da05c0e6d4370b433cd6ae774371595726b376febff627dfa5c2bd1f0b154abd57ff4bf55808d4379f4bc8e20552c7d73ffdd19fe60c89e93651f050ed
|
7
|
+
data.tar.gz: a53d91399cb196ba971ab0170c49da2fa7b996cd65d3e1371f095049092e3f820f5740011ac90b97424b6571b64ddfccaa2e2d1bec3360f9aca3fc37aae2b940
|
data/README.md
CHANGED
@@ -8,20 +8,22 @@ You are able to differentiate followers, favorites, watchers, votes and whatever
|
|
8
8
|
|
9
9
|
## Table of Contents
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
11
|
+
- [acts_as_favoritor](#actsasfavoritor)
|
12
|
+
- [Table of Contents](#table-of-contents)
|
13
|
+
- [Installation](#installation)
|
14
|
+
- [Usage](#usage)
|
15
|
+
- [Setup](#setup)
|
16
|
+
- [`acts_as_favoritor` methods](#actsasfavoritor-methods)
|
17
|
+
- [`acts_as_favoritable` methods](#actsasfavoritable-methods)
|
18
|
+
- [`Favorite` model](#favorite-model)
|
19
|
+
- [Scopes](#scopes)
|
20
|
+
- [Caching](#caching)
|
21
|
+
- [Configuration](#configuration)
|
22
|
+
- [Testing](#testing)
|
23
|
+
- [Release](#release)
|
24
|
+
- [To do](#to-do)
|
25
|
+
- [Contributing](#contributing)
|
26
|
+
- [Semantic Versioning](#semantic-versioning)
|
25
27
|
|
26
28
|
---
|
27
29
|
|
@@ -166,24 +168,25 @@ Using scopes with `acts_as_favoritor` enables you to Follow, Watch, Favorite, [.
|
|
166
168
|
|
167
169
|
By default all of your favorites are scoped to `'favorite'`.
|
168
170
|
|
169
|
-
You can create new scopes on the fly. Every single method takes `scope` as an option which expexts an array containing your scopes
|
171
|
+
You can create new scopes on the fly. Every single method takes `scope`/`scopes` as an option which expexts a symbol or an array of symbols containing your scopes.
|
170
172
|
|
171
173
|
So lets see how this works:
|
172
174
|
|
173
175
|
```ruby
|
174
176
|
user.favorite(book, scopes: [:favorite, :watching])
|
175
|
-
user.unfavorite(book,
|
177
|
+
user.unfavorite(book, scope: :watching)
|
176
178
|
second_user = User.find(2)
|
177
|
-
user.favorite(second_user,
|
179
|
+
user.favorite(second_user, scope: :follow)
|
178
180
|
```
|
179
181
|
|
180
182
|
That's simple!
|
181
183
|
|
182
|
-
When you call a method which returns something while specifying multiple scopes, the method returns the results in a hash with the scopes as keys:
|
184
|
+
When you call a method which returns something while specifying multiple scopes, the method returns the results in a hash with the scopes as keys when scopes are given as an array:
|
183
185
|
|
184
186
|
```ruby
|
185
187
|
user.favorited?(book, scopes: [:favorite, :watching]) # => { favorite: true, watching: false }
|
186
|
-
user.favorited?(book, scopes: [:favorite]) # => true
|
188
|
+
user.favorited?(book, scopes: [:favorite]) # => { favorite: true }
|
189
|
+
user.favorited?(book, scope: :favorite) # => true
|
187
190
|
```
|
188
191
|
|
189
192
|
`acts_as_favoritor` also provides some handy scopes for you to call on the `Favorite` model:
|
@@ -44,37 +44,39 @@ module ActsAsFavoritor
|
|
44
44
|
method.to_s[/favoritable_(.+)_total/]
|
45
45
|
end
|
46
46
|
|
47
|
-
def favoritors(
|
48
|
-
|
49
|
-
|
47
|
+
def favoritors(scope: ActsAsFavoritor.configuration.default_scope,
|
48
|
+
scopes: nil)
|
49
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
50
|
+
favorited.includes(:favoritor).unblocked.send("#{s}_list")
|
50
51
|
.map(&:favoritor)
|
51
52
|
end
|
52
53
|
end
|
53
54
|
|
54
55
|
def favoritors_by_type(favoritor_type,
|
55
|
-
|
56
|
-
|
57
|
-
self.class.build_result_for_scopes
|
56
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
57
|
+
scopes: nil)
|
58
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
58
59
|
favoritor_type.constantize.includes(:favorites)
|
59
60
|
.where(favorites: {
|
60
61
|
blocked: false, favoritable_id: id,
|
61
|
-
favoritable_type: self.class.name, scope:
|
62
|
+
favoritable_type: self.class.name, scope: s
|
62
63
|
})
|
63
64
|
end
|
64
65
|
end
|
65
66
|
|
66
67
|
def favorited_by?(favoritor,
|
67
|
-
|
68
|
-
|
69
|
-
|
68
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
69
|
+
scopes: nil)
|
70
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
71
|
+
favorited.unblocked.send("#{s}_list").for_favoritor(favoritor)
|
70
72
|
.first.present?
|
71
73
|
end
|
72
74
|
end
|
73
75
|
|
74
|
-
def block(favoritor,
|
75
|
-
scopes:
|
76
|
-
self.class.build_result_for_scopes
|
77
|
-
get_favorite_for(favoritor,
|
76
|
+
def block(favoritor, scope: ActsAsFavoritor.configuration.default_scope,
|
77
|
+
scopes: nil)
|
78
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
79
|
+
get_favorite_for(favoritor, s)&.block! || Favorite.create(
|
78
80
|
favoritable: self,
|
79
81
|
favoritor: favoritor,
|
80
82
|
blocked: true,
|
@@ -83,24 +85,26 @@ module ActsAsFavoritor
|
|
83
85
|
end
|
84
86
|
end
|
85
87
|
|
86
|
-
def unblock(favoritor,
|
87
|
-
scopes:
|
88
|
-
self.class.build_result_for_scopes
|
89
|
-
get_favorite_for(favoritor,
|
88
|
+
def unblock(favoritor, scope: ActsAsFavoritor.configuration.default_scope,
|
89
|
+
scopes: nil)
|
90
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
91
|
+
get_favorite_for(favoritor, s)&.update(blocked: false)
|
90
92
|
end
|
91
93
|
end
|
92
94
|
|
93
95
|
def blocked?(favoritor,
|
94
|
-
|
95
|
-
|
96
|
-
|
96
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
97
|
+
scopes: nil)
|
98
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
99
|
+
favorited.blocked.send("#{s}_list").for_favoritor(favoritor).first
|
97
100
|
.present?
|
98
101
|
end
|
99
102
|
end
|
100
103
|
|
101
|
-
def blocked(
|
102
|
-
|
103
|
-
|
104
|
+
def blocked(scope: ActsAsFavoritor.configuration.default_scope,
|
105
|
+
scopes: nil)
|
106
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
107
|
+
favorited.includes(:favoritor).blocked.send("#{s}_list")
|
104
108
|
.map(&:favoritor)
|
105
109
|
end
|
106
110
|
end
|
@@ -45,81 +45,88 @@ module ActsAsFavoritor
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def favorite(favoritable,
|
48
|
-
|
49
|
-
|
48
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
49
|
+
scopes: nil)
|
50
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
50
51
|
return nil if self == favoritable
|
51
52
|
|
52
|
-
inc_cache(favoritable,
|
53
|
+
inc_cache(favoritable, s) if ActsAsFavoritor.configuration.cache
|
53
54
|
|
54
|
-
favorites.for_favoritable(favoritable).send("#{
|
55
|
+
favorites.for_favoritable(favoritable).send("#{s}_list")
|
55
56
|
.first_or_create!
|
56
57
|
end
|
57
58
|
end
|
58
59
|
|
59
60
|
def unfavorite(favoritable,
|
60
|
-
|
61
|
-
|
62
|
-
|
61
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
62
|
+
scopes: nil)
|
63
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
64
|
+
favorite_record = get_favorite(favoritable, s)
|
63
65
|
return nil unless favorite_record.present?
|
64
66
|
|
65
|
-
dec_cache(favoritable,
|
67
|
+
dec_cache(favoritable, s) if ActsAsFavoritor.configuration.cache
|
66
68
|
favorite_record.destroy!
|
67
69
|
end
|
68
70
|
end
|
69
71
|
|
70
72
|
def favorited?(favoritable,
|
71
|
-
|
72
|
-
|
73
|
-
|
73
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
74
|
+
scopes: nil)
|
75
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
76
|
+
Favorite.unblocked.send("#{s}_list").for_favoritor(self)
|
74
77
|
.for_favoritable(favoritable).size.positive?
|
75
78
|
end
|
76
79
|
end
|
77
80
|
|
78
|
-
def all_favorites(
|
79
|
-
|
80
|
-
|
81
|
+
def all_favorites(scope: ActsAsFavoritor.configuration.default_scope,
|
82
|
+
scopes: nil)
|
83
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
84
|
+
favorites.unblocked.send("#{s}_list")
|
81
85
|
end
|
82
86
|
end
|
83
87
|
|
84
|
-
def all_favorited(
|
85
|
-
|
86
|
-
|
88
|
+
def all_favorited(scope: ActsAsFavoritor.configuration.default_scope,
|
89
|
+
scopes: nil)
|
90
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
91
|
+
favorites.unblocked.send("#{s}_list").includes(:favoritable)
|
87
92
|
.map(&:favoritable)
|
88
93
|
end
|
89
94
|
end
|
90
95
|
|
91
96
|
def favorites_by_type(favoritable_type,
|
92
|
-
|
93
|
-
|
94
|
-
self.class.build_result_for_scopes
|
95
|
-
favorites.unblocked.send("#{
|
97
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
98
|
+
scopes: nil)
|
99
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
100
|
+
favorites.unblocked.send("#{s}_list")
|
96
101
|
.for_favoritable_type(favoritable_type)
|
97
102
|
end
|
98
103
|
end
|
99
104
|
|
100
105
|
def favorited_by_type(favoritable_type,
|
101
|
-
|
102
|
-
|
103
|
-
self.class.build_result_for_scopes
|
106
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
107
|
+
scopes: nil)
|
108
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
104
109
|
favoritable_type.constantize.includes(:favorited)
|
105
110
|
.where(favorites: {
|
106
111
|
blocked: false, favoritor_id: id,
|
107
|
-
favoritor_type: self.class.name, scope:
|
112
|
+
favoritor_type: self.class.name, scope: s
|
108
113
|
})
|
109
114
|
end
|
110
115
|
end
|
111
116
|
|
112
117
|
def blocked_by?(favoritable,
|
113
|
-
|
114
|
-
|
115
|
-
|
118
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
119
|
+
scopes: nil)
|
120
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
121
|
+
Favorite.blocked.send("#{s}_list").for_favoritor(self)
|
116
122
|
.for_favoritable(favoritable).size.positive?
|
117
123
|
end
|
118
124
|
end
|
119
125
|
|
120
|
-
def blocked_by(
|
121
|
-
|
122
|
-
|
126
|
+
def blocked_by(scope: ActsAsFavoritor.configuration.default_scope,
|
127
|
+
scopes: nil)
|
128
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
129
|
+
favorites.includes(:favoritable).blocked.send("#{s}_list")
|
123
130
|
.map(&:favoritable)
|
124
131
|
end
|
125
132
|
end
|
@@ -3,14 +3,10 @@
|
|
3
3
|
module ActsAsFavoritor
|
4
4
|
module FavoritorLib
|
5
5
|
def build_result_for_scopes(scopes)
|
6
|
+
return yield(scopes) unless scopes.is_a?(Array)
|
6
7
|
return if scopes.empty?
|
7
8
|
|
8
|
-
scopes
|
9
|
-
result = scopes.map { |scope| [scope, yield(scope)] }.to_h
|
10
|
-
|
11
|
-
return result[scopes.first] if scopes.size == 1
|
12
|
-
|
13
|
-
result
|
9
|
+
sanitized_scopes(scopes).map { |scope| [scope, yield(scope)] }.to_h
|
14
10
|
end
|
15
11
|
|
16
12
|
private
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_favoritor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Hübotter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|