acts_as_favoritor 1.1.3 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a2b31e850114a77b2f9784b5240e6f2a7a32256
4
- data.tar.gz: fb8fea27d053678cf7640837788232af73d6501c
3
+ metadata.gz: daa9a52b6dc334d5f4f07afdd985c6f84059129a
4
+ data.tar.gz: a74b5698a4178853d6ca9d625b6bb7dde75a6abe
5
5
  SHA512:
6
- metadata.gz: 4fae80a894f000d0cb63d0d97c43833afdf529c80b599318f9d3c3513b17b5950f5436eb328a82cce2c5830a294fe2bd88a1d0b70ed47d4c64ba0f27fddb7840
7
- data.tar.gz: a7913c4206d756539eccb8da806f012f1b5213345100266ad08e71bc04cdc0328f3e99ac33e49d485eb2541a52c0615e9fd52eed45426678e503bd8e6fb67cca
6
+ metadata.gz: aec562b696eafde8da9b39ba22ac3a921bf133edcd8512e8f4ddca16ead6590a78438897f64c385f6236612e639ff74bd5a35ca8d30c7cf1af7c914382c1331a
7
+ data.tar.gz: 71428c34d2f78866cb3b4e8bc2deb4418f3840f91fd6b30898d2a91c57f0effad65dc26ddd3640b52ce8956acf02f24e84c90e37146de39437d3b1376b65cf4d
data/CHANGELOG.md CHANGED
@@ -1,9 +1,22 @@
1
1
  # Changelog
2
2
 
3
- ### unreleased
3
+ ### master
4
4
 
5
5
  * nothing yet
6
6
 
7
+ ### 1.2.0 - 2017-08-24
8
+
9
+ * features
10
+ * add `blocked?` method to `acts_as_favoritor` model
11
+ * add `blocked?` method to `acts_as_favoritable` model
12
+ * add `favoritable_type` method to `acts_as_favoritor` model
13
+ * add `favoritor_type` method to `acts_as_favoritable` model
14
+ * add `blocks` method to `acts_as_favoritor` model
15
+ * add `block` method to `acts_as_favoritor` model
16
+ * add `unblock` method to `acts_as_favoritor` model
17
+ * add `blocked_favoritables_count` method to `acts_as_favoritor` model
18
+ * minor bugfixes
19
+
7
20
  ### 1.1.3 - 2017-08-23
8
21
 
9
22
  * bugfixes
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  `acts_as_favoritor` is a Rubygem to allow any ActiveRecord model to associate any other model including the option for multiple relationships per association with scopes.
6
6
 
7
- You are able to differentiate followers, favorites, watchers and whatever else you can imagine through a single relationship. This is accomplished by a double polymorphic relationship on the Favorite model. There is also built in support for blocking/un-blocking favorite records.
7
+ You are able to differentiate followers, favorites, watchers, votes and whatever else you can imagine through a single relationship. This is accomplished by a double polymorphic relationship on the Favorite model. There is also built in support for blocking/un-blocking favorite records.
8
8
 
9
9
  ---
10
10
 
@@ -131,6 +131,24 @@ user.favorited_books_count
131
131
  # Returns the Arel scope for favorites.
132
132
  # This does not return the actual favorites, just the scope of favorited including the favoritables, essentially: `book.favorites.unblocked.includes(:favoritable)`.
133
133
  book.favorites_scoped
134
+
135
+ # Block a favoritable
136
+ user.block book
137
+
138
+ # Unblock a favoritable
139
+ user.unblock book
140
+
141
+ # Whether `user` has blocked `book`. Returns `true` or `false`.
142
+ user.blocked? book
143
+
144
+ # Returns an array including all blocked Favoritable records.
145
+ user.blocks
146
+
147
+ # Total number of `user`'s favorites blocked.
148
+ user.blocked_favoritables_count
149
+
150
+ # Whether `user` has favorited, not favorited or blocked `book`. Returns `true`, `nil` or `false`.
151
+ user.favoritable_type book
134
152
  ```
135
153
 
136
154
  These methods take an optional hash parameter of ActiveRecord options (`:limit`, `:order`, etc...)
@@ -161,7 +179,6 @@ book.favoritors_by_type_count 'User'
161
179
  # Returns the exact same as `book.favoritors_by_type_count 'User'`.
162
180
  book.count_user_favoritors
163
181
 
164
- To see is a model that acts_as_followable is followed by a model that acts_as_follower use the following
165
182
  # Whether `book` has been favorited by `user`. Returns `true` or `false`.
166
183
  book.favorited_by? user
167
184
 
@@ -171,11 +188,17 @@ book.block user
171
188
  # Unblock a favoritor
172
189
  book.unblock user
173
190
 
191
+ # Whether `book` has blocked `user` as favoritor. Returns `true` or `false`.
192
+ book.blocked? user
193
+
174
194
  # Returns an array including all blocked Favoritor records.
175
195
  book.blocks
176
196
 
177
197
  # Total number of `book`'s favoritors blocked.
178
198
  book.blocked_favoritors_count
199
+
200
+ # Whether `user` has favorited, not favorited or blocked `book`. Returns `true`, `nil` or `false`.
201
+ book.favoritor_type user
179
202
  ```
180
203
 
181
204
  These methods take an optional hash parameter of ActiveRecord options (`:limit`, `:order`, etc...)
@@ -206,7 +229,7 @@ Favorite.for_favoritable book
206
229
 
207
230
  ### Scopes
208
231
 
209
- Using scopes with `acts_as_favoritor` enables you to Follow, Watch, Favorite, [...] between any of your models. This way you can separate distinct functionalities in your app between user states. For example: A user sees all his favorited books in a dashboard (`'favorite'`), but he only receives notifications for those, he is watching (`'watch'`). Just like YouTube does it.
232
+ Using scopes with `acts_as_favoritor` enables you to Follow, Watch, Favorite, [...] between any of your models. This way you can separate distinct functionalities in your app between user states. For example: A user sees all his favorited books in a dashboard (`'favorite'`), but he only receives notifications for those, he is watching (`'watch'`). Just like YouTube or GitHub do it. Options are endless. You could also integrate a voting / star system similar to YouTube or GitHub
210
233
 
211
234
  By default all of your favorites are scoped to `'favorite'`.
212
235
 
@@ -166,6 +166,23 @@ module ActsAsFavoritor #:nodoc:
166
166
  end
167
167
  end
168
168
 
169
+ # Returns true if the current instance has blocked the passed record
170
+ # Returns false if the current instance has not blocked the passed record
171
+ def blocked? favoritor, options = {}
172
+ if options.has_key?(:multiple_scopes) == false
173
+ options[:parameter] = favoritor
174
+ validate_scopes __method__, options
175
+ elsif options[:multiple_scopes]
176
+ results = {}
177
+ options[:scope].each do |scope|
178
+ results[scope] = favorited.blocked.send(scope + '_list').for_favoritor(favoritor).first.present?
179
+ end
180
+ return results
181
+ else
182
+ return favorited.blocked.send(options[:scope] + '_list').for_favoritor(favoritor).first.present?
183
+ end
184
+ end
185
+
169
186
  # Returns true if the current instance is favorited by the passed record
170
187
  # Returns false if the current instance is blocked by the passed record or no favorite is found
171
188
  def favorited_by? favoritor, options = {}
@@ -183,6 +200,36 @@ module ActsAsFavoritor #:nodoc:
183
200
  end
184
201
  end
185
202
 
203
+ # Returns true if this instance has been favorited by the object passed as an argument.
204
+ # Returns nil if this instance has not been favorited by the object passed as an argument.
205
+ # Returns false if this instance has blocked the object passed as an argument.
206
+ def favoritor_type favoritor, options = {}
207
+ if options.has_key?(:multiple_scopes) == false
208
+ options[:parameter] = favoritor
209
+ validate_scopes __method__, options
210
+ elsif options[:multiple_scopes]
211
+ results = {}
212
+ options[:scope].each do |scope|
213
+ if favorited.unblocked.send(scope + '_list').for_favoritor(favoritor).first.present?
214
+ results[scope] = true
215
+ elsif favorited.blocked.send(scope + '_list').for_favoritor(favoritor).first.present?
216
+ results[scope] = false
217
+ else
218
+ results[scope] = nil
219
+ end
220
+ end
221
+ return results
222
+ else
223
+ if favorited.unblocked.send(options[:scope] + '_list').for_favoritor(favoritor).first.present?
224
+ return true
225
+ elsif favorited.blocked.send(options[:scope] + '_list').for_favoritor(favoritor).first.present?
226
+ return false
227
+ else
228
+ return nil
229
+ end
230
+ end
231
+ end
232
+
186
233
  def block favoritor, options = {}
187
234
  if options.has_key?(:multiple_scopes) == false
188
235
  options[:parameter] = favoritor
@@ -31,6 +31,52 @@ module ActsAsFavoritor #:nodoc:
31
31
  end
32
32
  end
33
33
 
34
+ # Returns true if this instance has blocked the object passed as an argument.
35
+ def blocked? favoritable, options = {}
36
+ if options.has_key?(:multiple_scopes) == false
37
+ options[:parameter] = favoritable
38
+ validate_scopes __method__, options
39
+ elsif options[:multiple_scopes]
40
+ results = {}
41
+ options[:scope].each do |scope|
42
+ results[scope] = 0 < Favorite.blocked.send(scope + '_list').for_favoritor(self).for_favoritable(favoritable).count
43
+ end
44
+ return results
45
+ else
46
+ return 0 < Favorite.blocked.send(options[:scope] + '_list').for_favoritor(self).for_favoritable(favoritable).count
47
+ end
48
+ end
49
+
50
+ # Returns true if this instance has favorited the object passed as an argument.
51
+ # Returns nil if this instance has not favorited the object passed as an argument.
52
+ # Returns false if this instance has blocked the object passed as an argument.
53
+ def favorited_type favoritable, options = {}
54
+ if options.has_key?(:multiple_scopes) == false
55
+ options[:parameter] = favoritable
56
+ validate_scopes __method__, options
57
+ elsif options[:multiple_scopes]
58
+ results = {}
59
+ options[:scope].each do |scope|
60
+ if Favorite.unblocked.send(scope + '_list').for_favoritor(self).for_favoritable(favoritable).count > 0
61
+ results[scope] = true
62
+ elsif Favorite.blocked.send(scope + '_list').for_favoritor(self).for_favoritable(favoritable).count > 0
63
+ results[scope] = false
64
+ else
65
+ results[scope] = nil
66
+ end
67
+ end
68
+ return results
69
+ else
70
+ if Favorite.unblocked.send(options[:scope] + '_list').for_favoritor(self).for_favoritable(favoritable).count > 0
71
+ return true
72
+ elsif Favorite.blocked.send(options[:scope] + '_list').for_favoritor(self).for_favoritable(favoritable).count > 0
73
+ return false
74
+ else
75
+ return nil
76
+ end
77
+ end
78
+ end
79
+
34
80
  # Returns the number of objects this instance has favorited.
35
81
  def favorites_count options = {}
36
82
  if options.has_key?(:multiple_scopes) == false
@@ -77,13 +123,13 @@ module ActsAsFavoritor #:nodoc:
77
123
  elsif options[:multiple_scopes]
78
124
  results = {}
79
125
  options[:scope].each do |scope|
80
- if favorite = get_favorite(favoritable)&.send(scope + '_list')
126
+ if favorite = get_favorite(favoritable, scope: scope)
81
127
  results[scope] = favorite.destroy
82
128
  end
83
129
  end
84
130
  return results
85
131
  else
86
- if favorite = get_favorite(favoritable)&.send(options[:scope] + '_list')
132
+ if favorite = get_favorite(favoritable, scope: options[:scope])
87
133
  return favorite.destroy
88
134
  end
89
135
  end
@@ -141,7 +187,17 @@ module ActsAsFavoritor #:nodoc:
141
187
 
142
188
  # Returns the actual records which this instance has favorited.
143
189
  def all_favorited options = {}
144
- return all_favorites(options).collect{ |f| f.favoritable }
190
+ if options.has_key?(:multiple_scopes) == false
191
+ validate_scopes __method__, options
192
+ elsif options[:multiple_scopes]
193
+ results = {}
194
+ options[:scope].each do |scope|
195
+ results[scope] = all_favorites(options).collect{ |f| f.favoritable }
196
+ end
197
+ return results
198
+ else
199
+ return all_favorites(options).collect{ |f| f.favoritable }
200
+ end
145
201
  end
146
202
 
147
203
  # Returns the actual records of a particular type which this record has fovarited.
@@ -231,6 +287,100 @@ module ActsAsFavoritor #:nodoc:
231
287
  end
232
288
  end
233
289
 
290
+ def blocks options = {}
291
+ if options.has_key?(:multiple_scopes) == false
292
+ validate_scopes __method__, options
293
+ elsif options[:multiple_scopes]
294
+ results = {}
295
+ options[:scope].each do |scope|
296
+ blocked_favoritors_scope = favoritables_scoped(scope: scope).blocked
297
+ blocked_favoritors_scope = apply_options_to_scope blocked_favoritors_scope, options
298
+ results[scope] = blocked_favoritors_scope.to_a.collect{ |f| f.favoritable }
299
+ end
300
+ return results
301
+ else
302
+ blocked_favoritors_scope = favoritors_scoped(scope: options[:scope]).blocked
303
+ blocked_favoritors_scope = apply_options_to_scope blocked_favoritors_scope, options
304
+ return blocked_favoritors_scope.to_a.collect{ |f| f.favoritable }
305
+ end
306
+ end
307
+
308
+ def block favoritable, options = {}
309
+ if options.has_key?(:multiple_scopes) == false
310
+ options[:parameter] = favoritable
311
+ validate_scopes __method__, options
312
+ elsif options[:multiple_scopes]
313
+ results = {}
314
+ options[:scope].each do |scope|
315
+ results[scope] = get_favorite(favoritable, scope: scope) ? block_existing_favorite(favoritable, scope: scope) : block_future_favorite(favoritable, scope: scope)
316
+ end
317
+ return results
318
+ else
319
+ return get_favorite(favoritable, scope: options[:scope]) ? block_existing_favorite(favoritable, scope: options[:scope]) : block_future_favorite(favoritable, scope: options[:scope])
320
+ end
321
+ end
322
+
323
+ def unblock favoritable, options = {}
324
+ if options.has_key?(:multiple_scopes) == false
325
+ options[:parameter] = favoritable
326
+ validate_scopes __method__, options
327
+ elsif options[:multiple_scopes]
328
+ results = {}
329
+ options[:scope].each do |scope|
330
+ results[scope] = get_favorite(favoritable, scope: scope)&.update_attribute :blocked, false
331
+ end
332
+ return results
333
+ else
334
+ return get_favorite(favoritable, scope: options[:scope])&.update_attribute :blocked, false
335
+ end
336
+ end
337
+
338
+ def blocked_favoritables_count options = {}
339
+ if options.has_key?(:multiple_scopes) == false
340
+ validate_scopes __method__, options
341
+ elsif options[:multiple_scopes]
342
+ results = {}
343
+ options[:scope].each do |scope|
344
+ results[scope] = favorites.blocked.send(scope + '_list').count
345
+ end
346
+ return results
347
+ else
348
+ return favorites.blocked.send(options[:scope] + '_list').count
349
+ end
350
+ end
351
+
352
+ private
353
+
354
+ def block_future_favorite favoritable, options = {}
355
+ if options.has_key?(:multiple_scopes) == false
356
+ options[:parameter] = favoritable
357
+ validate_scopes __method__, options
358
+ elsif options[:multiple_scopes]
359
+ results = {}
360
+ options[:scope].each do |scope|
361
+ results[scope] = Favorite.create favoritable: favoritable, favoritor: self, blocked: true, scope: scope
362
+ end
363
+ return results
364
+ else
365
+ return Favorite.create favoritable: favoritable, favoritor: self, blocked: true, scope: options[:scope]
366
+ end
367
+ end
368
+
369
+ def block_existing_favorite favoritable, options = {}
370
+ if options.has_key?(:multiple_scopes) == false
371
+ options[:parameter] = favoritable
372
+ validate_scopes __method__, options
373
+ elsif options[:multiple_scopes]
374
+ results = {}
375
+ options[:scope].each do |scope|
376
+ results[scope] = get_favorite(favoritable, scope: scope).block!
377
+ end
378
+ return results
379
+ else
380
+ return get_favorite(favoritable, scope: options[:scope]).block!
381
+ end
382
+ end
383
+
234
384
  end
235
385
 
236
386
  end
@@ -37,7 +37,7 @@ module ActsAsFavoritor
37
37
  end
38
38
 
39
39
  def validate_scopes method, options = {}
40
- options[:scope] = options[:scope] || ActsAsFavoritor.default_scope
40
+ options[:scope] = options[:scope] || [ActsAsFavoritor.default_scope]
41
41
  if options[:scope].size > 1
42
42
  options[:multiple_scopes] = false # ?
43
43
  else
@@ -1,5 +1,5 @@
1
1
  module ActsAsFavoritor
2
2
 
3
- VERSION = '1.1.3'
3
+ VERSION = '1.2.0'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_favoritor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Hübotter