acts_as_favoritor 4.0.0 → 5.0.1

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
  SHA256:
3
- metadata.gz: 688cadb90a8c8e5fb799d2973aaffa6345e756456640fdba0c3d9f44715b92c2
4
- data.tar.gz: 3b9a838d6fdf2a011494ca5822ca8f54290738d1a81ddb82f36e051f8f3630cc
3
+ metadata.gz: 2554cf505e5a47eb5f24bc1de95fb6287d397259cc70685f4905d99be6164e72
4
+ data.tar.gz: ac5749692ac44627c97dfb2797834671f7d3cc368d32e63aac5d2d9a94e4b6b6
5
5
  SHA512:
6
- metadata.gz: c1a3319925be822442579ec4d022b454b52649b5a95fa5949fdd7498420bb38038889136bb39c2da96f5511713a398eaf3a372a578ad4c8b6d1edd9089188ff5
7
- data.tar.gz: 52f518e8ab2d8c66c6fdd47f3bc57329745c3cdf28bd1f3ac41b13b3214d4d8ce9bad2549415b3098326437ec2747d90baf99c27411973991adcc82755ac79e2
6
+ metadata.gz: 544dda8f78ced970192a0d523f8c7b4804be135d33afe8635a15eaef3263d5413793d54ca5342fa12636d4a1c893b4d52bb1c160180bb72386c4a144a3280a6b
7
+ data.tar.gz: 28fd936df2e6dc96f6b5a2449fc654f40b4307a7d4bed58e5f648652e283af46d3547f1e3ac2245b928400d1391186fae291ff4fc1756438a14afb8c03858abe
data/README.md CHANGED
@@ -1,42 +1,20 @@
1
1
  # acts_as_favoritor
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/acts_as_favoritor.svg)](https://badge.fury.io/rb/acts_as_favoritor) ![Travis](https://travis-ci.org/jonhue/acts_as_favoritor.svg?branch=master)
4
-
5
3
  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
4
 
7
5
  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 as well as caching.
8
6
 
9
- ---
10
-
11
- ## Table of Contents
12
-
13
- * [Installation](#installation)
14
- * [Usage](#usage)
15
- * [Setup](#setup)
16
- * [`acts_as_favoritor` methods](#acts_as_favoritor-methods)
17
- * [`acts_as_favoritable` methods](#acts_as_favoritable-methods)
18
- * [`Favorite` model](#favorite-model)
19
- * [Scopes](#scopes)
20
- * [Caching](#caching)
21
- * [Configuration](#configuration)
22
- * [Testing](#testing)
23
- * [To do](#to-do)
24
- * [Contributing](#contributing)
25
- * [Semantic versioning](#semantic-versioning)
26
-
27
- ---
28
-
29
7
  ## Installation
30
8
 
31
- acts_as_favoritor works with Rails 5.0 onwards. You can add it to your `Gemfile` with:
9
+ You can add acts_as_favoritor to your `Gemfile` with:
32
10
 
33
11
  ```ruby
34
12
  gem 'acts_as_favoritor'
35
13
  ```
36
14
 
37
- And then execute:
15
+ And then run:
38
16
 
39
- $ bundle
17
+ $ bundle install
40
18
 
41
19
  Or install it yourself as:
42
20
 
@@ -92,48 +70,44 @@ user.favorite(book)
92
70
  # `user` removes `book` from favorites.
93
71
  user.unfavorite(book)
94
72
 
95
- # Whether `user` has marked `book` as his favorite. Returns `true` or `false`.
73
+ # Whether `user` has marked `book` as his favorite.
96
74
  user.favorited?(book)
97
75
 
98
- # Returnes `user`'s favorites that have not been blocked as an array of `Favorite` records.
76
+ # Returns an Active Record relation of `user`'s `Favorite` records that have not been blocked.
99
77
  user.all_favorites
100
78
 
101
- # Returns all favorited objects of `user` as an array (unblocked). This can be a collection of different object types, e.g.: `User`, `Book`.
79
+ # Returns an array of all unblocked favorited objects of `user`. This can be a collection of different object types, e.g.: `User`, `Book`.
102
80
  user.all_favorited
103
81
 
104
- # Returns an array of `Favorite` records where the `favoritable_type` is `Book`.
82
+ # Returns an Active Record relation of `Favorite` records where the `favoritable_type` is `Book`.
105
83
  user.favorites_by_type('Book')
106
84
 
107
- # Returns an array of all favorited objects of `user` where `favoritable_type` is 'Book', this can be a collection of different object types, e.g.: `User`, `Book`.
85
+ # Returns an Active Record relation of all favorited objects of `user` where `favoritable_type` is 'Book'.
108
86
  user.favorited_by_type('Book')
109
87
 
110
- # Returns the exact same result as `user.favorited_by_type 'User'`.
88
+ # Returns the exact same as `user.favorited_by_type('User')`.
111
89
  user.favorited_users
112
90
 
113
- # Whether `user` has been blocked by `book`. Returns `true` or `false`.
91
+ # Whether `user` has been blocked by `book`.
114
92
  user.blocked_by?(book)
115
93
 
116
- # Returns an array including all blocked Favorite records.
94
+ # Returns an array of all favoritables that blocked `user`.
117
95
  user.blocked_by
118
96
  ```
119
97
 
120
- These methods take an optional hash parameter of ActiveRecord options (`:limit`, `:order`, etc...)
121
-
122
- favorites_by_type, all_favorites, all_favorited, favorited_by_type
123
-
124
98
  ### `acts_as_favoritable` methods
125
99
 
126
100
  ```ruby
127
101
  # Returns all favoritors of a model that `acts_as_favoritable`
128
102
  book.favoritors
129
103
 
130
- # Returns an array of records with type `User` following `book`.
104
+ # Returns an Active Record relation of records with type `User` following `book`.
131
105
  book.favoritors_by_type('User')
132
106
 
133
- # Returns the exact same as `book.favoritors_by_type 'User'`.
107
+ # Returns the exact same as `book.favoritors_by_type('User')`.
134
108
  book.user_favoritors
135
109
 
136
- # Whether `book` has been favorited by `user`. Returns `true` or `false`.
110
+ # Whether `book` has been favorited by `user`.
137
111
  book.favorited_by?(user)
138
112
 
139
113
  # Block a favoritor
@@ -142,30 +116,26 @@ book.block(user)
142
116
  # Unblock a favoritor
143
117
  book.unblock(user)
144
118
 
145
- # Whether `book` has blocked `user` as favoritor. Returns `true` or `false`.
119
+ # Whether `book` has blocked `user` as favoritor.
146
120
  book.blocked?(user)
147
121
 
148
- # Returns an array including all blocked Favoritor records.
122
+ # Returns an array of all blocked favoritors.
149
123
  book.blocked
150
124
  ```
151
125
 
152
- These methods take an optional hash parameter of ActiveRecord options (`:limit`, `:order`, etc...)
153
-
154
- favoritors_by_type, favoritors, blocks
155
-
156
126
  ### `Favorite` model
157
127
 
158
128
  ```ruby
159
- # Returns all `Favorite` records where `blocked` is `false`.
129
+ # Returns an Active Record relation of all `Favorite` records where `blocked` is `false`.
160
130
  Favorite.unblocked
161
131
 
162
- # Returns all `Favorite` records where `blocked` is `true`.
132
+ # Returns an Active Record relation of all `Favorite` records where `blocked` is `true`.
163
133
  Favorite.blocked
164
134
 
165
- # Returns all favorites of `user`, including those who were blocked.
135
+ # Returns an Active Record relation of all favorites of `user`, including those who were blocked.
166
136
  Favorite.for_favoritor(user)
167
137
 
168
- # Returns all favoritors of `book`, including those who were blocked.
138
+ # Returns an Active Record relation of all favoritors of `book`, including those who were blocked.
169
139
  Favorite.for_favoritable(book)
170
140
  ```
171
141
 
@@ -175,24 +145,25 @@ Using scopes with `acts_as_favoritor` enables you to Follow, Watch, Favorite, [.
175
145
 
176
146
  By default all of your favorites are scoped to `'favorite'`.
177
147
 
178
- You can create new scopes on the fly. Every single method takes `scope` as an option which expexts an array containing your scopes as strings.
148
+ You can create new scopes on the fly. Every single method takes `scope`/`scopes` as an option which expexts a symbol or an array of symbols containing your scopes.
179
149
 
180
150
  So lets see how this works:
181
151
 
182
152
  ```ruby
183
153
  user.favorite(book, scopes: [:favorite, :watching])
184
- user.unfavorite(book, scopes: [:watching])
154
+ user.unfavorite(book, scope: :watching)
185
155
  second_user = User.find(2)
186
- user.favorite(second_user, scopes: [:follow])
156
+ user.favorite(second_user, scope: :follow)
187
157
  ```
188
158
 
189
159
  That's simple!
190
160
 
191
- 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:
161
+ 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 when scopes are given as an array:
192
162
 
193
163
  ```ruby
194
164
  user.favorited?(book, scopes: [:favorite, :watching]) # => { favorite: true, watching: false }
195
- user.favorited?(book, scopes: [:favorite]) # => true
165
+ user.favorited?(book, scopes: [:favorite]) # => { favorite: true }
166
+ user.favorited?(book, scope: :favorite) # => true
196
167
  ```
197
168
 
198
169
  `acts_as_favoritor` also provides some handy scopes for you to call on the `Favorite` model:
@@ -251,58 +222,55 @@ book.favoritable_favorite_cache # => 1
251
222
 
252
223
  **Note:** These methods are available for every scope you are using.
253
224
 
254
- ---
255
-
256
225
  ## Configuration
257
226
 
258
227
  You can configure acts_as_favoritor by passing a block to `configure`. This can be done in `config/initializers/acts_as_favoritor.rb`:
259
228
 
260
229
  ```ruby
261
230
  ActsAsFavoritor.configure do |config|
262
- config.default_scope = 'follow'
231
+ config.default_scope = :follow
263
232
  end
264
233
  ```
265
234
 
266
- **`default_scope`** Specify your default scope. Takes a string. Defaults to `'favorite'`. Learn more about scopes [here](#scopes).
235
+ **`default_scope`** Specify your default scope. Takes a string. Defaults to `:favorite`. Learn more about scopes [here](#scopes).
267
236
 
268
237
  **`cache`** Whether `acts_as_favoritor` uses caching or not. Takes a boolean. Defaults to `false`. Learn more about caching [here](#caching).
269
238
 
270
- ---
271
-
272
- ## Testing
239
+ ## Development
273
240
 
274
- Tests are written with Shoulda on top of `Test::Unit` with Factory Girl being used instead of fixtures. Tests are run using rake.
241
+ To start development you first have to fork this repository and locally clone your fork.
275
242
 
276
- 1. Fork this repository
277
- 2. Clone your forked git locally
278
- 3. Install dependencies
243
+ Install the projects dependencies by running:
279
244
 
280
- `$ bundle install`
245
+ $ bundle install
281
246
 
282
- 4. Run tests
247
+ ### Testing
283
248
 
284
- `$ bundle exec rspec`
249
+ Tests are written with RSpec and can be found in `/spec`.
285
250
 
286
- 5. Run RuboCop
251
+ To run tests:
287
252
 
288
- `$ bundle exec rubocop`
253
+ $ bundle exec rspec
289
254
 
290
- ---
255
+ To run RuboCop:
291
256
 
292
- ## To do
257
+ $ bundle exec rubocop
293
258
 
294
- We use [GitHub projects](https://github.com/jonhue/acts_as_favoritor/projects/1) to coordinate the work on this project.
295
-
296
- To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/acts_as_favoritor/issues/new).
259
+ ## Contributing
297
260
 
298
- ---
261
+ We warmly welcome everyone who is intersted in contributing. Please reference our [contributing guidelines](CONTRIBUTING.md) and our [Code of Conduct](CODE_OF_CONDUCT.md).
299
262
 
300
- ## Contributing
263
+ ## Releases
301
264
 
302
- We hope that you will consider contributing to acts_as_favoritor. Please read this short overview for some information about how to get started:
265
+ [Here](https://github.com/jonhue/acts_as_favoritor/releases) you can find details on all past releases. Unreleased breaking changes that are on the current master can be found [here](CHANGELOG.md).
303
266
 
304
- [Learn more about contributing to this repository](CONTRIBUTING.md), [Code of Conduct](CODE_OF_CONDUCT.md)
267
+ acts_as_favoritor follows Semantic Versioning 2.0 as defined at http://semver.org. Reference our [security policy](SECURITY.md).
305
268
 
306
- ### Semantic Versioning
269
+ ### Publishing
307
270
 
308
- acts_as_favoritor follows Semantic Versioning 2.0 as defined at http://semver.org.
271
+ 1. Review breaking changes and deprecations in `CHANGELOG.md`.
272
+ 1. Change the gem version in `lib/acts_as_favoritor/version.rb`.
273
+ 1. Reset `CHANGELOG.md`.
274
+ 1. Create a pull request to merge the changes into `master`.
275
+ 1. After the pull request was merged, create a new release listing the breaking changes and commits on `master` since the last release.
276
+ 2. The release workflow will publish the gem to RubyGems.
@@ -14,8 +14,7 @@ module ActsAsFavoritor
14
14
  DEFAULT_SCOPE = :favorite
15
15
  DEFAULT_CACHE = false
16
16
 
17
- attr_accessor :default_scope
18
- attr_accessor :cache
17
+ attr_accessor :cache, :default_scope
19
18
 
20
19
  def initialize
21
20
  @default_scope = DEFAULT_SCOPE
@@ -38,40 +38,47 @@ module ActsAsFavoritor
38
38
  end
39
39
  # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
40
40
 
41
+ # rubocop:disable Style/OptionalBooleanParameter
41
42
  def respond_to_missing?(method, include_private = false)
42
43
  super || method.to_s[/(.+)_favoritors/] ||
43
44
  method.to_s[/favoritable_(.+)_score/] ||
44
45
  method.to_s[/favoritable_(.+)_total/]
45
46
  end
47
+ # rubocop:enable Style/OptionalBooleanParameter
46
48
 
47
- def favoritors(scopes: [ActsAsFavoritor.configuration.default_scope])
48
- self.class.build_result_for_scopes scopes do |scope|
49
- favorited.includes(:favoritor).unblocked.send("#{scope}_list")
49
+ def favoritors(scope: ActsAsFavoritor.configuration.default_scope,
50
+ scopes: nil)
51
+ self.class.build_result_for_scopes(scopes || scope) do |s|
52
+ favorited.includes(:favoritor).unblocked.send("#{s}_list")
50
53
  .map(&:favoritor)
51
54
  end
52
55
  end
53
56
 
54
57
  def favoritors_by_type(favoritor_type,
55
- scopes: [ActsAsFavoritor.configuration
56
- .default_scope])
57
- self.class.build_result_for_scopes scopes do |scope|
58
- favorited.unblocked.send("#{scope}_list")
59
- .for_favoritor_type(favoritor_type).map(&:favoritor)
58
+ scope: ActsAsFavoritor.configuration.default_scope,
59
+ scopes: nil)
60
+ self.class.build_result_for_scopes(scopes || scope) do |s|
61
+ favoritor_type.constantize.includes(:favorites)
62
+ .where(favorites: {
63
+ blocked: false, favoritable_id: id,
64
+ favoritable_type: self.class.name, scope: s
65
+ })
60
66
  end
61
67
  end
62
68
 
63
69
  def favorited_by?(favoritor,
64
- scopes: [ActsAsFavoritor.configuration.default_scope])
65
- self.class.build_result_for_scopes scopes do |scope|
66
- favorited.unblocked.send("#{scope}_list").for_favoritor(favoritor)
70
+ scope: ActsAsFavoritor.configuration.default_scope,
71
+ scopes: nil)
72
+ self.class.build_result_for_scopes(scopes || scope) do |s|
73
+ favorited.unblocked.send("#{s}_list").for_favoritor(favoritor)
67
74
  .first.present?
68
75
  end
69
76
  end
70
77
 
71
- def block(favoritor,
72
- scopes: [ActsAsFavoritor.configuration.default_scope])
73
- self.class.build_result_for_scopes scopes do |scope|
74
- get_favorite_for(favoritor, scope)&.block! || Favorite.create(
78
+ def block(favoritor, scope: ActsAsFavoritor.configuration.default_scope,
79
+ scopes: nil)
80
+ self.class.build_result_for_scopes(scopes || scope) do |s|
81
+ get_favorite_for(favoritor, s)&.block! || Favorite.create(
75
82
  favoritable: self,
76
83
  favoritor: favoritor,
77
84
  blocked: true,
@@ -80,24 +87,26 @@ module ActsAsFavoritor
80
87
  end
81
88
  end
82
89
 
83
- def unblock(favoritor,
84
- scopes: [ActsAsFavoritor.configuration.default_scope])
85
- self.class.build_result_for_scopes scopes do |scope|
86
- get_favorite_for(favoritor, scope)&.update(blocked: false)
90
+ def unblock(favoritor, scope: ActsAsFavoritor.configuration.default_scope,
91
+ scopes: nil)
92
+ self.class.build_result_for_scopes(scopes || scope) do |s|
93
+ get_favorite_for(favoritor, s)&.update(blocked: false)
87
94
  end
88
95
  end
89
96
 
90
97
  def blocked?(favoritor,
91
- scopes: [ActsAsFavoritor.configuration.default_scope])
92
- self.class.build_result_for_scopes scopes do |scope|
93
- favorited.blocked.send("#{scope}_list").for_favoritor(favoritor).first
98
+ scope: ActsAsFavoritor.configuration.default_scope,
99
+ scopes: nil)
100
+ self.class.build_result_for_scopes(scopes || scope) do |s|
101
+ favorited.blocked.send("#{s}_list").for_favoritor(favoritor).first
94
102
  .present?
95
103
  end
96
104
  end
97
105
 
98
- def blocked(scopes: [ActsAsFavoritor.configuration.default_scope])
99
- self.class.build_result_for_scopes scopes do |scope|
100
- favorited.includes(:favoritor).blocked.send("#{scope}_list")
106
+ def blocked(scope: ActsAsFavoritor.configuration.default_scope,
107
+ scopes: nil)
108
+ self.class.build_result_for_scopes(scopes || scope) do |s|
109
+ favorited.includes(:favoritor).blocked.send("#{s}_list")
101
110
  .map(&:favoritor)
102
111
  end
103
112
  end
@@ -12,9 +12,11 @@ module ActsAsFavoritor
12
12
  end
13
13
  end
14
14
 
15
+ # rubocop:disable Style/OptionalBooleanParameter
15
16
  def respond_to_missing?(method, include_private = false)
16
17
  super || method.to_s[/(.+)_list/]
17
18
  end
19
+ # rubocop:enable Style/OptionalBooleanParameter
18
20
 
19
21
  def for_favoritor(favoritor)
20
22
  where(
@@ -38,85 +38,97 @@ module ActsAsFavoritor
38
38
  end
39
39
  # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
40
40
 
41
+ # rubocop:disable Style/OptionalBooleanParameter
41
42
  def respond_to_missing?(method, include_private = false)
42
43
  super || method.to_s[/favorited_(.+)/] ||
43
44
  method.to_s[/favoritor_(.+)_score/] ||
44
45
  method.to_s[/favoritor_(.+)_total/]
45
46
  end
47
+ # rubocop:enable Style/OptionalBooleanParameter
46
48
 
47
49
  def favorite(favoritable,
48
- scopes: [ActsAsFavoritor.configuration.default_scope])
49
- self.class.build_result_for_scopes scopes do |scope|
50
+ scope: ActsAsFavoritor.configuration.default_scope,
51
+ scopes: nil)
52
+ self.class.build_result_for_scopes(scopes || scope) do |s|
50
53
  return nil if self == favoritable
51
54
 
52
- inc_cache if ActsAsFavoritor.configuration.cache
55
+ inc_cache(favoritable, s) if ActsAsFavoritor.configuration.cache
53
56
 
54
- favorites.for_favoritable(favoritable).send("#{scope}_list")
57
+ favorites.for_favoritable(favoritable).send("#{s}_list")
55
58
  .first_or_create!
56
59
  end
57
60
  end
58
61
 
59
62
  def unfavorite(favoritable,
60
- scopes: [ActsAsFavoritor.configuration.default_scope])
61
- self.class.build_result_for_scopes scopes do |scope|
62
- favorite_record = get_favorite(favoritable, scope)
63
+ scope: ActsAsFavoritor.configuration.default_scope,
64
+ scopes: nil)
65
+ self.class.build_result_for_scopes(scopes || scope) do |s|
66
+ favorite_record = get_favorite(favoritable, s)
63
67
  return nil unless favorite_record.present?
64
68
 
65
- dec_cache if ActsAsFavoritor.configuration.cache
69
+ dec_cache(favoritable, s) if ActsAsFavoritor.configuration.cache
66
70
  favorite_record.destroy!
67
71
  end
68
72
  end
69
73
 
70
74
  def favorited?(favoritable,
71
- scopes: [ActsAsFavoritor.configuration.default_scope])
72
- self.class.build_result_for_scopes scopes do |scope|
73
- Favorite.unblocked.send("#{scope}_list").for_favoritor(self)
75
+ scope: ActsAsFavoritor.configuration.default_scope,
76
+ scopes: nil)
77
+ self.class.build_result_for_scopes(scopes || scope) do |s|
78
+ Favorite.unblocked.send("#{s}_list").for_favoritor(self)
74
79
  .for_favoritable(favoritable).size.positive?
75
80
  end
76
81
  end
77
82
 
78
- def all_favorites(scopes: [ActsAsFavoritor.configuration.default_scope])
79
- self.class.build_result_for_scopes scopes do |scope|
80
- favorites.unblocked.send("#{scope}_list")
83
+ def all_favorites(scope: ActsAsFavoritor.configuration.default_scope,
84
+ scopes: nil)
85
+ self.class.build_result_for_scopes(scopes || scope) do |s|
86
+ favorites.unblocked.send("#{s}_list")
81
87
  end
82
88
  end
83
89
 
84
- def all_favorited(scopes: [ActsAsFavoritor.configuration.default_scope])
85
- self.class.build_result_for_scopes scopes do |scope|
86
- favorites.unblocked.send("#{scope}_list").includes(:favoritable)
90
+ def all_favorited(scope: ActsAsFavoritor.configuration.default_scope,
91
+ scopes: nil)
92
+ self.class.build_result_for_scopes(scopes || scope) do |s|
93
+ favorites.unblocked.send("#{s}_list").includes(:favoritable)
87
94
  .map(&:favoritable)
88
95
  end
89
96
  end
90
97
 
91
98
  def favorites_by_type(favoritable_type,
92
- scopes: [ActsAsFavoritor.configuration
93
- .default_scope])
94
- self.class.build_result_for_scopes scopes do |scope|
95
- favorites.unblocked.send("#{scope}_list")
99
+ scope: ActsAsFavoritor.configuration.default_scope,
100
+ scopes: nil)
101
+ self.class.build_result_for_scopes(scopes || scope) do |s|
102
+ favorites.unblocked.send("#{s}_list")
96
103
  .for_favoritable_type(favoritable_type)
97
104
  end
98
105
  end
99
106
 
100
107
  def favorited_by_type(favoritable_type,
101
- scopes: [ActsAsFavoritor.configuration
102
- .default_scope])
103
- self.class.build_result_for_scopes scopes do |scope|
104
- favorites.unblocked.send("#{scope}_list").includes(:favoritable)
105
- .for_favoritable_type(favoritable_type).map(&:favoritable)
108
+ scope: ActsAsFavoritor.configuration.default_scope,
109
+ scopes: nil)
110
+ self.class.build_result_for_scopes(scopes || scope) do |s|
111
+ favoritable_type.constantize.includes(:favorited)
112
+ .where(favorites: {
113
+ blocked: false, favoritor_id: id,
114
+ favoritor_type: self.class.name, scope: s
115
+ })
106
116
  end
107
117
  end
108
118
 
109
119
  def blocked_by?(favoritable,
110
- scopes: [ActsAsFavoritor.configuration.default_scope])
111
- self.class.build_result_for_scopes scopes do |scope|
112
- Favorite.blocked.send("#{scope}_list").for_favoritor(self)
120
+ scope: ActsAsFavoritor.configuration.default_scope,
121
+ scopes: nil)
122
+ self.class.build_result_for_scopes(scopes || scope) do |s|
123
+ Favorite.blocked.send("#{s}_list").for_favoritor(self)
113
124
  .for_favoritable(favoritable).size.positive?
114
125
  end
115
126
  end
116
127
 
117
- def blocked_by(scopes: [ActsAsFavoritor.configuration.default_scope])
118
- self.class.build_result_for_scopes scopes do |scope|
119
- favorites.includes(:favoritable).blocked.send("#{scope}_list")
128
+ def blocked_by(scope: ActsAsFavoritor.configuration.default_scope,
129
+ scopes: nil)
130
+ self.class.build_result_for_scopes(scopes || scope) do |s|
131
+ favorites.includes(:favoritable).blocked.send("#{s}_list")
120
132
  .map(&:favoritable)
121
133
  end
122
134
  end
@@ -129,7 +141,7 @@ module ActsAsFavoritor
129
141
  end
130
142
 
131
143
  # rubocop:disable Metrics/AbcSize
132
- def inc_cache
144
+ def inc_cache(favoritable, scope)
133
145
  favoritor_score[scope] = (favoritor_score[scope] || 0) + 1
134
146
  favoritor_total[scope] = (favoritor_total[scope] || 0) + 1
135
147
  save!
@@ -141,16 +153,14 @@ module ActsAsFavoritor
141
153
  favoritable.save!
142
154
  end
143
155
 
144
- def dec_cache
156
+ def dec_cache(favoritable, scope)
145
157
  favoritor_score[scope] = (favoritor_score[scope] || 0) - 1
146
158
  favoritor_score.delete(scope) unless favoritor_score[scope].positive?
147
159
  save!
148
160
 
149
161
  favoritable.favoritable_score[scope] =
150
162
  (favoritable.favoritable_score[scope] || 0) - 1
151
- # rubocop:disable Metrics/LineLength
152
163
  favoritable.favoritable_score.delete(scope) unless favoritable.favoritable_score[scope].positive?
153
- # rubocop:enable Metrics/LineLength
154
164
  favoritable.save!
155
165
  end
156
166
  # rubocop:enable Metrics/AbcSize
@@ -3,14 +3,10 @@
3
3
  module ActsAsFavoritor
4
4
  module FavoritorLib
5
5
  def build_result_for_scopes(scopes)
6
+ return yield(scopes) unless scopes.is_a?(Array)
6
7
  return if scopes.empty?
7
8
 
8
- scopes = sanitized_scopes(scopes)
9
- result = scopes.map { |scope| [scope, yield(scope)] }.to_h
10
-
11
- return result[scopes.first] if scopes.size == 1
12
-
13
- result
9
+ sanitized_scopes(scopes).map { |scope| [scope, yield(scope)] }.to_h
14
10
  end
15
11
 
16
12
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActsAsFavoritor
4
- VERSION = '4.0.0'
4
+ VERSION = '5.0.1'
5
5
  end
@@ -13,7 +13,8 @@ class ActsAsFavoritorGenerator < Rails::Generators::Base
13
13
  if ActiveRecord::Base.timestamped_migrations
14
14
  Time.now.utc.strftime('%Y%m%d%H%M%S')
15
15
  else
16
- format('%.3d', current_migration_number(dirname) + 1)
16
+ format('%<migration_number>.3d',
17
+ migration_number: current_migration_number(dirname) + 1)
17
18
  end
18
19
  end
19
20
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_favoritor
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 5.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Hübotter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-05 00:00:00.000000000 Z
11
+ date: 2020-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rails
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -145,14 +159,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
145
159
  requirements:
146
160
  - - ">="
147
161
  - !ruby/object:Gem::Version
148
- version: '2.3'
162
+ version: '2.5'
149
163
  required_rubygems_version: !ruby/object:Gem::Requirement
150
164
  requirements:
151
165
  - - ">="
152
166
  - !ruby/object:Gem::Version
153
167
  version: '0'
154
168
  requirements: []
155
- rubygems_version: 3.0.3
169
+ rubygems_version: 3.1.4
156
170
  signing_key:
157
171
  specification_version: 4
158
172
  summary: A Rubygem to add Favorite, Follow, Vote, etc. functionality to ActiveRecord