acts_as_favoritor 2.1.2 → 3.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2875795f7875bb8dee5c728af27a406a6808b0eda78a42f0349824362ac3e26
4
- data.tar.gz: 1dc45485b56e56d9df9a20f61e1044bbe1dbcf83731dbdf5beeb621aa768cc2e
3
+ metadata.gz: 184296038ccc8938d320282f82c180549f13e810ce0d33d45e5999960efa437c
4
+ data.tar.gz: 7b4bbb4d41550563d65da40a205cee21ee9f27bb100c47d99e345140992d1b2f
5
5
  SHA512:
6
- metadata.gz: bb5ba1b48c7c76ec581ae6b3c65bde08ed91a2946f01a51c5da3c9daf8d744d461ee2973ebae4baf2fdc39f695498f58eeddf8d7a95e9a14d86aeb71e48c6589
7
- data.tar.gz: 203b76be7a03c1d133345165e7d014164d2910d326991939e3314951b3f3e15d88a37118f2ece835d7a74906ab7ce1f692c94e1983179b994c3e3c4f4bc6917f
6
+ metadata.gz: 1aeb504450b30f447c5beb3a480a3f26ab71a3d0786b853276a2e2ec8c4c9c6a87a4d25145ab31f954de3206062600a4e733baa7ac37fff9657bf6fe8bda0235
7
+ data.tar.gz: 5280a639b92064db05318f2a5b5a3158878e97bb16eb218d2e42f05436ab857e8bdf8fc3528c71f55e9d7d3386652046e8b8e460ecdf3cf7b0327f60dcb6cdca
data/README.md CHANGED
@@ -95,9 +95,6 @@ user.remove_favorite(book)
95
95
  # Whether `user` has marked `book` as his favorite. Returns `true` or `false`.
96
96
  user.favorited?(book)
97
97
 
98
- # Total number of favorites by `user`.
99
- user.favorites_count
100
-
101
98
  # Returnes `user`'s favorites that have not been blocked as an array of `Favorite` records.
102
99
  user.all_favorites
103
100
 
@@ -113,16 +110,6 @@ user.favorited_by_type('Book')
113
110
  # Returns the exact same result as `user.favorited_by_type 'User'`.
114
111
  user.favorited_users
115
112
 
116
- # Total number of favorited books by `user`.
117
- user.favorited_by_type_count('Book')
118
-
119
- # Returns the exact same result as `user.favorited_by_type_count 'Book'`.
120
- user.favorited_books_count
121
-
122
- # Returns the Arel scope for favorites.
123
- # This does not return the actual favorites, just the scope of favorited including the favoritables, essentially: `book.favorites.unblocked.includes(:favoritable)`.
124
- book.favorites_scoped
125
-
126
113
  # Block a favoritable
127
114
  user.block(book)
128
115
 
@@ -134,12 +121,6 @@ user.blocked?(book)
134
121
 
135
122
  # Returns an array including all blocked Favoritable records.
136
123
  user.blocks
137
-
138
- # Total number of `user`'s favorites blocked.
139
- user.blocked_favoritables_count
140
-
141
- # Whether `user` has favorited, not favorited or blocked `book`. Returns `true`, `nil` or `false`.
142
- user.favoritable_type(book)
143
124
  ```
144
125
 
145
126
  These methods take an optional hash parameter of ActiveRecord options (`:limit`, `:order`, etc...)
@@ -152,24 +133,12 @@ These methods take an optional hash parameter of ActiveRecord options (`:limit`,
152
133
  # Returns all favoritors of a model that `acts_as_favoritable`
153
134
  book.favoritors
154
135
 
155
- # Returns the Arel scope for favoritors. This does not return the actual favoritors, just the scope of favorited records including the favoritors, essentially: `book.favorited.includes(:favoritors)`.
156
- book.favoritors_scoped
157
-
158
- # Total number of favoritors.
159
- book.favoritors_count
160
-
161
136
  # Returns an array of records with type `User` following `book`.
162
137
  book.favoritors_by_type('User')
163
138
 
164
139
  # Returns the exact same as `book.favoritors_by_type 'User'`.
165
140
  book.user_favoritors
166
141
 
167
- # Total number of favoritors with type `User`.
168
- book.favoritors_by_type_count('User')
169
-
170
- # Returns the exact same as `book.favoritors_by_type_count 'User'`.
171
- book.count_user_favoritors
172
-
173
142
  # Whether `book` has been favorited by `user`. Returns `true` or `false`.
174
143
  book.favorited_by?(user)
175
144
 
@@ -184,12 +153,6 @@ book.blocked?(user)
184
153
 
185
154
  # Returns an array including all blocked Favoritor records.
186
155
  book.blocks
187
-
188
- # Total number of `book`'s favoritors blocked.
189
- book.blocked_favoritors_count
190
-
191
- # Whether `user` has favorited, not favorited or blocked `book`. Returns `true`, `nil` or `false`.
192
- book.favoritor_type(user)
193
156
  ```
194
157
 
195
158
  These methods take an optional hash parameter of ActiveRecord options (`:limit`, `:order`, etc...)
@@ -229,11 +192,11 @@ You can create new scopes on the fly. Every single method takes `scope` as an op
229
192
  So lets see how this works:
230
193
 
231
194
  ```ruby
232
- user.favorite(book, scope: [:favorite, :watching])
233
- user.remove_favorite(book, scope: [:watching])
195
+ user.favorite(book, scopes: [:favorite, :watching])
196
+ user.remove_favorite(book, scopes: [:watching])
234
197
  second_user = User.find(2)
235
- user.favorite(second_user, scope: [:follow])
236
- book.block(user, scope: [:all]) # applies to all scopes
198
+ user.favorite(second_user, scopes: [:follow])
199
+ book.block(user, scopes: [:all]) # applies to all scopes
237
200
  ```
238
201
 
239
202
  That's simple!
@@ -241,15 +204,15 @@ That's simple!
241
204
  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:
242
205
 
243
206
  ```ruby
244
- user.favorited?(book, scope: [:favorite, :watching]) # => { favorite: true, watching: false }
245
- user.favorited?(book, scope: [:all]) # => true
207
+ user.favorited?(book, scopes: [:favorite, :watching]) # => { favorite: true, watching: false }
208
+ user.favorited?(book, scopes: [:all]) # => true
246
209
  ```
247
210
 
248
211
  `acts_as_favoritor` also provides some handy scopes for you to call on the `Favorite` model:
249
212
 
250
213
  ```ruby
251
214
  # Returns all `Favorite` records where `scope` is `my_scope`
252
- Favorite.send(my_scope + '_list')
215
+ Favorite.send("#{my_scope}_list")
253
216
 
254
217
  ## Examples
255
218
  ### Returns all `Favorite` records where `scope` is `favorites`
@@ -333,7 +296,7 @@ Tests are written with Shoulda on top of `Test::Unit` with Factory Girl being us
333
296
 
334
297
  4. Run tests
335
298
 
336
- `$ bundle exec rake test`
299
+ `$ bundle exec rspec`
337
300
 
338
301
  5. Run RuboCop
339
302
 
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'acts_as_favoritor/version'
3
+ require_relative 'acts_as_favoritor/version'
4
4
 
5
5
  module ActsAsFavoritor
6
- require 'acts_as_favoritor/configuration'
6
+ require_relative 'acts_as_favoritor/configuration'
7
7
 
8
8
  autoload :Favoritor, 'acts_as_favoritor/favoritor'
9
9
  autoload :Favoritable, 'acts_as_favoritor/favoritable'
10
10
  autoload :FavoritorLib, 'acts_as_favoritor/favoritor_lib'
11
11
  autoload :FavoriteScopes, 'acts_as_favoritor/favorite_scopes'
12
12
 
13
- require 'acts_as_favoritor/railtie' if defined?(Rails::Railtie)
13
+ require_relative 'acts_as_favoritor/railtie' if defined?(Rails::Railtie)
14
14
  end
@@ -8,100 +8,24 @@ module ActsAsFavoritor
8
8
 
9
9
  module ClassMethods
10
10
  def acts_as_favoritable
11
- has_many :favorited,
12
- as: :favoritable, dependent: :destroy, class_name: 'Favorite'
13
- include ActsAsFavoritor::Favoritable::InstanceMethods
14
- include ActsAsFavoritor::FavoritorLib
11
+ if ActsAsFavoritor.configuration&.cache
12
+ serialize :favoritable_score, Hash
13
+ serialize :favoritable_total, Hash
14
+ end
15
15
 
16
- return unless ActsAsFavoritor.configuration&.cache
16
+ has_many :favorited, as: :favoritable, dependent: :destroy,
17
+ class_name: 'Favorite'
17
18
 
18
- serialize :favoritable_score, Hash
19
- serialize :favoritable_total, Hash
19
+ include ActsAsFavoritor::Favoritable::InstanceMethods
20
+ include ActsAsFavoritor::FavoritorLib
20
21
  end
21
22
  end
22
23
 
23
24
  module InstanceMethods
24
- # Returns the number of favoritors a record has.
25
- def favoritors_count(options = {})
26
- if options.key?(:multiple_scopes) == false
27
- validate_scopes(__method__, options)
28
- elsif options[:multiple_scopes]
29
- results = {}
30
- options[:scope].each do |scope|
31
- results[scope] = favorited.unblocked.send(scope + '_list').count
32
- end
33
- results
34
- else
35
- favorited.unblocked.send(options[:scope] + '_list').count
36
- end
37
- end
38
-
39
- # Returns the favoritors by a given type.
40
- def favoritors_by_type(favoritor_type, options = {})
41
- if options.key?(:multiple_scopes) == false
42
- options[:parameter] = favoritor_type
43
- validate_scopes(__method__, options)
44
- elsif options[:multiple_scopes]
45
- results = {}
46
- options[:scope].each do |scope|
47
- favorites = favoritor_type.constantize.joins(:favorites)
48
- favorites = favorites.where(
49
- 'favorites.blocked': false,
50
- 'favorites.favoritable_id': id,
51
- 'favorites.favoritable_type': parent_class_name(self),
52
- 'favorites.favoritor_type': favoritor_type,
53
- 'favorites.scope': scope
54
- )
55
- favorites = favorites.limit(options[:limit]) if options.key?(:limit)
56
- if options.key?(:includes)
57
- favorites = favorites.includes(options[:includes])
58
- end
59
- results[scope] = favorites
60
- end
61
- results
62
- else
63
- favorites = favoritor_type.constantize.joins(:favorites)
64
- favorites = favorites.where(
65
- 'favorites.blocked': false,
66
- 'favorites.favoritable_id': id,
67
- 'favorites.favoritable_type': parent_class_name(self),
68
- 'favorites.favoritor_type': favoritor_type,
69
- 'favorites.scope': options[:scope]
70
- )
71
- favorites = favorites.limit(options[:limit]) if options.key?(:limit)
72
- if options.key?(:includes)
73
- favorites = favorites.includes(options[:includes])
74
- end
75
- favorites
76
- end
77
- end
78
-
79
- def favoritors_by_type_count(favoritor_type, options = {})
80
- if options.key?(:multiple_scopes) == false
81
- options[:parameter] = favoritor_type
82
- validate_scopes(__method__, options)
83
- elsif options[:multiple_scopes]
84
- results = {}
85
- options[:scope].each do |scope|
86
- results[scope] = favorited.unblocked.send(scope + '_list')
87
- .for_favoritor_type(favoritor_type).count
88
- end
89
- results
90
- else
91
- favorited.unblocked.send(options[:scope] + '_list')
92
- .for_favoritor_type(favoritor_type).count
93
- end
94
- end
95
-
96
- # Allows magic names on favoritors_by_type
97
- # e.g. user_favoritors == favoritors_by_type 'User'
98
- # Allows magic names on favoritors_by_type_count
99
- # e.g. count_user_favoritors == favoritors_by_type_count 'User'
25
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
100
26
  def method_missing(method, *args)
101
- if method.to_s[/count_(.+)_favoritors/]
102
- favoritors_by_type_count $1.singularize.classify
103
- elsif method.to_s[/(.+)_favoritors/]
104
- favoritors_by_type $1.singularize.classify
27
+ if method.to_s[/(.+)_favoritors/]
28
+ favoritors_by_type($1.singularize.classify)
105
29
  elsif ActsAsFavoritor.configuration.cache &&
106
30
  method.to_s[/favoritable_(.+)_score/]
107
31
  favoritable_score[$1.singularize.classify]
@@ -112,281 +36,76 @@ module ActsAsFavoritor
112
36
  super
113
37
  end
114
38
  end
39
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
115
40
 
116
41
  def respond_to_missing?(method, include_private = false)
117
- super || method.to_s[/count_(.+)_favoritors/] ||
118
- method.to_s[/(.+)_favoritors/]
42
+ super || method.to_s[/(.+)_favoritors/] ||
43
+ method.to_s[/favoritable_(.+)_score/] ||
44
+ method.to_s[/favoritable_(.+)_total/]
119
45
  end
120
46
 
121
- def blocked_favoritors_count(options = {})
122
- if options.key?(:multiple_scopes) == false
123
- validate_scopes(__method__, options)
124
- elsif options[:multiple_scopes]
125
- results = {}
126
- options[:scope].each do |scope|
127
- results[scope] = favorited.blocked.send(scope + '_list').count
128
- end
129
- results
130
- else
131
- favorited.blocked.send(options[:scope] + '_list').count
47
+ def favoritors(scopes: [ActsAsFavoritor.configuration.default_scope])
48
+ build_result_for_scopes scopes do |scope|
49
+ favorited.includes(:favoritor).unblocked.send("#{scope}_list")
50
+ .map(&:favoritor)
132
51
  end
133
52
  end
134
53
 
135
- # Returns the favorited records scoped.
136
- def favoritors_scoped(options = {})
137
- if options.key?(:multiple_scopes) == false
138
- validate_scopes(__method__, options)
139
- elsif options[:multiple_scopes]
140
- results = {}
141
- options[:scope].each do |scope|
142
- results[scope] = favorited.send(scope + '_list')
143
- .includes(:favoritor)
144
- end
145
- results
146
- else
147
- favorited.send(options[:scope] + '_list').includes(:favoritor)
54
+ def favoritors_by_type(favoritor_type,
55
+ scopes: [ActsAsFavoritor.configuration
56
+ .default_scope])
57
+ build_result_for_scopes scopes do |scope|
58
+ favorited.unblocked.send("#{scope}_list")
59
+ .for_favoritor_type(favoritor_type).map(&:favoritor)
148
60
  end
149
61
  end
150
62
 
151
- def favoritors(options = {})
152
- if options.key?(:multiple_scopes) == false
153
- validate_scopes(__method__, options)
154
- elsif options[:multiple_scopes]
155
- results = {}
156
- options[:scope].each do |scope|
157
- favoritors_scope = favoritors_scoped(
158
- scope: scope, multiple_scopes: false
159
- ).unblocked
160
- favoritors_scope = apply_options_to_scope(
161
- favoritors_scope, options
162
- )
163
- results[scope] = favoritors_scope.to_a.collect(&:favoritor)
164
- end
165
- results
166
- else
167
- favoritors_scope = favoritors_scoped(
168
- scope: options[:scope], multiple_scopes: false
169
- ).unblocked
170
- favoritors_scope = apply_options_to_scope(
171
- favoritors_scope, options
172
- )
173
- favoritors_scope.to_a.collect(&:favoritor)
63
+ def favorited_by?(favoritor,
64
+ scopes: [ActsAsFavoritor.configuration.default_scope])
65
+ build_result_for_scopes scopes do |scope|
66
+ favorited.unblocked.send("#{scope}_list").for_favoritor(favoritor)
67
+ .first.present?
174
68
  end
175
69
  end
176
70
 
177
- def blocks(options = {})
178
- if options.key?(:multiple_scopes) == false
179
- validate_scopes(__method__, options)
180
- elsif options[:multiple_scopes]
181
- results = {}
182
- options[:scope].each do |scope|
183
- blocked_favoritors_scope = favoritors_scoped(
184
- scope: scope, multiple_scopes: false
185
- ).blocked
186
- blocked_favoritors_scope = apply_options_to_scope(
187
- blocked_favoritors_scope, options
188
- )
189
- results[scope] = blocked_favoritors_scope.to_a.collect(&:favoritor)
190
- end
191
- results
192
- else
193
- blocked_favoritors_scope = favoritors_scoped(
194
- scope: options[:scope], multiple_scopes: false
195
- ).blocked
196
- blocked_favoritors_scope = apply_options_to_scope(
197
- blocked_favoritors_scope, options
71
+ def block(favoritor,
72
+ scopes: [ActsAsFavoritor.configuration.default_scope])
73
+ build_result_for_scopes scopes do |scope|
74
+ get_favorite_for(favoritor, scope).block! || Favorite.create(
75
+ favoritable: self,
76
+ favoritor: favoritor,
77
+ blocked: true,
78
+ scope: scope
198
79
  )
199
- blocked_favoritors_scope.to_a.collect(&:favoritor)
200
80
  end
201
81
  end
202
82
 
203
- # Returns true if the current instance has blocked the passed record.
204
- # Returns false if the current instance has not blocked the passed record.
205
- def blocked?(favoritor, options = {})
206
- if options.key?(:multiple_scopes) == false
207
- options[:parameter] = favoritor
208
- validate_scopes(__method__, options)
209
- elsif options[:multiple_scopes]
210
- results = {}
211
- options[:scope].each do |scope|
212
- results[scope] = favorited.blocked.send(scope + '_list')
213
- .for_favoritor(favoritor).first.present?
214
- end
215
- results
216
- else
217
- favorited.blocked.send(options[:scope] + '_list')
218
- .for_favoritor(favoritor).first.present?
83
+ def unblock(favoritor,
84
+ scopes: [ActsAsFavoritor.configuration.default_scope])
85
+ build_result_for_scopes scopes do |scope|
86
+ get_favorite_for(favoritor, scope)&.update(blocked: false)
219
87
  end
220
88
  end
221
89
 
222
- # Returns true if the current instance is favorited by the passed record.
223
- # Returns false if the current instance is blocked by the passed record or
224
- # no favorite is found.
225
- def favorited_by?(favoritor, options = {})
226
- if options.key?(:multiple_scopes) == false
227
- options[:parameter] = favoritor
228
- validate_scopes(__method__, options)
229
- elsif options[:multiple_scopes]
230
- results = {}
231
- options[:scope].each do |scope|
232
- results[scope] = favorited.unblocked.send(scope + '_list')
233
- .for_favoritor(favoritor).first.present?
234
- end
235
- results
236
- else
237
- favorited.unblocked.send(options[:scope] + '_list')
238
- .for_favoritor(favoritor).first.present?
90
+ def blocked?(favoritor,
91
+ scopes: [ActsAsFavoritor.configuration.default_scope])
92
+ build_result_for_scopes scopes do |scope|
93
+ favorited.blocked.send("#{scope}_list").for_favoritor(favoritor).first
94
+ .present?
239
95
  end
240
96
  end
241
97
 
242
- # Returns true if this instance has been favorited by the object passed as
243
- # an argument. Returns nil if this instance has not been favorited by the
244
- # object passed as an argument. Returns false if this instance has blocked
245
- # the object passed as an argument.
246
- def favoritor_type(favoritor, options = {})
247
- if options.key?(:multiple_scopes) == false
248
- options[:parameter] = favoritor
249
- validate_scopes(__method__, options)
250
- elsif options[:multiple_scopes]
251
- results = {}
252
- options[:scope].each do |scope|
253
- if favorited.unblocked.send(scope + '_list')
254
- .for_favoritor(favoritor).first.present?
255
- results[scope] = true
256
- elsif favorited.blocked.send(scope + '_list')
257
- .for_favoritor(favoritor).first.present?
258
- results[scope] = false
259
- else
260
- results[scope] = nil
261
- end
262
- end
263
- results
264
- elsif favorited.unblocked.send(options[:scope] + '_list')
265
- .for_favoritor(favoritor).first.present?
266
- true
267
- elsif favorited.blocked.send(options[:scope] + '_list')
268
- .for_favoritor(favoritor).first.present?
269
- false
270
- end
271
- end
272
-
273
- def block(favoritor, options = {})
274
- if options.key?(:multiple_scopes) == false
275
- options[:parameter] = favoritor
276
- validate_scopes(__method__, options)
277
- elsif options[:multiple_scopes]
278
- results = {}
279
- options[:scope].each do |scope|
280
- favorite = get_favorite_for(
281
- favoritor, scope: scope, multiple_scopes: false
282
- )
283
- results[scope] = if favorite
284
- block_existing_favorite(
285
- favoritor, scope: scope, multiple_scopes: false
286
- )
287
- else
288
- block_future_favorite(
289
- favoritor, scope: scope, multiple_scopes: false
290
- )
291
- end
292
- end
293
- results
294
- else
295
- favorite = get_favorite_for(
296
- favoritor, scope: options[:scope], multiple_scopes: false
297
- )
298
- if favorite
299
- block_existing_favorite(
300
- favoritor, scope: options[:scope], multiple_scopes: false
301
- )
302
- else
303
- block_future_favorite(
304
- favoritor, scope: options[:scope], multiple_scopes: false
305
- )
306
- end
307
- end
308
- end
309
-
310
- def unblock(favoritor, options = {})
311
- if options.key?(:multiple_scopes) == false
312
- options[:parameter] = favoritor
313
- validate_scopes(__method__, options)
314
- elsif options[:multiple_scopes]
315
- results = {}
316
- options[:scope].each do |scope|
317
- results[scope] = get_favorite_for(
318
- favoritor, scope: scope, multiple_scopes: false
319
- )&.update(blocked: false)
320
- end
321
- results
322
- else
323
- get_favorite_for(
324
- favoritor, scope: options[:scope], multiple_scopes: false
325
- )&.update(blocked: false)
326
- end
327
- end
328
-
329
- def get_favorite_for(favoritor, options = {})
330
- if options.key?(:multiple_scopes) == false
331
- options[:parameter] = favoritor
332
- validate_scopes(__method__, options)
333
- elsif options[:multiple_scopes]
334
- results = {}
335
- options[:scope].each do |scope|
336
- results[scope] = favorited.send(scope + '_list')
337
- .for_favoritor(favoritor).first
338
- end
339
- results
340
- else
341
- favorited.send(options[:scope] + '_list').for_favoritor(favoritor)
342
- .first
98
+ def blocks(scopes: [ActsAsFavoritor.configuration.default_scope])
99
+ build_result_for_scopes scopes do |scope|
100
+ favorited.includes(:favoritor).blocked.send("#{scope}_list")
101
+ .map(&:favoritor)
343
102
  end
344
103
  end
345
104
 
346
105
  private
347
106
 
348
- def block_future_favorite(favoritor, options = {})
349
- if options.key?(:multiple_scopes) == false
350
- options[:parameter] = favoritor
351
- validate_scopes(__method__, options)
352
- elsif options[:multiple_scopes]
353
- results = {}
354
- options[:scope].each do |scope|
355
- results[scope] = Favorite.create(
356
- favoritable: self,
357
- favoritor: favoritor,
358
- blocked: true,
359
- scope: scope
360
- )
361
- end
362
- results
363
- else
364
- Favorite.create(
365
- favoritable: self,
366
- favoritor: favoritor,
367
- blocked: true,
368
- scope: options[:scope]
369
- )
370
- end
371
- end
372
-
373
- def block_existing_favorite(favoritor, options = {})
374
- if options.key?(:multiple_scopes) == false
375
- options[:parameter] = favoritor
376
- validate_scopes(__method__, options)
377
- elsif options[:multiple_scopes]
378
- results = {}
379
- options[:scope].each do |scope|
380
- results[scope] = get_favorite_for(
381
- favoritor, scope: scope, multiple_scopes: false
382
- ).block!
383
- end
384
- results
385
- else
386
- get_favorite_for(
387
- favoritor, scope: options[:scope], multiple_scopes: false
388
- ).block!
389
- end
107
+ def get_favorite_for(favoritor, scope)
108
+ favorited.send("#{scope}_list").for_favoritor(favoritor).first
390
109
  end
391
110
  end
392
111
  end