acts_as_favoritor 4.0.0 → 5.0.1
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 +4 -4
- data/README.md +50 -82
- data/lib/acts_as_favoritor/configuration.rb +1 -2
- data/lib/acts_as_favoritor/favoritable.rb +34 -25
- data/lib/acts_as_favoritor/favorite_scopes.rb +2 -0
- data/lib/acts_as_favoritor/favoritor.rb +46 -36
- data/lib/acts_as_favoritor/favoritor_lib.rb +2 -6
- data/lib/acts_as_favoritor/version.rb +1 -1
- data/lib/generators/acts_as_favoritor_generator.rb +2 -1
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2554cf505e5a47eb5f24bc1de95fb6287d397259cc70685f4905d99be6164e72
|
4
|
+
data.tar.gz: ac5749692ac44627c97dfb2797834671f7d3cc368d32e63aac5d2d9a94e4b6b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 544dda8f78ced970192a0d523f8c7b4804be135d33afe8635a15eaef3263d5413793d54ca5342fa12636d4a1c893b4d52bb1c160180bb72386c4a144a3280a6b
|
7
|
+
data.tar.gz: 28fd936df2e6dc96f6b5a2449fc654f40b4307a7d4bed58e5f648652e283af46d3547f1e3ac2245b928400d1391186fae291ff4fc1756438a14afb8c03858abe
|
data/README.md
CHANGED
@@ -1,42 +1,20 @@
|
|
1
1
|
# acts_as_favoritor
|
2
2
|
|
3
|
-
[](https://badge.fury.io/rb/acts_as_favoritor) 
|
4
|
-
|
5
3
|
acts_as_favoritor is a Rubygem to allow any ActiveRecord model to associate any other model including the option for multiple relationships per association with scopes.
|
6
4
|
|
7
5
|
You are able to differentiate followers, favorites, watchers, votes and whatever else you can imagine through a single relationship. This is accomplished by a double polymorphic relationship on the Favorite model. There is also built in support for blocking/un-blocking favorite records as well as caching.
|
8
6
|
|
9
|
-
---
|
10
|
-
|
11
|
-
## Table of Contents
|
12
|
-
|
13
|
-
* [Installation](#installation)
|
14
|
-
* [Usage](#usage)
|
15
|
-
* [Setup](#setup)
|
16
|
-
* [`acts_as_favoritor` methods](#acts_as_favoritor-methods)
|
17
|
-
* [`acts_as_favoritable` methods](#acts_as_favoritable-methods)
|
18
|
-
* [`Favorite` model](#favorite-model)
|
19
|
-
* [Scopes](#scopes)
|
20
|
-
* [Caching](#caching)
|
21
|
-
* [Configuration](#configuration)
|
22
|
-
* [Testing](#testing)
|
23
|
-
* [To do](#to-do)
|
24
|
-
* [Contributing](#contributing)
|
25
|
-
* [Semantic versioning](#semantic-versioning)
|
26
|
-
|
27
|
-
---
|
28
|
-
|
29
7
|
## Installation
|
30
8
|
|
31
|
-
|
9
|
+
You can add acts_as_favoritor to your `Gemfile` with:
|
32
10
|
|
33
11
|
```ruby
|
34
12
|
gem 'acts_as_favoritor'
|
35
13
|
```
|
36
14
|
|
37
|
-
And then
|
15
|
+
And then run:
|
38
16
|
|
39
|
-
$ bundle
|
17
|
+
$ bundle install
|
40
18
|
|
41
19
|
Or install it yourself as:
|
42
20
|
|
@@ -92,48 +70,44 @@ user.favorite(book)
|
|
92
70
|
# `user` removes `book` from favorites.
|
93
71
|
user.unfavorite(book)
|
94
72
|
|
95
|
-
# Whether `user` has marked `book` as his favorite.
|
73
|
+
# Whether `user` has marked `book` as his favorite.
|
96
74
|
user.favorited?(book)
|
97
75
|
|
98
|
-
#
|
76
|
+
# Returns an Active Record relation of `user`'s `Favorite` records that have not been blocked.
|
99
77
|
user.all_favorites
|
100
78
|
|
101
|
-
# Returns all favorited objects of `user
|
79
|
+
# Returns an array of all unblocked favorited objects of `user`. This can be a collection of different object types, e.g.: `User`, `Book`.
|
102
80
|
user.all_favorited
|
103
81
|
|
104
|
-
# Returns an
|
82
|
+
# Returns an Active Record relation of `Favorite` records where the `favoritable_type` is `Book`.
|
105
83
|
user.favorites_by_type('Book')
|
106
84
|
|
107
|
-
# Returns an
|
85
|
+
# Returns an Active Record relation of all favorited objects of `user` where `favoritable_type` is 'Book'.
|
108
86
|
user.favorited_by_type('Book')
|
109
87
|
|
110
|
-
# Returns the exact same
|
88
|
+
# Returns the exact same as `user.favorited_by_type('User')`.
|
111
89
|
user.favorited_users
|
112
90
|
|
113
|
-
# Whether `user` has been blocked by `book`.
|
91
|
+
# Whether `user` has been blocked by `book`.
|
114
92
|
user.blocked_by?(book)
|
115
93
|
|
116
|
-
# Returns an array
|
94
|
+
# Returns an array of all favoritables that blocked `user`.
|
117
95
|
user.blocked_by
|
118
96
|
```
|
119
97
|
|
120
|
-
These methods take an optional hash parameter of ActiveRecord options (`:limit`, `:order`, etc...)
|
121
|
-
|
122
|
-
favorites_by_type, all_favorites, all_favorited, favorited_by_type
|
123
|
-
|
124
98
|
### `acts_as_favoritable` methods
|
125
99
|
|
126
100
|
```ruby
|
127
101
|
# Returns all favoritors of a model that `acts_as_favoritable`
|
128
102
|
book.favoritors
|
129
103
|
|
130
|
-
# Returns an
|
104
|
+
# Returns an Active Record relation of records with type `User` following `book`.
|
131
105
|
book.favoritors_by_type('User')
|
132
106
|
|
133
|
-
# Returns the exact same as `book.favoritors_by_type
|
107
|
+
# Returns the exact same as `book.favoritors_by_type('User')`.
|
134
108
|
book.user_favoritors
|
135
109
|
|
136
|
-
# Whether `book` has been favorited by `user`.
|
110
|
+
# Whether `book` has been favorited by `user`.
|
137
111
|
book.favorited_by?(user)
|
138
112
|
|
139
113
|
# Block a favoritor
|
@@ -142,30 +116,26 @@ book.block(user)
|
|
142
116
|
# Unblock a favoritor
|
143
117
|
book.unblock(user)
|
144
118
|
|
145
|
-
# Whether `book` has blocked `user` as favoritor.
|
119
|
+
# Whether `book` has blocked `user` as favoritor.
|
146
120
|
book.blocked?(user)
|
147
121
|
|
148
|
-
# Returns an array
|
122
|
+
# Returns an array of all blocked favoritors.
|
149
123
|
book.blocked
|
150
124
|
```
|
151
125
|
|
152
|
-
These methods take an optional hash parameter of ActiveRecord options (`:limit`, `:order`, etc...)
|
153
|
-
|
154
|
-
favoritors_by_type, favoritors, blocks
|
155
|
-
|
156
126
|
### `Favorite` model
|
157
127
|
|
158
128
|
```ruby
|
159
|
-
# Returns all `Favorite` records where `blocked` is `false`.
|
129
|
+
# Returns an Active Record relation of all `Favorite` records where `blocked` is `false`.
|
160
130
|
Favorite.unblocked
|
161
131
|
|
162
|
-
# Returns all `Favorite` records where `blocked` is `true`.
|
132
|
+
# Returns an Active Record relation of all `Favorite` records where `blocked` is `true`.
|
163
133
|
Favorite.blocked
|
164
134
|
|
165
|
-
# Returns all favorites of `user`, including those who were blocked.
|
135
|
+
# Returns an Active Record relation of all favorites of `user`, including those who were blocked.
|
166
136
|
Favorite.for_favoritor(user)
|
167
137
|
|
168
|
-
# Returns all favoritors of `book`, including those who were blocked.
|
138
|
+
# Returns an Active Record relation of all favoritors of `book`, including those who were blocked.
|
169
139
|
Favorite.for_favoritable(book)
|
170
140
|
```
|
171
141
|
|
@@ -175,24 +145,25 @@ Using scopes with `acts_as_favoritor` enables you to Follow, Watch, Favorite, [.
|
|
175
145
|
|
176
146
|
By default all of your favorites are scoped to `'favorite'`.
|
177
147
|
|
178
|
-
You can create new scopes on the fly. Every single method takes `scope` as an option which expexts an array containing your scopes
|
148
|
+
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.
|
179
149
|
|
180
150
|
So lets see how this works:
|
181
151
|
|
182
152
|
```ruby
|
183
153
|
user.favorite(book, scopes: [:favorite, :watching])
|
184
|
-
user.unfavorite(book,
|
154
|
+
user.unfavorite(book, scope: :watching)
|
185
155
|
second_user = User.find(2)
|
186
|
-
user.favorite(second_user,
|
156
|
+
user.favorite(second_user, scope: :follow)
|
187
157
|
```
|
188
158
|
|
189
159
|
That's simple!
|
190
160
|
|
191
|
-
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:
|
161
|
+
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:
|
192
162
|
|
193
163
|
```ruby
|
194
164
|
user.favorited?(book, scopes: [:favorite, :watching]) # => { favorite: true, watching: false }
|
195
|
-
user.favorited?(book, scopes: [:favorite]) # => true
|
165
|
+
user.favorited?(book, scopes: [:favorite]) # => { favorite: true }
|
166
|
+
user.favorited?(book, scope: :favorite) # => true
|
196
167
|
```
|
197
168
|
|
198
169
|
`acts_as_favoritor` also provides some handy scopes for you to call on the `Favorite` model:
|
@@ -251,58 +222,55 @@ book.favoritable_favorite_cache # => 1
|
|
251
222
|
|
252
223
|
**Note:** These methods are available for every scope you are using.
|
253
224
|
|
254
|
-
---
|
255
|
-
|
256
225
|
## Configuration
|
257
226
|
|
258
227
|
You can configure acts_as_favoritor by passing a block to `configure`. This can be done in `config/initializers/acts_as_favoritor.rb`:
|
259
228
|
|
260
229
|
```ruby
|
261
230
|
ActsAsFavoritor.configure do |config|
|
262
|
-
config.default_scope =
|
231
|
+
config.default_scope = :follow
|
263
232
|
end
|
264
233
|
```
|
265
234
|
|
266
|
-
**`default_scope`** Specify your default scope. Takes a string. Defaults to
|
235
|
+
**`default_scope`** Specify your default scope. Takes a string. Defaults to `:favorite`. Learn more about scopes [here](#scopes).
|
267
236
|
|
268
237
|
**`cache`** Whether `acts_as_favoritor` uses caching or not. Takes a boolean. Defaults to `false`. Learn more about caching [here](#caching).
|
269
238
|
|
270
|
-
|
271
|
-
|
272
|
-
## Testing
|
239
|
+
## Development
|
273
240
|
|
274
|
-
|
241
|
+
To start development you first have to fork this repository and locally clone your fork.
|
275
242
|
|
276
|
-
|
277
|
-
2. Clone your forked git locally
|
278
|
-
3. Install dependencies
|
243
|
+
Install the projects dependencies by running:
|
279
244
|
|
280
|
-
|
245
|
+
$ bundle install
|
281
246
|
|
282
|
-
|
247
|
+
### Testing
|
283
248
|
|
284
|
-
|
249
|
+
Tests are written with RSpec and can be found in `/spec`.
|
285
250
|
|
286
|
-
|
251
|
+
To run tests:
|
287
252
|
|
288
|
-
|
253
|
+
$ bundle exec rspec
|
289
254
|
|
290
|
-
|
255
|
+
To run RuboCop:
|
291
256
|
|
292
|
-
|
257
|
+
$ bundle exec rubocop
|
293
258
|
|
294
|
-
|
295
|
-
|
296
|
-
To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/acts_as_favoritor/issues/new).
|
259
|
+
## Contributing
|
297
260
|
|
298
|
-
|
261
|
+
We warmly welcome everyone who is intersted in contributing. Please reference our [contributing guidelines](CONTRIBUTING.md) and our [Code of Conduct](CODE_OF_CONDUCT.md).
|
299
262
|
|
300
|
-
##
|
263
|
+
## Releases
|
301
264
|
|
302
|
-
|
265
|
+
[Here](https://github.com/jonhue/acts_as_favoritor/releases) you can find details on all past releases. Unreleased breaking changes that are on the current master can be found [here](CHANGELOG.md).
|
303
266
|
|
304
|
-
|
267
|
+
acts_as_favoritor follows Semantic Versioning 2.0 as defined at http://semver.org. Reference our [security policy](SECURITY.md).
|
305
268
|
|
306
|
-
###
|
269
|
+
### Publishing
|
307
270
|
|
308
|
-
|
271
|
+
1. Review breaking changes and deprecations in `CHANGELOG.md`.
|
272
|
+
1. Change the gem version in `lib/acts_as_favoritor/version.rb`.
|
273
|
+
1. Reset `CHANGELOG.md`.
|
274
|
+
1. Create a pull request to merge the changes into `master`.
|
275
|
+
1. After the pull request was merged, create a new release listing the breaking changes and commits on `master` since the last release.
|
276
|
+
2. The release workflow will publish the gem to RubyGems.
|
@@ -38,40 +38,47 @@ module ActsAsFavoritor
|
|
38
38
|
end
|
39
39
|
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
40
40
|
|
41
|
+
# rubocop:disable Style/OptionalBooleanParameter
|
41
42
|
def respond_to_missing?(method, include_private = false)
|
42
43
|
super || method.to_s[/(.+)_favoritors/] ||
|
43
44
|
method.to_s[/favoritable_(.+)_score/] ||
|
44
45
|
method.to_s[/favoritable_(.+)_total/]
|
45
46
|
end
|
47
|
+
# rubocop:enable Style/OptionalBooleanParameter
|
46
48
|
|
47
|
-
def favoritors(
|
48
|
-
|
49
|
-
|
49
|
+
def favoritors(scope: ActsAsFavoritor.configuration.default_scope,
|
50
|
+
scopes: nil)
|
51
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
52
|
+
favorited.includes(:favoritor).unblocked.send("#{s}_list")
|
50
53
|
.map(&:favoritor)
|
51
54
|
end
|
52
55
|
end
|
53
56
|
|
54
57
|
def favoritors_by_type(favoritor_type,
|
55
|
-
|
56
|
-
|
57
|
-
self.class.build_result_for_scopes
|
58
|
-
|
59
|
-
|
58
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
59
|
+
scopes: nil)
|
60
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
61
|
+
favoritor_type.constantize.includes(:favorites)
|
62
|
+
.where(favorites: {
|
63
|
+
blocked: false, favoritable_id: id,
|
64
|
+
favoritable_type: self.class.name, scope: s
|
65
|
+
})
|
60
66
|
end
|
61
67
|
end
|
62
68
|
|
63
69
|
def favorited_by?(favoritor,
|
64
|
-
|
65
|
-
|
66
|
-
|
70
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
71
|
+
scopes: nil)
|
72
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
73
|
+
favorited.unblocked.send("#{s}_list").for_favoritor(favoritor)
|
67
74
|
.first.present?
|
68
75
|
end
|
69
76
|
end
|
70
77
|
|
71
|
-
def block(favoritor,
|
72
|
-
scopes:
|
73
|
-
self.class.build_result_for_scopes
|
74
|
-
get_favorite_for(favoritor,
|
78
|
+
def block(favoritor, scope: ActsAsFavoritor.configuration.default_scope,
|
79
|
+
scopes: nil)
|
80
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
81
|
+
get_favorite_for(favoritor, s)&.block! || Favorite.create(
|
75
82
|
favoritable: self,
|
76
83
|
favoritor: favoritor,
|
77
84
|
blocked: true,
|
@@ -80,24 +87,26 @@ module ActsAsFavoritor
|
|
80
87
|
end
|
81
88
|
end
|
82
89
|
|
83
|
-
def unblock(favoritor,
|
84
|
-
scopes:
|
85
|
-
self.class.build_result_for_scopes
|
86
|
-
get_favorite_for(favoritor,
|
90
|
+
def unblock(favoritor, scope: ActsAsFavoritor.configuration.default_scope,
|
91
|
+
scopes: nil)
|
92
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
93
|
+
get_favorite_for(favoritor, s)&.update(blocked: false)
|
87
94
|
end
|
88
95
|
end
|
89
96
|
|
90
97
|
def blocked?(favoritor,
|
91
|
-
|
92
|
-
|
93
|
-
|
98
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
99
|
+
scopes: nil)
|
100
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
101
|
+
favorited.blocked.send("#{s}_list").for_favoritor(favoritor).first
|
94
102
|
.present?
|
95
103
|
end
|
96
104
|
end
|
97
105
|
|
98
|
-
def blocked(
|
99
|
-
|
100
|
-
|
106
|
+
def blocked(scope: ActsAsFavoritor.configuration.default_scope,
|
107
|
+
scopes: nil)
|
108
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
109
|
+
favorited.includes(:favoritor).blocked.send("#{s}_list")
|
101
110
|
.map(&:favoritor)
|
102
111
|
end
|
103
112
|
end
|
@@ -12,9 +12,11 @@ module ActsAsFavoritor
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
# rubocop:disable Style/OptionalBooleanParameter
|
15
16
|
def respond_to_missing?(method, include_private = false)
|
16
17
|
super || method.to_s[/(.+)_list/]
|
17
18
|
end
|
19
|
+
# rubocop:enable Style/OptionalBooleanParameter
|
18
20
|
|
19
21
|
def for_favoritor(favoritor)
|
20
22
|
where(
|
@@ -38,85 +38,97 @@ module ActsAsFavoritor
|
|
38
38
|
end
|
39
39
|
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
40
40
|
|
41
|
+
# rubocop:disable Style/OptionalBooleanParameter
|
41
42
|
def respond_to_missing?(method, include_private = false)
|
42
43
|
super || method.to_s[/favorited_(.+)/] ||
|
43
44
|
method.to_s[/favoritor_(.+)_score/] ||
|
44
45
|
method.to_s[/favoritor_(.+)_total/]
|
45
46
|
end
|
47
|
+
# rubocop:enable Style/OptionalBooleanParameter
|
46
48
|
|
47
49
|
def favorite(favoritable,
|
48
|
-
|
49
|
-
|
50
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
51
|
+
scopes: nil)
|
52
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
50
53
|
return nil if self == favoritable
|
51
54
|
|
52
|
-
inc_cache if ActsAsFavoritor.configuration.cache
|
55
|
+
inc_cache(favoritable, s) if ActsAsFavoritor.configuration.cache
|
53
56
|
|
54
|
-
favorites.for_favoritable(favoritable).send("#{
|
57
|
+
favorites.for_favoritable(favoritable).send("#{s}_list")
|
55
58
|
.first_or_create!
|
56
59
|
end
|
57
60
|
end
|
58
61
|
|
59
62
|
def unfavorite(favoritable,
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
64
|
+
scopes: nil)
|
65
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
66
|
+
favorite_record = get_favorite(favoritable, s)
|
63
67
|
return nil unless favorite_record.present?
|
64
68
|
|
65
|
-
dec_cache if ActsAsFavoritor.configuration.cache
|
69
|
+
dec_cache(favoritable, s) if ActsAsFavoritor.configuration.cache
|
66
70
|
favorite_record.destroy!
|
67
71
|
end
|
68
72
|
end
|
69
73
|
|
70
74
|
def favorited?(favoritable,
|
71
|
-
|
72
|
-
|
73
|
-
|
75
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
76
|
+
scopes: nil)
|
77
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
78
|
+
Favorite.unblocked.send("#{s}_list").for_favoritor(self)
|
74
79
|
.for_favoritable(favoritable).size.positive?
|
75
80
|
end
|
76
81
|
end
|
77
82
|
|
78
|
-
def all_favorites(
|
79
|
-
|
80
|
-
|
83
|
+
def all_favorites(scope: ActsAsFavoritor.configuration.default_scope,
|
84
|
+
scopes: nil)
|
85
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
86
|
+
favorites.unblocked.send("#{s}_list")
|
81
87
|
end
|
82
88
|
end
|
83
89
|
|
84
|
-
def all_favorited(
|
85
|
-
|
86
|
-
|
90
|
+
def all_favorited(scope: ActsAsFavoritor.configuration.default_scope,
|
91
|
+
scopes: nil)
|
92
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
93
|
+
favorites.unblocked.send("#{s}_list").includes(:favoritable)
|
87
94
|
.map(&:favoritable)
|
88
95
|
end
|
89
96
|
end
|
90
97
|
|
91
98
|
def favorites_by_type(favoritable_type,
|
92
|
-
|
93
|
-
|
94
|
-
self.class.build_result_for_scopes
|
95
|
-
favorites.unblocked.send("#{
|
99
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
100
|
+
scopes: nil)
|
101
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
102
|
+
favorites.unblocked.send("#{s}_list")
|
96
103
|
.for_favoritable_type(favoritable_type)
|
97
104
|
end
|
98
105
|
end
|
99
106
|
|
100
107
|
def favorited_by_type(favoritable_type,
|
101
|
-
|
102
|
-
|
103
|
-
self.class.build_result_for_scopes
|
104
|
-
|
105
|
-
|
108
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
109
|
+
scopes: nil)
|
110
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
111
|
+
favoritable_type.constantize.includes(:favorited)
|
112
|
+
.where(favorites: {
|
113
|
+
blocked: false, favoritor_id: id,
|
114
|
+
favoritor_type: self.class.name, scope: s
|
115
|
+
})
|
106
116
|
end
|
107
117
|
end
|
108
118
|
|
109
119
|
def blocked_by?(favoritable,
|
110
|
-
|
111
|
-
|
112
|
-
|
120
|
+
scope: ActsAsFavoritor.configuration.default_scope,
|
121
|
+
scopes: nil)
|
122
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
123
|
+
Favorite.blocked.send("#{s}_list").for_favoritor(self)
|
113
124
|
.for_favoritable(favoritable).size.positive?
|
114
125
|
end
|
115
126
|
end
|
116
127
|
|
117
|
-
def blocked_by(
|
118
|
-
|
119
|
-
|
128
|
+
def blocked_by(scope: ActsAsFavoritor.configuration.default_scope,
|
129
|
+
scopes: nil)
|
130
|
+
self.class.build_result_for_scopes(scopes || scope) do |s|
|
131
|
+
favorites.includes(:favoritable).blocked.send("#{s}_list")
|
120
132
|
.map(&:favoritable)
|
121
133
|
end
|
122
134
|
end
|
@@ -129,7 +141,7 @@ module ActsAsFavoritor
|
|
129
141
|
end
|
130
142
|
|
131
143
|
# rubocop:disable Metrics/AbcSize
|
132
|
-
def inc_cache
|
144
|
+
def inc_cache(favoritable, scope)
|
133
145
|
favoritor_score[scope] = (favoritor_score[scope] || 0) + 1
|
134
146
|
favoritor_total[scope] = (favoritor_total[scope] || 0) + 1
|
135
147
|
save!
|
@@ -141,16 +153,14 @@ module ActsAsFavoritor
|
|
141
153
|
favoritable.save!
|
142
154
|
end
|
143
155
|
|
144
|
-
def dec_cache
|
156
|
+
def dec_cache(favoritable, scope)
|
145
157
|
favoritor_score[scope] = (favoritor_score[scope] || 0) - 1
|
146
158
|
favoritor_score.delete(scope) unless favoritor_score[scope].positive?
|
147
159
|
save!
|
148
160
|
|
149
161
|
favoritable.favoritable_score[scope] =
|
150
162
|
(favoritable.favoritable_score[scope] || 0) - 1
|
151
|
-
# rubocop:disable Metrics/LineLength
|
152
163
|
favoritable.favoritable_score.delete(scope) unless favoritable.favoritable_score[scope].positive?
|
153
|
-
# rubocop:enable Metrics/LineLength
|
154
164
|
favoritable.save!
|
155
165
|
end
|
156
166
|
# rubocop:enable Metrics/AbcSize
|
@@ -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
|
@@ -13,7 +13,8 @@ class ActsAsFavoritorGenerator < Rails::Generators::Base
|
|
13
13
|
if ActiveRecord::Base.timestamped_migrations
|
14
14
|
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
15
15
|
else
|
16
|
-
format('
|
16
|
+
format('%<migration_number>.3d',
|
17
|
+
migration_number: current_migration_number(dirname) + 1)
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
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.1
|
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-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
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'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rails
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -145,14 +159,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
145
159
|
requirements:
|
146
160
|
- - ">="
|
147
161
|
- !ruby/object:Gem::Version
|
148
|
-
version: '2.
|
162
|
+
version: '2.5'
|
149
163
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
164
|
requirements:
|
151
165
|
- - ">="
|
152
166
|
- !ruby/object:Gem::Version
|
153
167
|
version: '0'
|
154
168
|
requirements: []
|
155
|
-
rubygems_version: 3.
|
169
|
+
rubygems_version: 3.1.4
|
156
170
|
signing_key:
|
157
171
|
specification_version: 4
|
158
172
|
summary: A Rubygem to add Favorite, Follow, Vote, etc. functionality to ActiveRecord
|