acts_as_favoritor 2.1.2 → 3.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 +8 -45
- data/lib/acts_as_favoritor.rb +3 -3
- data/lib/acts_as_favoritor/favoritable.rb +53 -334
- data/lib/acts_as_favoritor/favoritor.rb +111 -484
- data/lib/acts_as_favoritor/favoritor_lib.rb +11 -26
- data/lib/acts_as_favoritor/version.rb +1 -1
- data/lib/generators/templates/model.rb +1 -1
- metadata +13 -13
@@ -8,547 +8,174 @@ module ActsAsFavoritor
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
def acts_as_favoritor
|
11
|
+
if ActsAsFavoritor.configuration&.cache
|
12
|
+
serialize :favoritor_score, Hash
|
13
|
+
serialize :favoritor_total, Hash
|
14
|
+
end
|
15
|
+
|
11
16
|
has_many :favorites, as: :favoritor, dependent: :destroy
|
17
|
+
|
12
18
|
include ActsAsFavoritor::Favoritor::InstanceMethods
|
13
19
|
include ActsAsFavoritor::FavoritorLib
|
14
|
-
|
15
|
-
return unless ActsAsFavoritor.configuration&.cache
|
16
|
-
|
17
|
-
serialize :favoritor_score, Hash
|
18
|
-
serialize :favoritor_total, Hash
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
23
|
+
# rubocop:disable Metrics/ModuleLength
|
22
24
|
module InstanceMethods
|
23
|
-
#
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
.for_favoritor(self)
|
34
|
-
.for_favoritable(favoritable).count
|
35
|
-
.positive?
|
36
|
-
end
|
37
|
-
results
|
25
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
26
|
+
def method_missing(method, *args)
|
27
|
+
if method.to_s[/favorited_(.+)/]
|
28
|
+
favorited_by_type($1.singularize.classify)
|
29
|
+
elsif ActsAsFavoritor.configuration.cache &&
|
30
|
+
method.to_s[/favoritor_(.+)_score/]
|
31
|
+
favoritor_score[$1.singularize.classify]
|
32
|
+
elsif ActsAsFavoritor.configuration.cache &&
|
33
|
+
method.to_s[/favoritor_(.+)_total/]
|
34
|
+
favoritor_total[$1.singularize.classify]
|
38
35
|
else
|
39
|
-
|
40
|
-
.for_favoritor(self).for_favoritable(favoritable)
|
41
|
-
.count.positive?
|
36
|
+
super
|
42
37
|
end
|
43
38
|
end
|
39
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
44
40
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
options[:parameter] = favoritable
|
50
|
-
validate_scopes(__method__, options)
|
51
|
-
elsif options[:multiple_scopes]
|
52
|
-
results = {}
|
53
|
-
options[:scope].each do |scope|
|
54
|
-
results[scope] = Favorite.blocked.send(scope + '_list')
|
55
|
-
.for_favoritor(self)
|
56
|
-
.for_favoritable(favoritable).count
|
57
|
-
.positive?
|
58
|
-
end
|
59
|
-
results
|
60
|
-
else
|
61
|
-
Favorite.blocked.send(options[:scope] + '_list').for_favoritor(self)
|
62
|
-
.for_favoritable(favoritable).count.positive?
|
63
|
-
end
|
41
|
+
def respond_to_missing?(method, include_private = false)
|
42
|
+
super || method.to_s[/favorited_(.+)/] ||
|
43
|
+
method.to_s[/favoritor_(.+)_score/] ||
|
44
|
+
method.to_s[/favoritor_(.+)_total/]
|
64
45
|
end
|
65
46
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
if options.key?(:multiple_scopes) == false
|
72
|
-
options[:parameter] = favoritable
|
73
|
-
validate_scopes(__method__, options)
|
74
|
-
elsif options[:multiple_scopes]
|
75
|
-
results = {}
|
76
|
-
options[:scope].each do |scope|
|
77
|
-
if Favorite.unblocked.send(scope + '_list').for_favoritor(self)
|
78
|
-
.for_favoritable(favoritable).count.positive?
|
79
|
-
results[scope] = true
|
80
|
-
elsif Favorite.blocked.send(scope + '_list').for_favoritor(self)
|
81
|
-
.for_favoritable(favoritable).count.positive?
|
82
|
-
results[scope] = false
|
83
|
-
else
|
84
|
-
results[scope] = nil
|
85
|
-
end
|
86
|
-
end
|
87
|
-
results
|
88
|
-
elsif Favorite.unblocked.send(options[:scope] + '_list')
|
89
|
-
.for_favoritor(self).for_favoritable(favoritable).count
|
90
|
-
.positive?
|
91
|
-
true
|
92
|
-
elsif Favorite.blocked.send(options[:scope] + '_list')
|
93
|
-
.for_favoritor(self).for_favoritable(favoritable).count
|
94
|
-
.positive?
|
95
|
-
false
|
96
|
-
end
|
97
|
-
end
|
47
|
+
def favorite(favoritable,
|
48
|
+
scopes: [ActsAsFavoritor.configuration.default_scope])
|
49
|
+
build_result_for_scopes scopes do |scope|
|
50
|
+
return if self == favoritable
|
51
|
+
return if scope == :all
|
98
52
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
results[scope] = Favorite.unblocked.send(scope + '_list')
|
107
|
-
.for_favoritor(self).count
|
108
|
-
end
|
109
|
-
results
|
110
|
-
else
|
111
|
-
Favorite.unblocked.send(options[:scope] + '_list')
|
112
|
-
.for_favoritor(self).count
|
53
|
+
inc_cache if ActsAsFavoritor.configuration.cache
|
54
|
+
|
55
|
+
favorites.where(
|
56
|
+
favoritable_id: favoritable.id,
|
57
|
+
favoritable_type: parent_class_name(favoritable),
|
58
|
+
scope: scope
|
59
|
+
).first_or_create!
|
113
60
|
end
|
114
61
|
end
|
115
62
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
options[:parameter] = favoritable
|
121
|
-
validate_scopes(__method__, options)
|
122
|
-
elsif options[:multiple_scopes]
|
123
|
-
results = {}
|
124
|
-
options[:scope].each do |scope|
|
125
|
-
if ActsAsFavoritor.configuration.cache
|
126
|
-
favoritor_score[scope] = (favoritor_score[scope] || 0) + 1
|
127
|
-
favoritor_total[scope] = (favoritor_total[scope] || 0) + 1
|
128
|
-
save!
|
129
|
-
favoritable.favoritable_score[scope] =
|
130
|
-
(favoritable.favoritable_score[scope] || 0) + 1
|
131
|
-
favoritable.favoritable_total[scope] =
|
132
|
-
(favoritable.favoritable_total[scope] || 0) + 1
|
133
|
-
favoritable.save!
|
134
|
-
end
|
135
|
-
next unless self != favoritable && scope != 'all'
|
63
|
+
def remove_favorite(favoritable,
|
64
|
+
scopes: [ActsAsFavoritor.configuration.default_scope])
|
65
|
+
build_result_for_scopes scopes do |scope|
|
66
|
+
dec_cache if ActsAsFavoritor.configuration.cache
|
136
67
|
|
137
|
-
|
138
|
-
favoritable_id: favoritable.id,
|
139
|
-
favoritable_type: parent_class_name(favoritable),
|
140
|
-
scope: scope
|
141
|
-
}
|
142
|
-
results[scope] = favorites.where(params).first_or_create!
|
143
|
-
end
|
144
|
-
results
|
145
|
-
else
|
146
|
-
if ActsAsFavoritor.configuration.cache
|
147
|
-
favoritor_score[options[:scope]] =
|
148
|
-
(favoritor_score[options[:scope]] || 0) + 1
|
149
|
-
favoritor_total[options[:scope]] =
|
150
|
-
(favoritor_total[options[:scope]] || 0) + 1
|
151
|
-
save!
|
152
|
-
favoritable.favoritable_score[options[:scope]] =
|
153
|
-
(favoritable.favoritable_score[options[:scope]] || 0) + 1
|
154
|
-
favoritable.favoritable_total[options[:scope]] =
|
155
|
-
(favoritable.favoritable_total[options[:scope]] || 0) + 1
|
156
|
-
favoritable.save!
|
157
|
-
end
|
158
|
-
if self != favoritable && options[:scope] != 'all'
|
159
|
-
params = {
|
160
|
-
favoritable_id: favoritable.id,
|
161
|
-
favoritable_type: parent_class_name(favoritable),
|
162
|
-
scope: options[:scope]
|
163
|
-
}
|
164
|
-
favorites.where(params).first_or_create!
|
165
|
-
end
|
68
|
+
get_favorite(favoritable, scope)&.destroy
|
166
69
|
end
|
167
70
|
end
|
168
71
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
elsif options[:multiple_scopes]
|
175
|
-
results = {}
|
176
|
-
options[:scope].each do |scope|
|
177
|
-
if ActsAsFavoritor.configuration.cache
|
178
|
-
favoritor_score[scope] = favoritor_score[scope] - 1
|
179
|
-
unless favoritor_score[scope].positive?
|
180
|
-
favoritor_score.delete(scope)
|
181
|
-
end
|
182
|
-
save!
|
183
|
-
favoritable.favoritable_score[scope] =
|
184
|
-
favoritable.favoritable_score[scope] - 1
|
185
|
-
unless favoritable.favoritable_score[scope].positive?
|
186
|
-
favoritable.favoritable_score.delete(scope)
|
187
|
-
end
|
188
|
-
favoritable.save!
|
189
|
-
end
|
190
|
-
favorite = get_favorite(
|
191
|
-
favoritable, scope: scope, multiple_scopes: false
|
192
|
-
)
|
193
|
-
results[scope] = favorite.destroy if favorite
|
194
|
-
end
|
195
|
-
results
|
196
|
-
else
|
197
|
-
if ActsAsFavoritor.configuration.cache
|
198
|
-
favoritor_score[options[:scope]] =
|
199
|
-
favoritor_score[options[:scope]] - 1
|
200
|
-
unless favoritor_score[options[:scope]].positive?
|
201
|
-
favoritor_score.delete(options[:scope])
|
202
|
-
end
|
203
|
-
save!
|
204
|
-
favoritable.favoritable_score[options[:scope]] =
|
205
|
-
favoritable.favoritable_score[options[:scope]] - 1
|
206
|
-
unless favoritable.favoritable_score[options[:scope]].positive?
|
207
|
-
favoritable.favoritable_score.delete(options[:scope])
|
208
|
-
end
|
209
|
-
favoritable.save!
|
210
|
-
end
|
211
|
-
favorite = get_favorite(
|
212
|
-
favoritable, scope: options[:scope], multiple_scopes: false
|
213
|
-
)
|
214
|
-
favorite&.destroy
|
72
|
+
def favorited?(favoritable,
|
73
|
+
scopes: [ActsAsFavoritor.configuration.default_scope])
|
74
|
+
build_result_for_scopes scopes do |scope|
|
75
|
+
Favorite.unblocked.send("#{scope}_list").for_favoritor(self)
|
76
|
+
.for_favoritable(favoritable).size.positive?
|
215
77
|
end
|
216
78
|
end
|
217
79
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
validate_scopes(__method__, options)
|
222
|
-
elsif options[:multiple_scopes]
|
223
|
-
results = {}
|
224
|
-
options[:scope].each do |scope|
|
225
|
-
results[scope] = favorites.unblocked.send(scope + '_list')
|
226
|
-
.includes(:favoritable)
|
227
|
-
end
|
228
|
-
results
|
229
|
-
else
|
230
|
-
favorites.unblocked.send(options[:scope] + '_list')
|
231
|
-
.includes(:favoritable)
|
80
|
+
def all_favorites(scopes: [ActsAsFavoritor.configuration.default_scope])
|
81
|
+
build_result_for_scopes scopes do |scope|
|
82
|
+
favorites.unblocked.send("#{scope}_list")
|
232
83
|
end
|
233
84
|
end
|
234
85
|
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
validate_scopes(__method__, options)
|
240
|
-
elsif options[:multiple_scopes]
|
241
|
-
results = {}
|
242
|
-
options[:scope].each do |scope|
|
243
|
-
favorites_scope = favorites_scoped(
|
244
|
-
scope: scope, multiple_scopes: false
|
245
|
-
).for_favoritable_type(favoritable_type)
|
246
|
-
results[scope] = apply_options_to_scope(
|
247
|
-
favorites_scope, options
|
248
|
-
)
|
249
|
-
end
|
250
|
-
results
|
251
|
-
else
|
252
|
-
favorites_scope = favorites_scoped(
|
253
|
-
scope: options[:scope], multiple_scopes: false
|
254
|
-
).for_favoritable_type(favoritable_type)
|
255
|
-
apply_options_to_scope(
|
256
|
-
favorites_scope, options
|
257
|
-
)
|
86
|
+
def all_favorited(scopes: [ActsAsFavoritor.configuration.default_scope])
|
87
|
+
build_result_for_scopes scopes do |scope|
|
88
|
+
favorites.unblocked.send("#{scope}_list").includes(:favoritable)
|
89
|
+
.map(&:favoritable)
|
258
90
|
end
|
259
91
|
end
|
260
92
|
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
results = {}
|
268
|
-
options[:scope].each do |scope|
|
269
|
-
favorites_scope = favorites_scoped(
|
270
|
-
scope: scope, multiple_scopes: false
|
271
|
-
)
|
272
|
-
results[scope] = apply_options_to_scope(
|
273
|
-
favorites_scope, options
|
274
|
-
)
|
275
|
-
end
|
276
|
-
results
|
277
|
-
else
|
278
|
-
favorites_scope = favorites_scoped(
|
279
|
-
scope: options[:scope], multiple_scopes: false
|
280
|
-
)
|
281
|
-
apply_options_to_scope(
|
282
|
-
favorites_scope, options
|
283
|
-
)
|
93
|
+
def favorites_by_type(favoritable_type,
|
94
|
+
scopes: [ActsAsFavoritor.configuration
|
95
|
+
.default_scope])
|
96
|
+
build_result_for_scopes scopes do |scope|
|
97
|
+
favorites.unblocked.send("#{scope}_list").includes(:favoritable)
|
98
|
+
.for_favoritable_type(favoritable_type)
|
284
99
|
end
|
285
100
|
end
|
286
101
|
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
options[:scope].each do |scope|
|
294
|
-
results[scope] = all_favorites(options).collect(&:favoritable)
|
295
|
-
end
|
296
|
-
results
|
297
|
-
else
|
298
|
-
all_favorites(options).collect(&:favoritable)
|
102
|
+
def favorited_by_type(favoritable_type,
|
103
|
+
scopes: [ActsAsFavoritor.configuration
|
104
|
+
.default_scope])
|
105
|
+
build_result_for_scopes scopes do |scope|
|
106
|
+
favorites.unblocked.send("#{scope}_list")
|
107
|
+
.for_favoritable_type(favoritable_type).map(&:favoritable)
|
299
108
|
end
|
300
109
|
end
|
301
110
|
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
options[:scope].each do |scope|
|
311
|
-
favoritables = favoritable_type.constantize.joins(:favorited)
|
312
|
-
favoritables = favoritables.where(
|
313
|
-
'favorites.blocked': false,
|
314
|
-
'favorites.favoritor_id': id,
|
315
|
-
'favorites.favoritor_type': parent_class_name(self),
|
316
|
-
'favorites.favoritable_type': favoritable_type,
|
317
|
-
'favorites.scope': scope
|
318
|
-
)
|
319
|
-
if options.key?(:limit)
|
320
|
-
favoritables = favoritables.limit options[:limit]
|
321
|
-
end
|
322
|
-
if options.key?(:includes)
|
323
|
-
favoritables = favoritables.includes options[:includes]
|
324
|
-
end
|
325
|
-
results[scope] = favoritables
|
326
|
-
end
|
327
|
-
results
|
328
|
-
else
|
329
|
-
favoritables = favoritable_type.constantize.joins(:favorited)
|
330
|
-
favoritables = favoritables.where(
|
331
|
-
'favorites.blocked': false,
|
332
|
-
'favorites.favoritor_id': id,
|
333
|
-
'favorites.favoritor_type': parent_class_name(self),
|
334
|
-
'favorites.favoritable_type': favoritable_type,
|
335
|
-
'favorites.scope': options[:scope]
|
111
|
+
def block(favoritable,
|
112
|
+
scopes: [ActsAsFavoritor.configuration.default_scope])
|
113
|
+
build_result_for_scopes scopes do |scope|
|
114
|
+
get_favorite(favoritable, scope).block! || Favorite.create(
|
115
|
+
favoritable: favoritable,
|
116
|
+
favoritor: self,
|
117
|
+
blocked: true,
|
118
|
+
scope: scope
|
336
119
|
)
|
337
|
-
if options.key? :limit
|
338
|
-
favoritables = favoritables.limit options[:limit]
|
339
|
-
end
|
340
|
-
if options.key? :includes
|
341
|
-
favoritables = favoritables.includes options[:includes]
|
342
|
-
end
|
343
|
-
favoritables
|
344
120
|
end
|
345
121
|
end
|
346
122
|
|
347
|
-
def
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
elsif options[:multiple_scopes]
|
352
|
-
results = {}
|
353
|
-
options[:scope].each do |scope|
|
354
|
-
results[scope] = favorites.unblocked
|
355
|
-
.send(scope + '_list')
|
356
|
-
.for_favoritable_type(favoritable_type)
|
357
|
-
.count
|
358
|
-
end
|
359
|
-
results
|
360
|
-
else
|
361
|
-
favorites.unblocked.send(options[:scope] + '_list')
|
362
|
-
.for_favoritable_type(favoritable_type).count
|
123
|
+
def unblock(favoritable,
|
124
|
+
scopes: [ActsAsFavoritor.configuration.default_scope])
|
125
|
+
build_result_for_scopes scopes do |scope|
|
126
|
+
get_favorite(favoritable, scope)&.update(blocked: false)
|
363
127
|
end
|
364
128
|
end
|
365
129
|
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
if method.to_s[/favorited_(.+)_count/]
|
372
|
-
favorited_by_type_count $1.singularize.classify
|
373
|
-
elsif method.to_s[/favorited_(.+)/]
|
374
|
-
favorited_by_type $1.singularize.classify
|
375
|
-
elsif ActsAsFavoritor.configuration.cache &&
|
376
|
-
method.to_s[/favoritor_(.+)_score/]
|
377
|
-
favoritor_score[$1.singularize.classify]
|
378
|
-
elsif ActsAsFavoritor.configuration.cache &&
|
379
|
-
method.to_s[/favoritor_(.+)_total/]
|
380
|
-
favoritor_total[$1.singularize.classify]
|
381
|
-
else
|
382
|
-
super
|
130
|
+
def blocked?(favoritable,
|
131
|
+
scopes: [ActsAsFavoritor.configuration.default_scope])
|
132
|
+
build_result_for_scopes scopes do |scope|
|
133
|
+
Favorite.blocked.send("#{scope}_list").for_favoritor(self)
|
134
|
+
.for_favoritable(favoritable).size.positive?
|
383
135
|
end
|
384
136
|
end
|
385
137
|
|
386
|
-
def
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
# Returns a favorite record for the current instance and favoritable
|
392
|
-
# object.
|
393
|
-
def get_favorite(favoritable, options = {})
|
394
|
-
if options.key?(:multiple_scopes) == false
|
395
|
-
options[:parameter] = favoritable
|
396
|
-
validate_scopes(__method__, options)
|
397
|
-
elsif options[:multiple_scopes]
|
398
|
-
results = {}
|
399
|
-
options[:scope].each do |scope|
|
400
|
-
results[scope] = favorites.unblocked.send(scope.to_s + '_list')
|
401
|
-
.for_favoritable(favoritable).first
|
402
|
-
end
|
403
|
-
results
|
404
|
-
else
|
405
|
-
favorites.unblocked.send(options[:scope].to_s + '_list')
|
406
|
-
.for_favoritable(favoritable).first
|
138
|
+
def blocks(scopes: [ActsAsFavoritor.configuration.default_scope])
|
139
|
+
build_result_for_scopes scopes do |scope|
|
140
|
+
favorites.includes(:favoritable).blocked.send("#{scope}_list")
|
141
|
+
.map(&:favoritable)
|
407
142
|
end
|
408
143
|
end
|
409
144
|
|
410
|
-
|
411
|
-
if options.key?(:multiple_scopes) == false
|
412
|
-
validate_scopes(__method__, options)
|
413
|
-
elsif options[:multiple_scopes]
|
414
|
-
results = {}
|
415
|
-
options[:scope].each do |scope|
|
416
|
-
blocked_favoritors_scope = favoritables_scoped(
|
417
|
-
scope: scope, multiple_scopes: false
|
418
|
-
).blocked
|
419
|
-
blocked_favoritors_scope = apply_options_to_scope(
|
420
|
-
blocked_favoritors_scope, options
|
421
|
-
)
|
422
|
-
results[scope] = blocked_favoritors_scope.to_a
|
423
|
-
.collect(&:favoritable)
|
424
|
-
end
|
425
|
-
results
|
426
|
-
else
|
427
|
-
blocked_favoritors_scope = favoritors_scoped(
|
428
|
-
scope: options[:scope], multiple_scopes: false
|
429
|
-
).blocked
|
430
|
-
blocked_favoritors_scope = apply_options_to_scope(
|
431
|
-
blocked_favoritors_scope, options
|
432
|
-
)
|
433
|
-
blocked_favoritors_scope.to_a.collect(&:favoritable)
|
434
|
-
end
|
435
|
-
end
|
145
|
+
private
|
436
146
|
|
437
|
-
def
|
438
|
-
|
439
|
-
|
440
|
-
validate_scopes(__method__, options)
|
441
|
-
elsif options[:multiple_scopes]
|
442
|
-
results = {}
|
443
|
-
options[:scope].each do |scope|
|
444
|
-
favorite = get_favorite(
|
445
|
-
favoritable, scope: scope, multiple_scopes: false
|
446
|
-
)
|
447
|
-
if favorite
|
448
|
-
results[scope] = block_existing_favorite(
|
449
|
-
favoritable, scope: scope, multiple_scopes: false
|
450
|
-
)
|
451
|
-
else
|
452
|
-
results[scope] = block_future_favorite(
|
453
|
-
favoritable, scope: scope, multiple_scopes: false
|
454
|
-
)
|
455
|
-
end
|
456
|
-
end
|
457
|
-
results
|
458
|
-
else
|
459
|
-
favorite = get_favorite(
|
460
|
-
favoritable, scope: options[:scope], multiple_scopes: false
|
461
|
-
)
|
462
|
-
if favorite
|
463
|
-
block_existing_favorite(
|
464
|
-
favoritable, scope: options[:scope], multiple_scopes: false
|
465
|
-
)
|
466
|
-
else
|
467
|
-
block_future_favorite(
|
468
|
-
favoritable, scope: options[:scope], multiple_scopes: false
|
469
|
-
)
|
470
|
-
end
|
471
|
-
end
|
147
|
+
def get_favorite(favoritable, scope)
|
148
|
+
favorites.unblocked.send("#{scope}_list").for_favoritable(favoritable)
|
149
|
+
.first
|
472
150
|
end
|
473
151
|
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
results = {}
|
480
|
-
options[:scope].each do |scope|
|
481
|
-
results[scope] = get_favorite(
|
482
|
-
favoritable, scope: scope, multiple_scopes: false
|
483
|
-
)&.update_attribute :blocked, false
|
484
|
-
end
|
485
|
-
results
|
486
|
-
else
|
487
|
-
get_favorite(
|
488
|
-
favoritable, scope: options[:scope], multiple_scopes: false
|
489
|
-
)&.update_attribute :blocked, false
|
490
|
-
end
|
491
|
-
end
|
152
|
+
# rubocop:disable Metrics/AbcSize
|
153
|
+
def inc_cache
|
154
|
+
favoritor_score[scope] = (favoritor_score[scope] || 0) + 1
|
155
|
+
favoritor_total[scope] = (favoritor_total[scope] || 0) + 1
|
156
|
+
save!
|
492
157
|
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
options[:scope].each do |scope|
|
499
|
-
results[scope] = favorites.blocked.send(scope + '_list').count
|
500
|
-
end
|
501
|
-
results
|
502
|
-
else
|
503
|
-
favorites.blocked.send(options[:scope] + '_list').count
|
504
|
-
end
|
158
|
+
favoritable.favoritable_score[scope] =
|
159
|
+
(favoritable.favoritable_score[scope] || 0) + 1
|
160
|
+
favoritable.favoritable_total[scope] =
|
161
|
+
(favoritable.favoritable_total[scope] || 0) + 1
|
162
|
+
favoritable.save!
|
505
163
|
end
|
506
164
|
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
options[:parameter] = favoritable
|
512
|
-
validate_scopes(__method__, options)
|
513
|
-
elsif options[:multiple_scopes]
|
514
|
-
results = {}
|
515
|
-
options[:scope].each do |scope|
|
516
|
-
results[scope] = Favorite.create(
|
517
|
-
favoritable: favoritable,
|
518
|
-
favoritor: self,
|
519
|
-
blocked: true,
|
520
|
-
scope: scope
|
521
|
-
)
|
522
|
-
end
|
523
|
-
results
|
524
|
-
else
|
525
|
-
Favorite.create(
|
526
|
-
favoritable: favoritable,
|
527
|
-
favoritor: self,
|
528
|
-
blocked: true,
|
529
|
-
scope: options[:scope]
|
530
|
-
)
|
531
|
-
end
|
532
|
-
end
|
165
|
+
def dec_cache
|
166
|
+
favoritor_score[scope] = (favoritor_score[scope] || 0) - 1
|
167
|
+
favoritor_score.delete(scope) unless favoritor_score[scope].positive?
|
168
|
+
save!
|
533
169
|
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
options[:scope].each do |scope|
|
541
|
-
results[scope] = get_favorite(
|
542
|
-
favoritable, scope: scope, multiple_scopes: false
|
543
|
-
).block!
|
544
|
-
end
|
545
|
-
results
|
546
|
-
else
|
547
|
-
get_favorite(
|
548
|
-
favoritable, scope: options[:scope], multiple_scopes: false
|
549
|
-
).block!
|
550
|
-
end
|
170
|
+
favoritable.favoritable_score[scope] =
|
171
|
+
(favoritable.favoritable_score[scope] || 0) - 1
|
172
|
+
# rubocop:disable Metrics/LineLength
|
173
|
+
favoritable.favoritable_score.delete(scope) unless favoritable.favoritable_score[scope].positive?
|
174
|
+
# rubocop:enable Metrics/LineLength
|
175
|
+
favoritable.save!
|
551
176
|
end
|
177
|
+
# rubocop:enable Metrics/AbcSize
|
552
178
|
end
|
179
|
+
# rubocop:enable Metrics/ModuleLength
|
553
180
|
end
|
554
181
|
end
|