acts_as_favoritor 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE +1 -1
  4. data/README.md +1 -3
  5. data/lib/acts_as_favoritor.rb +2 -11
  6. data/lib/acts_as_favoritor/configuration.rb +10 -0
  7. data/lib/acts_as_favoritor/favoritable.rb +1 -1
  8. data/lib/acts_as_favoritor/favorite_scopes.rb +1 -1
  9. data/lib/acts_as_favoritor/favoritor.rb +425 -425
  10. data/lib/acts_as_favoritor/railtie.rb +1 -1
  11. data/lib/acts_as_favoritor/version.rb +1 -1
  12. data/lib/generators/templates/model.rb +1 -2
  13. metadata +3 -44
  14. data/.github/issue_template.md +0 -14
  15. data/.github/pull_request_template.md +0 -21
  16. data/.gitignore +0 -8
  17. data/.travis.yml +0 -3
  18. data/CODE_OF_CONDUCT.md +0 -46
  19. data/CONTRIBUTING.md +0 -1
  20. data/DEPRECATIONS.md +0 -5
  21. data/Gemfile +0 -9
  22. data/INSTALL.md +0 -3
  23. data/Rakefile +0 -16
  24. data/acts_as_favoritor.gemspec +0 -30
  25. data/init.rb +0 -1
  26. data/test/acts_as_favoritable_test.rb +0 -283
  27. data/test/acts_as_favoritor_test.rb +0 -224
  28. data/test/dummy30/Gemfile +0 -1
  29. data/test/dummy30/Rakefile +0 -4
  30. data/test/dummy30/app/models/application_record.rb +0 -3
  31. data/test/dummy30/app/models/band.rb +0 -4
  32. data/test/dummy30/app/models/band/punk.rb +0 -4
  33. data/test/dummy30/app/models/band/punk/pop_punk.rb +0 -4
  34. data/test/dummy30/app/models/custom_record.rb +0 -3
  35. data/test/dummy30/app/models/some.rb +0 -5
  36. data/test/dummy30/app/models/user.rb +0 -5
  37. data/test/dummy30/config.ru +0 -2
  38. data/test/dummy30/config/application.rb +0 -14
  39. data/test/dummy30/config/boot.rb +0 -10
  40. data/test/dummy30/config/database.yml +0 -6
  41. data/test/dummy30/config/environment.rb +0 -5
  42. data/test/dummy30/config/environments/development.rb +0 -7
  43. data/test/dummy30/config/environments/test.rb +0 -6
  44. data/test/dummy30/config/initializers/acts_as_favoritor.rb +0 -9
  45. data/test/dummy30/config/initializers/secret_token.rb +0 -1
  46. data/test/dummy30/config/initializers/session_store.rb +0 -1
  47. data/test/dummy30/config/locales/en.yml +0 -2
  48. data/test/dummy30/config/routes.rb +0 -2
  49. data/test/factories/bands.rb +0 -17
  50. data/test/factories/somes.rb +0 -9
  51. data/test/factories/users.rb +0 -13
  52. data/test/favorite_test.rb +0 -10
  53. data/test/schema.rb +0 -26
  54. data/test/test_helper.rb +0 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e92b39df88d3baeafffbe37c4b0bb1592a4670c3549b7b8283f81ee69587e36a
4
- data.tar.gz: f852795bafe8966bdd78c51bae2df98ddc68e90fced56192b78143ca77c8c4ad
3
+ metadata.gz: 1bda840e48829f86862b98f8bb559cd9d7310352af4edc26173d6b5b44ee815a
4
+ data.tar.gz: d7e99df4d9541794b1c1d2828e6c0b024625cbe5c5ddb51f1158714ff62eeb87
5
5
  SHA512:
6
- metadata.gz: e0bcce346eb37f9b0237435de80b842f2bf985490401a79cf6bde54446cca4d1446fbb9ef58e6074e61bdc7e0290d993b0ce811057695786a7d29e1503a7faeb
7
- data.tar.gz: 0ed91c5554faac7434268f1afd3b2cdd9575ab975fbaee2c7499e2af887a17c3c953f955d95f88e33e692dc4c3789a1b99955df4cd6eb52a6f44f7e360107747
6
+ metadata.gz: eeb4bdf4c2443059da696a58d65055d4f06e9b233d469a383f13a243239b4e19b61dfd9a039ab234c3af85dd2191d91fc72ce61b596a227752038c9a689a73e7
7
+ data.tar.gz: 9f92498cab6a74bbe770b6551aff0e77920a6e9e7f400174298a59578de80cc8d90b6442f80f6d772c2490732a1236e0e88cbe45db30b6aa3c7b19290d398220
data/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@
4
4
 
5
5
  * nothing yet
6
6
 
7
+ ### 2.0.1 - 2018-01-01
8
+
9
+ * bugfixes
10
+ * fixed error when generating files (without configuration becoming initialized)
11
+
7
12
  ### 2.0.0 - 2017-12-21
8
13
 
9
14
  * features
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017 Slooob
3
+ Copyright (c) 2017 Jonas Hübotter
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -59,8 +59,6 @@ To wrap things up, migrate the changes into your database:
59
59
 
60
60
  $ rails db:migrate
61
61
 
62
- This will create a Favorite model as well as a migration file.
63
-
64
62
  ## Usage
65
63
 
66
64
  ### Setup
@@ -312,7 +310,7 @@ book.favoritable_favorite_cache # => 1
312
310
 
313
311
  ## Configuration
314
312
 
315
- You can configure devise-onesignal by passing a block to `configure`. This can be done in `config/initializers/devise-onesignal.rb`:
313
+ You can configure acts_as_favoritor by passing a block to `configure`. This can be done in `config/initializers/acts_as_favoritor.rb`:
316
314
 
317
315
  ```ruby
318
316
  ActsAsFavoritor.configure do |config|
@@ -2,22 +2,13 @@ require 'acts_as_favoritor/version'
2
2
 
3
3
  module ActsAsFavoritor
4
4
 
5
- autoload :Configuration, 'acts_as_favoritor/configuration'
6
-
7
- class << self
8
- attr_accessor :configuration
9
- end
10
-
11
- def self.configure
12
- self.configuration ||= Configuration.new
13
- yield configuration
14
- end
5
+ require 'acts_as_favoritor/configuration'
15
6
 
16
7
  autoload :Favoritor, 'acts_as_favoritor/favoritor'
17
8
  autoload :Favoritable, 'acts_as_favoritor/favoritable'
18
9
  autoload :FavoritorLib, 'acts_as_favoritor/favoritor_lib'
19
10
  autoload :FavoriteScopes, 'acts_as_favoritor/favorite_scopes'
20
11
 
21
- require 'acts_as_favoritor/railtie' if defined?(Rails)
12
+ require 'acts_as_favoritor/railtie' if defined?(Rails::Railtie)
22
13
 
23
14
  end
@@ -1,4 +1,14 @@
1
1
  module ActsAsFavoritor
2
+
3
+ class << self
4
+ attr_accessor :configuration
5
+ end
6
+
7
+ def self.configure
8
+ self.configuration ||= Configuration.new
9
+ yield configuration
10
+ end
11
+
2
12
  class Configuration
3
13
 
4
14
  attr_accessor :default_scope
@@ -1,4 +1,4 @@
1
- module ActsAsFavoritor #:nodoc:
1
+ module ActsAsFavoritor
2
2
  module Favoritable
3
3
 
4
4
  def self.included base
@@ -1,4 +1,4 @@
1
- module ActsAsFavoritor #:nodoc:
1
+ module ActsAsFavoritor
2
2
  module FavoriteScopes
3
3
 
4
4
  # Allows magic names on send(scope + '_list') - returns favorite records of certain scope
@@ -1,425 +1,425 @@
1
- module ActsAsFavoritor #:nodoc:
2
- module Favoritor
3
-
4
- def self.included base
5
- base.extend ClassMethods
6
- end
7
-
8
- module ClassMethods
9
- def acts_as_favoritor
10
- has_many :favorites, as: :favoritor, dependent: :destroy
11
- include ActsAsFavoritor::Favoritor::InstanceMethods
12
- include ActsAsFavoritor::FavoritorLib
13
-
14
- serialize :favoritor_cache, Hash if ActsAsFavoritor.configuration.cache
15
- end
16
- end
17
-
18
- module InstanceMethods
19
-
20
- # Returns true if this instance has favorited the object passed as an argument.
21
- def favorited? favoritable, options = {}
22
- if options.has_key?(:multiple_scopes) == false
23
- options[:parameter] = favoritable
24
- validate_scopes __method__, options
25
- elsif options[:multiple_scopes]
26
- results = {}
27
- options[:scope].each do |scope|
28
- results[scope] = 0 < Favorite.unblocked.send(scope + '_list').for_favoritor(self).for_favoritable(favoritable).count
29
- end
30
- return results
31
- else
32
- return 0 < Favorite.unblocked.send(options[:scope] + '_list').for_favoritor(self).for_favoritable(favoritable).count
33
- end
34
- end
35
-
36
- # Returns true if this instance has blocked the object passed as an argument.
37
- def blocked? favoritable, options = {}
38
- if options.has_key?(:multiple_scopes) == false
39
- options[:parameter] = favoritable
40
- validate_scopes __method__, options
41
- elsif options[:multiple_scopes]
42
- results = {}
43
- options[:scope].each do |scope|
44
- results[scope] = 0 < Favorite.blocked.send(scope + '_list').for_favoritor(self).for_favoritable(favoritable).count
45
- end
46
- return results
47
- else
48
- return 0 < Favorite.blocked.send(options[:scope] + '_list').for_favoritor(self).for_favoritable(favoritable).count
49
- end
50
- end
51
-
52
- # Returns true if this instance has favorited the object passed as an argument.
53
- # Returns nil if this instance has not favorited the object passed as an argument.
54
- # Returns false if this instance has blocked the object passed as an argument.
55
- def favorited_type favoritable, options = {}
56
- if options.has_key?(:multiple_scopes) == false
57
- options[:parameter] = favoritable
58
- validate_scopes __method__, options
59
- elsif options[:multiple_scopes]
60
- results = {}
61
- options[:scope].each do |scope|
62
- if Favorite.unblocked.send(scope + '_list').for_favoritor(self).for_favoritable(favoritable).count > 0
63
- results[scope] = true
64
- elsif Favorite.blocked.send(scope + '_list').for_favoritor(self).for_favoritable(favoritable).count > 0
65
- results[scope] = false
66
- else
67
- results[scope] = nil
68
- end
69
- end
70
- return results
71
- else
72
- if Favorite.unblocked.send(options[:scope] + '_list').for_favoritor(self).for_favoritable(favoritable).count > 0
73
- return true
74
- elsif Favorite.blocked.send(options[:scope] + '_list').for_favoritor(self).for_favoritable(favoritable).count > 0
75
- return false
76
- else
77
- return nil
78
- end
79
- end
80
- end
81
-
82
- # Returns the number of objects this instance has favorited.
83
- def favorites_count options = {}
84
- if options.has_key?(:multiple_scopes) == false
85
- validate_scopes __method__, options
86
- elsif options[:multiple_scopes]
87
- results = {}
88
- options[:scope].each do |scope|
89
- results[scope] = Favorite.unblocked.send(scope + '_list').for_favoritor(self).count
90
- end
91
- return results
92
- else
93
- return Favorite.unblocked.send(options[:scope] + '_list').for_favoritor(self).count
94
- end
95
- end
96
-
97
- # Creates a new favorite record for this instance to favorite the passed object.
98
- # Does not allow duplicate records to be created.
99
- def favorite favoritable, options = {}
100
- if options.has_key?(:multiple_scopes) == false
101
- options[:parameter] = favoritable
102
- validate_scopes __method__, options
103
- elsif options[:multiple_scopes]
104
- results = {}
105
- options[:scope].each do |scope|
106
- if ActsAsFavoritor.configuration.cache
107
- self.favoritor_score[scope] = self.favoritor_score[scope] + 1 || 1
108
- self.favoritor_total[scope] = self.favoritor_total[scope] + 1 || 1
109
- self.save!
110
- favoritable.favoritable_score[scope] = favoritable.favoritable_score[scope] + 1 || 1
111
- favoritable.favoritable_total[scope] = favoritable.favoritable_total[scope] + 1 || 1
112
- favoritable.save!
113
- end
114
- if self != favoritable && scope != 'all'
115
- params = {favoritable_id: favoritable.id, favoritable_type: parent_class_name(favoritable), scope: scope}
116
- results[scope] = favorites.where(params).first_or_create!
117
- end
118
- end
119
- return results
120
- else
121
- if ActsAsFavoritor.configuration.cache
122
- self.favoritor_score[options[:scope]] = self.favoritor_score[options[:scope]] + 1 || 1
123
- self.favoritor_total[options[:scope]] = self.favoritor_total[options[:scope]] + 1 || 1
124
- self.save!
125
- favoritable.favoritable_score[options[:scope]] = favoritable.favoritable_score[options[:scope]] + 1 || 1
126
- favoritable.favoritable_total[options[:scope]] = favoritable.favoritable_total[options[:scope]] + 1 || 1
127
- favoritable.save!
128
- end
129
- if self != favoritable && options[:scope] != 'all'
130
- params = {favoritable_id: favoritable.id, favoritable_type: parent_class_name(favoritable), scope: options[:scope]}
131
- return favorites.where(params).first_or_create!
132
- end
133
- end
134
- end
135
-
136
- # Deletes the favorite record if it exists.
137
- def remove_favorite favoritable, options = {}
138
- if options.has_key?(:multiple_scopes) == false
139
- options[:parameter] = favoritable
140
- validate_scopes __method__, options
141
- elsif options[:multiple_scopes]
142
- results = {}
143
- options[:scope].each do |scope|
144
- if ActsAsFavoritor.configuration.cache
145
- self.favoritor_score[scope] = self.favoritor_score[scope] - 1
146
- self.favoritor_score.delete(scope) unless self.favoritor_score[scope] > 0
147
- self.save!
148
- favoritable.favoritable_score[scope] = favoritable.favoritable_score[scope] - 1
149
- favoritable.favoritable_score.delete(scope) unless favoritable.favoritable_score[scope] > 0
150
- favoritable.save!
151
- end
152
- if favorite = get_favorite(favoritable, scope: scope, multiple_scopes: false)
153
- results[scope] = favorite.destroy
154
- end
155
- end
156
- return results
157
- else
158
- if ActsAsFavoritor.configuration.cache
159
- self.favoritor_score[options[:scope]] = self.favoritor_score[options[:scope]] - 1
160
- self.favoritor_score.delete(options[:scope]) unless self.favoritor_score[options[:scope]] > 0
161
- self.save!
162
- favoritable.favoritable_score[options[:scope]] = favoritable.favoritable_score[options[:scope]] - 1
163
- favoritable.favoritable_score.delete(options[:scope]) unless favoritable.favoritable_score[options[:scope]] > 0
164
- favoritable.save!
165
- end
166
- if favorite = get_favorite(favoritable, scope: options[:scope], multiple_scopes: false)
167
- return favorite.destroy
168
- end
169
- end
170
- end
171
-
172
- # returns the favorite records to the current instance
173
- def favorites_scoped options = {}
174
- if options.has_key?(:multiple_scopes) == false
175
- validate_scopes __method__, options
176
- elsif options[:multiple_scopes]
177
- results = {}
178
- options[:scope].each do |scope|
179
- results[scope] = favorites.unblocked.send(scope + '_list').includes :favoritable
180
- end
181
- return results
182
- else
183
- return favorites.unblocked.send(options[:scope] + '_list').includes :favoritable
184
- end
185
- end
186
-
187
- # Returns the favorite records related to this instance by type.
188
- def favorites_by_type favoritable_type, options = {}
189
- if options.has_key?(:multiple_scopes) == false
190
- options[:parameter] = favoritable_type
191
- validate_scopes __method__, options
192
- elsif options[:multiple_scopes]
193
- results = {}
194
- options[:scope].each do |scope|
195
- favorites_scope = favorites_scoped(scope: scope, multiple_scopes: false).for_favoritable_type favoritable_type
196
- results[scope] = favorites_scope = apply_options_to_scope favorites_scope, options
197
- end
198
- return results
199
- else
200
- favorites_scope = favorites_scoped(scope: options[:scope], multiple_scopes: false).for_favoritable_type favoritable_type
201
- return favorites_scope = apply_options_to_scope(favorites_scope, options)
202
- end
203
- end
204
-
205
- # Returns the favorite records related to this instance with the favoritable included.
206
- def all_favorites options = {}
207
- if options.has_key?(:multiple_scopes) == false
208
- validate_scopes __method__, options
209
- elsif options[:multiple_scopes]
210
- results = {}
211
- options[:scope].each do |scope|
212
- favorites_scope = favorites_scoped scope: scope, multiple_scopes: false
213
- results[scope] = favorites_scope = apply_options_to_scope favorites_scope, options
214
- end
215
- return results
216
- else
217
- favorites_scope = favorites_scoped scope: options[:scope], multiple_scopes: false
218
- return favorites_scope = apply_options_to_scope(favorites_scope, options)
219
- end
220
- end
221
-
222
- # Returns the actual records which this instance has favorited.
223
- def all_favorited options = {}
224
- if options.has_key?(:multiple_scopes) == false
225
- validate_scopes __method__, options
226
- elsif options[:multiple_scopes]
227
- results = {}
228
- options[:scope].each do |scope|
229
- results[scope] = all_favorites(options).collect{ |f| f.favoritable }
230
- end
231
- return results
232
- else
233
- return all_favorites(options).collect{ |f| f.favoritable }
234
- end
235
- end
236
-
237
- # Returns the actual records of a particular type which this record has fovarited.
238
- def favorited_by_type favoritable_type, options = {}
239
- if options.has_key?(:multiple_scopes) == false
240
- options[:parameter] = favoritable_type
241
- validate_scopes __method__, options
242
- elsif options[:multiple_scopes]
243
- results = {}
244
- options[:scope].each do |scope|
245
- favoritables = favoritable_type.constantize.joins(:favorited).where('favorites.blocked': false,
246
- 'favorites.favoritor_id': id,
247
- 'favorites.favoritor_type': parent_class_name(self),
248
- 'favorites.favoritable_type': favoritable_type,
249
- 'favorites.scope': scope)
250
- if options.has_key? :limit
251
- favoritables = favoritables.limit options[:limit]
252
- end
253
- if options.has_key? :includes
254
- favoritables = favoritables.includes options[:includes]
255
- end
256
- results[scope] = favoritables
257
- end
258
- return results
259
- else
260
- favoritables = favoritable_type.constantize.joins(:favorited).where('favorites.blocked': false,
261
- 'favorites.favoritor_id': id,
262
- 'favorites.favoritor_type': parent_class_name(self),
263
- 'favorites.favoritable_type': favoritable_type,
264
- 'favorites.scope': options[:scope])
265
- if options.has_key? :limit
266
- favoritables = favoritables.limit options[:limit]
267
- end
268
- if options.has_key? :includes
269
- favoritables = favoritables.includes options[:includes]
270
- end
271
- return favoritables
272
- end
273
- end
274
-
275
- def favorited_by_type_count favoritable_type, options = {}
276
- if options.has_key?(:multiple_scopes) == false
277
- options[:parameter] = favoritable_type
278
- validate_scopes __method__, options
279
- elsif options[:multiple_scopes]
280
- results = {}
281
- options[:scope].each do |scope|
282
- results[scope] = favorites.unblocked.send(scope + '_list').for_favoritable_type(favoritable_type).count
283
- end
284
- return results
285
- else
286
- return favorites.unblocked.send(options[:scope] + '_list').for_favoritable_type(favoritable_type).count
287
- end
288
- end
289
-
290
- # Allows magic names on favorited_by_type
291
- # e.g. favorited_users == favorited_by_type 'User'
292
- # Allows magic names on favorited_by_type_count
293
- # e.g. favorited_users_count == favorited_by_type_count 'User'
294
- def method_missing m, *args
295
- if m.to_s[/favorited_(.+)_count/]
296
- favorited_by_type_count $1.singularize.classify
297
- elsif m.to_s[/favorited_(.+)/]
298
- favorited_by_type $1.singularize.classify
299
- elsif m.to_s[/favoritor_(.+)_score/]
300
- favoritor_score[$1.singularize.classify] if ActsAsFavoritor.configuration.cache
301
- elsif m.to_s[/favoritor_(.+)_total/]
302
- favoritor_total[$1.singularize.classify] if ActsAsFavoritor.configuration.cache
303
- else
304
- super
305
- end
306
- end
307
-
308
- def respond_to? m, include_private = false
309
- super || m.to_s[/favorited_(.+)_count/] || m.to_s[/favorited_(.+)/]
310
- end
311
-
312
- # Returns a favorite record for the current instance and favoritable object.
313
- def get_favorite favoritable, options = {}
314
- if options.has_key?(:multiple_scopes) == false
315
- options[:parameter] = favoritable
316
- validate_scopes __method__, options
317
- elsif options[:multiple_scopes]
318
- results = {}
319
- options[:scope].each do |scope|
320
- results[scope] = favorites.unblocked.send(scope + '_list').for_favoritable(favoritable).first
321
- end
322
- return results
323
- else
324
- return favorites.unblocked.send(options[:scope] + '_list').for_favoritable(favoritable).first
325
- end
326
- end
327
-
328
- def blocks options = {}
329
- if options.has_key?(:multiple_scopes) == false
330
- validate_scopes __method__, options
331
- elsif options[:multiple_scopes]
332
- results = {}
333
- options[:scope].each do |scope|
334
- blocked_favoritors_scope = favoritables_scoped(scope: scope, multiple_scopes: false).blocked
335
- blocked_favoritors_scope = apply_options_to_scope blocked_favoritors_scope, options
336
- results[scope] = blocked_favoritors_scope.to_a.collect{ |f| f.favoritable }
337
- end
338
- return results
339
- else
340
- blocked_favoritors_scope = favoritors_scoped(scope: options[:scope], multiple_scopes: false).blocked
341
- blocked_favoritors_scope = apply_options_to_scope blocked_favoritors_scope, options
342
- return blocked_favoritors_scope.to_a.collect{ |f| f.favoritable }
343
- end
344
- end
345
-
346
- def block favoritable, options = {}
347
- if options.has_key?(:multiple_scopes) == false
348
- options[:parameter] = favoritable
349
- validate_scopes __method__, options
350
- elsif options[:multiple_scopes]
351
- results = {}
352
- options[:scope].each do |scope|
353
- results[scope] = get_favorite(favoritable, scope: scope, multiple_scopes: false) ? block_existing_favorite(favoritable, scope: scope, multiple_scopes: false) : block_future_favorite(favoritable, scope: scope, multiple_scopes: false)
354
- end
355
- return results
356
- else
357
- return get_favorite(favoritable, scope: options[:scope], multiple_scopes: false) ? block_existing_favorite(favoritable, scope: options[:scope], multiple_scopes: false) : block_future_favorite(favoritable, scope: options[:scope], multiple_scopes: false)
358
- end
359
- end
360
-
361
- def unblock favoritable, options = {}
362
- if options.has_key?(:multiple_scopes) == false
363
- options[:parameter] = favoritable
364
- validate_scopes __method__, options
365
- elsif options[:multiple_scopes]
366
- results = {}
367
- options[:scope].each do |scope|
368
- results[scope] = get_favorite(favoritable, scope: scope, multiple_scopes: false)&.update_attribute :blocked, false
369
- end
370
- return results
371
- else
372
- return get_favorite(favoritable, scope: options[:scope], multiple_scopes: false)&.update_attribute :blocked, false
373
- end
374
- end
375
-
376
- def blocked_favoritables_count options = {}
377
- if options.has_key?(:multiple_scopes) == false
378
- validate_scopes __method__, options
379
- elsif options[:multiple_scopes]
380
- results = {}
381
- options[:scope].each do |scope|
382
- results[scope] = favorites.blocked.send(scope + '_list').count
383
- end
384
- return results
385
- else
386
- return favorites.blocked.send(options[:scope] + '_list').count
387
- end
388
- end
389
-
390
- private
391
-
392
- def block_future_favorite favoritable, options = {}
393
- if options.has_key?(:multiple_scopes) == false
394
- options[:parameter] = favoritable
395
- validate_scopes __method__, options
396
- elsif options[:multiple_scopes]
397
- results = {}
398
- options[:scope].each do |scope|
399
- results[scope] = Favorite.create favoritable: favoritable, favoritor: self, blocked: true, scope: scope
400
- end
401
- return results
402
- else
403
- return Favorite.create favoritable: favoritable, favoritor: self, blocked: true, scope: options[:scope]
404
- end
405
- end
406
-
407
- def block_existing_favorite favoritable, options = {}
408
- if options.has_key?(:multiple_scopes) == false
409
- options[:parameter] = favoritable
410
- validate_scopes __method__, options
411
- elsif options[:multiple_scopes]
412
- results = {}
413
- options[:scope].each do |scope|
414
- results[scope] = get_favorite(favoritable, scope: scope, multiple_scopes: false).block!
415
- end
416
- return results
417
- else
418
- return get_favorite(favoritable, scope: options[:scope], multiple_scopes: false).block!
419
- end
420
- end
421
-
422
- end
423
-
424
- end
425
- end
1
+ module ActsAsFavoritor
2
+ module Favoritor
3
+
4
+ def self.included base
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_favoritor
10
+ has_many :favorites, as: :favoritor, dependent: :destroy
11
+ include ActsAsFavoritor::Favoritor::InstanceMethods
12
+ include ActsAsFavoritor::FavoritorLib
13
+
14
+ serialize :favoritor_cache, Hash if ActsAsFavoritor.configuration&.cache
15
+ end
16
+ end
17
+
18
+ module InstanceMethods
19
+
20
+ # Returns true if this instance has favorited the object passed as an argument.
21
+ def favorited? favoritable, options = {}
22
+ if options.has_key?(:multiple_scopes) == false
23
+ options[:parameter] = favoritable
24
+ validate_scopes __method__, options
25
+ elsif options[:multiple_scopes]
26
+ results = {}
27
+ options[:scope].each do |scope|
28
+ results[scope] = 0 < Favorite.unblocked.send(scope + '_list').for_favoritor(self).for_favoritable(favoritable).count
29
+ end
30
+ return results
31
+ else
32
+ return 0 < Favorite.unblocked.send(options[:scope] + '_list').for_favoritor(self).for_favoritable(favoritable).count
33
+ end
34
+ end
35
+
36
+ # Returns true if this instance has blocked the object passed as an argument.
37
+ def blocked? favoritable, options = {}
38
+ if options.has_key?(:multiple_scopes) == false
39
+ options[:parameter] = favoritable
40
+ validate_scopes __method__, options
41
+ elsif options[:multiple_scopes]
42
+ results = {}
43
+ options[:scope].each do |scope|
44
+ results[scope] = 0 < Favorite.blocked.send(scope + '_list').for_favoritor(self).for_favoritable(favoritable).count
45
+ end
46
+ return results
47
+ else
48
+ return 0 < Favorite.blocked.send(options[:scope] + '_list').for_favoritor(self).for_favoritable(favoritable).count
49
+ end
50
+ end
51
+
52
+ # Returns true if this instance has favorited the object passed as an argument.
53
+ # Returns nil if this instance has not favorited the object passed as an argument.
54
+ # Returns false if this instance has blocked the object passed as an argument.
55
+ def favorited_type favoritable, options = {}
56
+ if options.has_key?(:multiple_scopes) == false
57
+ options[:parameter] = favoritable
58
+ validate_scopes __method__, options
59
+ elsif options[:multiple_scopes]
60
+ results = {}
61
+ options[:scope].each do |scope|
62
+ if Favorite.unblocked.send(scope + '_list').for_favoritor(self).for_favoritable(favoritable).count > 0
63
+ results[scope] = true
64
+ elsif Favorite.blocked.send(scope + '_list').for_favoritor(self).for_favoritable(favoritable).count > 0
65
+ results[scope] = false
66
+ else
67
+ results[scope] = nil
68
+ end
69
+ end
70
+ return results
71
+ else
72
+ if Favorite.unblocked.send(options[:scope] + '_list').for_favoritor(self).for_favoritable(favoritable).count > 0
73
+ return true
74
+ elsif Favorite.blocked.send(options[:scope] + '_list').for_favoritor(self).for_favoritable(favoritable).count > 0
75
+ return false
76
+ else
77
+ return nil
78
+ end
79
+ end
80
+ end
81
+
82
+ # Returns the number of objects this instance has favorited.
83
+ def favorites_count options = {}
84
+ if options.has_key?(:multiple_scopes) == false
85
+ validate_scopes __method__, options
86
+ elsif options[:multiple_scopes]
87
+ results = {}
88
+ options[:scope].each do |scope|
89
+ results[scope] = Favorite.unblocked.send(scope + '_list').for_favoritor(self).count
90
+ end
91
+ return results
92
+ else
93
+ return Favorite.unblocked.send(options[:scope] + '_list').for_favoritor(self).count
94
+ end
95
+ end
96
+
97
+ # Creates a new favorite record for this instance to favorite the passed object.
98
+ # Does not allow duplicate records to be created.
99
+ def favorite favoritable, options = {}
100
+ if options.has_key?(:multiple_scopes) == false
101
+ options[:parameter] = favoritable
102
+ validate_scopes __method__, options
103
+ elsif options[:multiple_scopes]
104
+ results = {}
105
+ options[:scope].each do |scope|
106
+ if ActsAsFavoritor.configuration.cache
107
+ self.favoritor_score[scope] = self.favoritor_score[scope] + 1 || 1
108
+ self.favoritor_total[scope] = self.favoritor_total[scope] + 1 || 1
109
+ self.save!
110
+ favoritable.favoritable_score[scope] = favoritable.favoritable_score[scope] + 1 || 1
111
+ favoritable.favoritable_total[scope] = favoritable.favoritable_total[scope] + 1 || 1
112
+ favoritable.save!
113
+ end
114
+ if self != favoritable && scope != 'all'
115
+ params = {favoritable_id: favoritable.id, favoritable_type: parent_class_name(favoritable), scope: scope}
116
+ results[scope] = favorites.where(params).first_or_create!
117
+ end
118
+ end
119
+ return results
120
+ else
121
+ if ActsAsFavoritor.configuration.cache
122
+ self.favoritor_score[options[:scope]] = self.favoritor_score[options[:scope]] + 1 || 1
123
+ self.favoritor_total[options[:scope]] = self.favoritor_total[options[:scope]] + 1 || 1
124
+ self.save!
125
+ favoritable.favoritable_score[options[:scope]] = favoritable.favoritable_score[options[:scope]] + 1 || 1
126
+ favoritable.favoritable_total[options[:scope]] = favoritable.favoritable_total[options[:scope]] + 1 || 1
127
+ favoritable.save!
128
+ end
129
+ if self != favoritable && options[:scope] != 'all'
130
+ params = {favoritable_id: favoritable.id, favoritable_type: parent_class_name(favoritable), scope: options[:scope]}
131
+ return favorites.where(params).first_or_create!
132
+ end
133
+ end
134
+ end
135
+
136
+ # Deletes the favorite record if it exists.
137
+ def remove_favorite favoritable, options = {}
138
+ if options.has_key?(:multiple_scopes) == false
139
+ options[:parameter] = favoritable
140
+ validate_scopes __method__, options
141
+ elsif options[:multiple_scopes]
142
+ results = {}
143
+ options[:scope].each do |scope|
144
+ if ActsAsFavoritor.configuration.cache
145
+ self.favoritor_score[scope] = self.favoritor_score[scope] - 1
146
+ self.favoritor_score.delete(scope) unless self.favoritor_score[scope] > 0
147
+ self.save!
148
+ favoritable.favoritable_score[scope] = favoritable.favoritable_score[scope] - 1
149
+ favoritable.favoritable_score.delete(scope) unless favoritable.favoritable_score[scope] > 0
150
+ favoritable.save!
151
+ end
152
+ if favorite = get_favorite(favoritable, scope: scope, multiple_scopes: false)
153
+ results[scope] = favorite.destroy
154
+ end
155
+ end
156
+ return results
157
+ else
158
+ if ActsAsFavoritor.configuration.cache
159
+ self.favoritor_score[options[:scope]] = self.favoritor_score[options[:scope]] - 1
160
+ self.favoritor_score.delete(options[:scope]) unless self.favoritor_score[options[:scope]] > 0
161
+ self.save!
162
+ favoritable.favoritable_score[options[:scope]] = favoritable.favoritable_score[options[:scope]] - 1
163
+ favoritable.favoritable_score.delete(options[:scope]) unless favoritable.favoritable_score[options[:scope]] > 0
164
+ favoritable.save!
165
+ end
166
+ if favorite = get_favorite(favoritable, scope: options[:scope], multiple_scopes: false)
167
+ return favorite.destroy
168
+ end
169
+ end
170
+ end
171
+
172
+ # returns the favorite records to the current instance
173
+ def favorites_scoped options = {}
174
+ if options.has_key?(:multiple_scopes) == false
175
+ validate_scopes __method__, options
176
+ elsif options[:multiple_scopes]
177
+ results = {}
178
+ options[:scope].each do |scope|
179
+ results[scope] = favorites.unblocked.send(scope + '_list').includes :favoritable
180
+ end
181
+ return results
182
+ else
183
+ return favorites.unblocked.send(options[:scope] + '_list').includes :favoritable
184
+ end
185
+ end
186
+
187
+ # Returns the favorite records related to this instance by type.
188
+ def favorites_by_type favoritable_type, options = {}
189
+ if options.has_key?(:multiple_scopes) == false
190
+ options[:parameter] = favoritable_type
191
+ validate_scopes __method__, options
192
+ elsif options[:multiple_scopes]
193
+ results = {}
194
+ options[:scope].each do |scope|
195
+ favorites_scope = favorites_scoped(scope: scope, multiple_scopes: false).for_favoritable_type favoritable_type
196
+ results[scope] = favorites_scope = apply_options_to_scope favorites_scope, options
197
+ end
198
+ return results
199
+ else
200
+ favorites_scope = favorites_scoped(scope: options[:scope], multiple_scopes: false).for_favoritable_type favoritable_type
201
+ return favorites_scope = apply_options_to_scope(favorites_scope, options)
202
+ end
203
+ end
204
+
205
+ # Returns the favorite records related to this instance with the favoritable included.
206
+ def all_favorites options = {}
207
+ if options.has_key?(:multiple_scopes) == false
208
+ validate_scopes __method__, options
209
+ elsif options[:multiple_scopes]
210
+ results = {}
211
+ options[:scope].each do |scope|
212
+ favorites_scope = favorites_scoped scope: scope, multiple_scopes: false
213
+ results[scope] = favorites_scope = apply_options_to_scope favorites_scope, options
214
+ end
215
+ return results
216
+ else
217
+ favorites_scope = favorites_scoped scope: options[:scope], multiple_scopes: false
218
+ return favorites_scope = apply_options_to_scope(favorites_scope, options)
219
+ end
220
+ end
221
+
222
+ # Returns the actual records which this instance has favorited.
223
+ def all_favorited options = {}
224
+ if options.has_key?(:multiple_scopes) == false
225
+ validate_scopes __method__, options
226
+ elsif options[:multiple_scopes]
227
+ results = {}
228
+ options[:scope].each do |scope|
229
+ results[scope] = all_favorites(options).collect{ |f| f.favoritable }
230
+ end
231
+ return results
232
+ else
233
+ return all_favorites(options).collect{ |f| f.favoritable }
234
+ end
235
+ end
236
+
237
+ # Returns the actual records of a particular type which this record has fovarited.
238
+ def favorited_by_type favoritable_type, options = {}
239
+ if options.has_key?(:multiple_scopes) == false
240
+ options[:parameter] = favoritable_type
241
+ validate_scopes __method__, options
242
+ elsif options[:multiple_scopes]
243
+ results = {}
244
+ options[:scope].each do |scope|
245
+ favoritables = favoritable_type.constantize.joins(:favorited).where('favorites.blocked': false,
246
+ 'favorites.favoritor_id': id,
247
+ 'favorites.favoritor_type': parent_class_name(self),
248
+ 'favorites.favoritable_type': favoritable_type,
249
+ 'favorites.scope': scope)
250
+ if options.has_key? :limit
251
+ favoritables = favoritables.limit options[:limit]
252
+ end
253
+ if options.has_key? :includes
254
+ favoritables = favoritables.includes options[:includes]
255
+ end
256
+ results[scope] = favoritables
257
+ end
258
+ return results
259
+ else
260
+ favoritables = favoritable_type.constantize.joins(:favorited).where('favorites.blocked': false,
261
+ 'favorites.favoritor_id': id,
262
+ 'favorites.favoritor_type': parent_class_name(self),
263
+ 'favorites.favoritable_type': favoritable_type,
264
+ 'favorites.scope': options[:scope])
265
+ if options.has_key? :limit
266
+ favoritables = favoritables.limit options[:limit]
267
+ end
268
+ if options.has_key? :includes
269
+ favoritables = favoritables.includes options[:includes]
270
+ end
271
+ return favoritables
272
+ end
273
+ end
274
+
275
+ def favorited_by_type_count favoritable_type, options = {}
276
+ if options.has_key?(:multiple_scopes) == false
277
+ options[:parameter] = favoritable_type
278
+ validate_scopes __method__, options
279
+ elsif options[:multiple_scopes]
280
+ results = {}
281
+ options[:scope].each do |scope|
282
+ results[scope] = favorites.unblocked.send(scope + '_list').for_favoritable_type(favoritable_type).count
283
+ end
284
+ return results
285
+ else
286
+ return favorites.unblocked.send(options[:scope] + '_list').for_favoritable_type(favoritable_type).count
287
+ end
288
+ end
289
+
290
+ # Allows magic names on favorited_by_type
291
+ # e.g. favorited_users == favorited_by_type 'User'
292
+ # Allows magic names on favorited_by_type_count
293
+ # e.g. favorited_users_count == favorited_by_type_count 'User'
294
+ def method_missing m, *args
295
+ if m.to_s[/favorited_(.+)_count/]
296
+ favorited_by_type_count $1.singularize.classify
297
+ elsif m.to_s[/favorited_(.+)/]
298
+ favorited_by_type $1.singularize.classify
299
+ elsif m.to_s[/favoritor_(.+)_score/]
300
+ favoritor_score[$1.singularize.classify] if ActsAsFavoritor.configuration.cache
301
+ elsif m.to_s[/favoritor_(.+)_total/]
302
+ favoritor_total[$1.singularize.classify] if ActsAsFavoritor.configuration.cache
303
+ else
304
+ super
305
+ end
306
+ end
307
+
308
+ def respond_to? m, include_private = false
309
+ super || m.to_s[/favorited_(.+)_count/] || m.to_s[/favorited_(.+)/]
310
+ end
311
+
312
+ # Returns a favorite record for the current instance and favoritable object.
313
+ def get_favorite favoritable, options = {}
314
+ if options.has_key?(:multiple_scopes) == false
315
+ options[:parameter] = favoritable
316
+ validate_scopes __method__, options
317
+ elsif options[:multiple_scopes]
318
+ results = {}
319
+ options[:scope].each do |scope|
320
+ results[scope] = favorites.unblocked.send(scope + '_list').for_favoritable(favoritable).first
321
+ end
322
+ return results
323
+ else
324
+ return favorites.unblocked.send(options[:scope] + '_list').for_favoritable(favoritable).first
325
+ end
326
+ end
327
+
328
+ def blocks options = {}
329
+ if options.has_key?(:multiple_scopes) == false
330
+ validate_scopes __method__, options
331
+ elsif options[:multiple_scopes]
332
+ results = {}
333
+ options[:scope].each do |scope|
334
+ blocked_favoritors_scope = favoritables_scoped(scope: scope, multiple_scopes: false).blocked
335
+ blocked_favoritors_scope = apply_options_to_scope blocked_favoritors_scope, options
336
+ results[scope] = blocked_favoritors_scope.to_a.collect{ |f| f.favoritable }
337
+ end
338
+ return results
339
+ else
340
+ blocked_favoritors_scope = favoritors_scoped(scope: options[:scope], multiple_scopes: false).blocked
341
+ blocked_favoritors_scope = apply_options_to_scope blocked_favoritors_scope, options
342
+ return blocked_favoritors_scope.to_a.collect{ |f| f.favoritable }
343
+ end
344
+ end
345
+
346
+ def block favoritable, options = {}
347
+ if options.has_key?(:multiple_scopes) == false
348
+ options[:parameter] = favoritable
349
+ validate_scopes __method__, options
350
+ elsif options[:multiple_scopes]
351
+ results = {}
352
+ options[:scope].each do |scope|
353
+ results[scope] = get_favorite(favoritable, scope: scope, multiple_scopes: false) ? block_existing_favorite(favoritable, scope: scope, multiple_scopes: false) : block_future_favorite(favoritable, scope: scope, multiple_scopes: false)
354
+ end
355
+ return results
356
+ else
357
+ return get_favorite(favoritable, scope: options[:scope], multiple_scopes: false) ? block_existing_favorite(favoritable, scope: options[:scope], multiple_scopes: false) : block_future_favorite(favoritable, scope: options[:scope], multiple_scopes: false)
358
+ end
359
+ end
360
+
361
+ def unblock favoritable, options = {}
362
+ if options.has_key?(:multiple_scopes) == false
363
+ options[:parameter] = favoritable
364
+ validate_scopes __method__, options
365
+ elsif options[:multiple_scopes]
366
+ results = {}
367
+ options[:scope].each do |scope|
368
+ results[scope] = get_favorite(favoritable, scope: scope, multiple_scopes: false)&.update_attribute :blocked, false
369
+ end
370
+ return results
371
+ else
372
+ return get_favorite(favoritable, scope: options[:scope], multiple_scopes: false)&.update_attribute :blocked, false
373
+ end
374
+ end
375
+
376
+ def blocked_favoritables_count options = {}
377
+ if options.has_key?(:multiple_scopes) == false
378
+ validate_scopes __method__, options
379
+ elsif options[:multiple_scopes]
380
+ results = {}
381
+ options[:scope].each do |scope|
382
+ results[scope] = favorites.blocked.send(scope + '_list').count
383
+ end
384
+ return results
385
+ else
386
+ return favorites.blocked.send(options[:scope] + '_list').count
387
+ end
388
+ end
389
+
390
+ private
391
+
392
+ def block_future_favorite favoritable, options = {}
393
+ if options.has_key?(:multiple_scopes) == false
394
+ options[:parameter] = favoritable
395
+ validate_scopes __method__, options
396
+ elsif options[:multiple_scopes]
397
+ results = {}
398
+ options[:scope].each do |scope|
399
+ results[scope] = Favorite.create favoritable: favoritable, favoritor: self, blocked: true, scope: scope
400
+ end
401
+ return results
402
+ else
403
+ return Favorite.create favoritable: favoritable, favoritor: self, blocked: true, scope: options[:scope]
404
+ end
405
+ end
406
+
407
+ def block_existing_favorite favoritable, options = {}
408
+ if options.has_key?(:multiple_scopes) == false
409
+ options[:parameter] = favoritable
410
+ validate_scopes __method__, options
411
+ elsif options[:multiple_scopes]
412
+ results = {}
413
+ options[:scope].each do |scope|
414
+ results[scope] = get_favorite(favoritable, scope: scope, multiple_scopes: false).block!
415
+ end
416
+ return results
417
+ else
418
+ return get_favorite(favoritable, scope: options[:scope], multiple_scopes: false).block!
419
+ end
420
+ end
421
+
422
+ end
423
+
424
+ end
425
+ end