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