acts_as_favoritor 4.0.3 → 5.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: 45ca952d9906033e84ecc6ff24aca3934457d803c51f979d59c561847b1d61d6
4
- data.tar.gz: 941c85b77b52b66360a6ba7df70227c2e7cc32fc5fe0230f6191e2d20e6afd3e
3
+ metadata.gz: 3e0e21ccec6a8d67036d67b66db5851d554e046eb5a7d8ba592eba0a98b8ae48
4
+ data.tar.gz: 951039111774466e4bf8347d8e5de83af8960817b8bb934611a06b228b854198
5
5
  SHA512:
6
- metadata.gz: bae098e81f7b219469fd3e2a18446914f46fe9ba65edc58f8f36ecd1ade9393f9a877ea8b96795abb4bd7324357fac01bdf2910a630a58828b9c5a984914076c
7
- data.tar.gz: 0adfa2859658a591c472279295197c7c97bf3af1157210e992fafa384fc0dd083c6e7f3b791395b84e0638b25ff25c8ab25a20ee04996ec1975fe75df0b0b1f1
6
+ metadata.gz: 390486da05c0e6d4370b433cd6ae774371595726b376febff627dfa5c2bd1f0b154abd57ff4bf55808d4379f4bc8e20552c7d73ffdd19fe60c89e93651f050ed
7
+ data.tar.gz: a53d91399cb196ba971ab0170c49da2fa7b996cd65d3e1371f095049092e3f820f5740011ac90b97424b6571b64ddfccaa2e2d1bec3360f9aca3fc37aae2b940
data/README.md CHANGED
@@ -8,20 +8,22 @@ You are able to differentiate followers, favorites, watchers, votes and whatever
8
8
 
9
9
  ## Table of Contents
10
10
 
11
- * [Installation](#installation)
12
- * [Usage](#usage)
13
- * [Setup](#setup)
14
- * [`acts_as_favoritor` methods](#acts_as_favoritor-methods)
15
- * [`acts_as_favoritable` methods](#acts_as_favoritable-methods)
16
- * [`Favorite` model](#favorite-model)
17
- * [Scopes](#scopes)
18
- * [Caching](#caching)
19
- * [Configuration](#configuration)
20
- * [Testing](#testing)
21
- * [Release](#release)
22
- * [To do](#to-do)
23
- * [Contributing](#contributing)
24
- * [Semantic versioning](#semantic-versioning)
11
+ - [acts_as_favoritor](#actsasfavoritor)
12
+ - [Table of Contents](#table-of-contents)
13
+ - [Installation](#installation)
14
+ - [Usage](#usage)
15
+ - [Setup](#setup)
16
+ - [`acts_as_favoritor` methods](#actsasfavoritor-methods)
17
+ - [`acts_as_favoritable` methods](#actsasfavoritable-methods)
18
+ - [`Favorite` model](#favorite-model)
19
+ - [Scopes](#scopes)
20
+ - [Caching](#caching)
21
+ - [Configuration](#configuration)
22
+ - [Testing](#testing)
23
+ - [Release](#release)
24
+ - [To do](#to-do)
25
+ - [Contributing](#contributing)
26
+ - [Semantic Versioning](#semantic-versioning)
25
27
 
26
28
  ---
27
29
 
@@ -166,24 +168,25 @@ Using scopes with `acts_as_favoritor` enables you to Follow, Watch, Favorite, [.
166
168
 
167
169
  By default all of your favorites are scoped to `'favorite'`.
168
170
 
169
- 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.
171
+ 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.
170
172
 
171
173
  So lets see how this works:
172
174
 
173
175
  ```ruby
174
176
  user.favorite(book, scopes: [:favorite, :watching])
175
- user.unfavorite(book, scopes: [:watching])
177
+ user.unfavorite(book, scope: :watching)
176
178
  second_user = User.find(2)
177
- user.favorite(second_user, scopes: [:follow])
179
+ user.favorite(second_user, scope: :follow)
178
180
  ```
179
181
 
180
182
  That's simple!
181
183
 
182
- 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:
184
+ 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:
183
185
 
184
186
  ```ruby
185
187
  user.favorited?(book, scopes: [:favorite, :watching]) # => { favorite: true, watching: false }
186
- user.favorited?(book, scopes: [:favorite]) # => true
188
+ user.favorited?(book, scopes: [:favorite]) # => { favorite: true }
189
+ user.favorited?(book, scope: :favorite) # => true
187
190
  ```
188
191
 
189
192
  `acts_as_favoritor` also provides some handy scopes for you to call on the `Favorite` model:
@@ -44,37 +44,39 @@ module ActsAsFavoritor
44
44
  method.to_s[/favoritable_(.+)_total/]
45
45
  end
46
46
 
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")
47
+ def favoritors(scope: ActsAsFavoritor.configuration.default_scope,
48
+ scopes: nil)
49
+ self.class.build_result_for_scopes(scopes || scope) do |s|
50
+ favorited.includes(:favoritor).unblocked.send("#{s}_list")
50
51
  .map(&:favoritor)
51
52
  end
52
53
  end
53
54
 
54
55
  def favoritors_by_type(favoritor_type,
55
- scopes: [ActsAsFavoritor.configuration
56
- .default_scope])
57
- self.class.build_result_for_scopes scopes do |scope|
56
+ scope: ActsAsFavoritor.configuration.default_scope,
57
+ scopes: nil)
58
+ self.class.build_result_for_scopes(scopes || scope) do |s|
58
59
  favoritor_type.constantize.includes(:favorites)
59
60
  .where(favorites: {
60
61
  blocked: false, favoritable_id: id,
61
- favoritable_type: self.class.name, scope: scope
62
+ favoritable_type: self.class.name, scope: s
62
63
  })
63
64
  end
64
65
  end
65
66
 
66
67
  def favorited_by?(favoritor,
67
- scopes: [ActsAsFavoritor.configuration.default_scope])
68
- self.class.build_result_for_scopes scopes do |scope|
69
- favorited.unblocked.send("#{scope}_list").for_favoritor(favoritor)
68
+ scope: ActsAsFavoritor.configuration.default_scope,
69
+ scopes: nil)
70
+ self.class.build_result_for_scopes(scopes || scope) do |s|
71
+ favorited.unblocked.send("#{s}_list").for_favoritor(favoritor)
70
72
  .first.present?
71
73
  end
72
74
  end
73
75
 
74
- def block(favoritor,
75
- scopes: [ActsAsFavoritor.configuration.default_scope])
76
- self.class.build_result_for_scopes scopes do |scope|
77
- get_favorite_for(favoritor, scope)&.block! || Favorite.create(
76
+ def block(favoritor, scope: ActsAsFavoritor.configuration.default_scope,
77
+ scopes: nil)
78
+ self.class.build_result_for_scopes(scopes || scope) do |s|
79
+ get_favorite_for(favoritor, s)&.block! || Favorite.create(
78
80
  favoritable: self,
79
81
  favoritor: favoritor,
80
82
  blocked: true,
@@ -83,24 +85,26 @@ module ActsAsFavoritor
83
85
  end
84
86
  end
85
87
 
86
- def unblock(favoritor,
87
- scopes: [ActsAsFavoritor.configuration.default_scope])
88
- self.class.build_result_for_scopes scopes do |scope|
89
- get_favorite_for(favoritor, scope)&.update(blocked: false)
88
+ def unblock(favoritor, scope: ActsAsFavoritor.configuration.default_scope,
89
+ scopes: nil)
90
+ self.class.build_result_for_scopes(scopes || scope) do |s|
91
+ get_favorite_for(favoritor, s)&.update(blocked: false)
90
92
  end
91
93
  end
92
94
 
93
95
  def blocked?(favoritor,
94
- scopes: [ActsAsFavoritor.configuration.default_scope])
95
- self.class.build_result_for_scopes scopes do |scope|
96
- favorited.blocked.send("#{scope}_list").for_favoritor(favoritor).first
96
+ scope: ActsAsFavoritor.configuration.default_scope,
97
+ scopes: nil)
98
+ self.class.build_result_for_scopes(scopes || scope) do |s|
99
+ favorited.blocked.send("#{s}_list").for_favoritor(favoritor).first
97
100
  .present?
98
101
  end
99
102
  end
100
103
 
101
- def blocked(scopes: [ActsAsFavoritor.configuration.default_scope])
102
- self.class.build_result_for_scopes scopes do |scope|
103
- favorited.includes(:favoritor).blocked.send("#{scope}_list")
104
+ def blocked(scope: ActsAsFavoritor.configuration.default_scope,
105
+ scopes: nil)
106
+ self.class.build_result_for_scopes(scopes || scope) do |s|
107
+ favorited.includes(:favoritor).blocked.send("#{s}_list")
104
108
  .map(&:favoritor)
105
109
  end
106
110
  end
@@ -45,81 +45,88 @@ module ActsAsFavoritor
45
45
  end
46
46
 
47
47
  def favorite(favoritable,
48
- scopes: [ActsAsFavoritor.configuration.default_scope])
49
- self.class.build_result_for_scopes scopes do |scope|
48
+ scope: ActsAsFavoritor.configuration.default_scope,
49
+ scopes: nil)
50
+ self.class.build_result_for_scopes(scopes || scope) do |s|
50
51
  return nil if self == favoritable
51
52
 
52
- inc_cache(favoritable, scope) if ActsAsFavoritor.configuration.cache
53
+ inc_cache(favoritable, s) if ActsAsFavoritor.configuration.cache
53
54
 
54
- favorites.for_favoritable(favoritable).send("#{scope}_list")
55
+ favorites.for_favoritable(favoritable).send("#{s}_list")
55
56
  .first_or_create!
56
57
  end
57
58
  end
58
59
 
59
60
  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)
61
+ scope: ActsAsFavoritor.configuration.default_scope,
62
+ scopes: nil)
63
+ self.class.build_result_for_scopes(scopes || scope) do |s|
64
+ favorite_record = get_favorite(favoritable, s)
63
65
  return nil unless favorite_record.present?
64
66
 
65
- dec_cache(favoritable, scope) if ActsAsFavoritor.configuration.cache
67
+ dec_cache(favoritable, s) if ActsAsFavoritor.configuration.cache
66
68
  favorite_record.destroy!
67
69
  end
68
70
  end
69
71
 
70
72
  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)
73
+ scope: ActsAsFavoritor.configuration.default_scope,
74
+ scopes: nil)
75
+ self.class.build_result_for_scopes(scopes || scope) do |s|
76
+ Favorite.unblocked.send("#{s}_list").for_favoritor(self)
74
77
  .for_favoritable(favoritable).size.positive?
75
78
  end
76
79
  end
77
80
 
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")
81
+ def all_favorites(scope: ActsAsFavoritor.configuration.default_scope,
82
+ scopes: nil)
83
+ self.class.build_result_for_scopes(scopes || scope) do |s|
84
+ favorites.unblocked.send("#{s}_list")
81
85
  end
82
86
  end
83
87
 
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)
88
+ def all_favorited(scope: ActsAsFavoritor.configuration.default_scope,
89
+ scopes: nil)
90
+ self.class.build_result_for_scopes(scopes || scope) do |s|
91
+ favorites.unblocked.send("#{s}_list").includes(:favoritable)
87
92
  .map(&:favoritable)
88
93
  end
89
94
  end
90
95
 
91
96
  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")
97
+ scope: ActsAsFavoritor.configuration.default_scope,
98
+ scopes: nil)
99
+ self.class.build_result_for_scopes(scopes || scope) do |s|
100
+ favorites.unblocked.send("#{s}_list")
96
101
  .for_favoritable_type(favoritable_type)
97
102
  end
98
103
  end
99
104
 
100
105
  def favorited_by_type(favoritable_type,
101
- scopes: [ActsAsFavoritor.configuration
102
- .default_scope])
103
- self.class.build_result_for_scopes scopes do |scope|
106
+ scope: ActsAsFavoritor.configuration.default_scope,
107
+ scopes: nil)
108
+ self.class.build_result_for_scopes(scopes || scope) do |s|
104
109
  favoritable_type.constantize.includes(:favorited)
105
110
  .where(favorites: {
106
111
  blocked: false, favoritor_id: id,
107
- favoritor_type: self.class.name, scope: scope
112
+ favoritor_type: self.class.name, scope: s
108
113
  })
109
114
  end
110
115
  end
111
116
 
112
117
  def blocked_by?(favoritable,
113
- scopes: [ActsAsFavoritor.configuration.default_scope])
114
- self.class.build_result_for_scopes scopes do |scope|
115
- Favorite.blocked.send("#{scope}_list").for_favoritor(self)
118
+ scope: ActsAsFavoritor.configuration.default_scope,
119
+ scopes: nil)
120
+ self.class.build_result_for_scopes(scopes || scope) do |s|
121
+ Favorite.blocked.send("#{s}_list").for_favoritor(self)
116
122
  .for_favoritable(favoritable).size.positive?
117
123
  end
118
124
  end
119
125
 
120
- def blocked_by(scopes: [ActsAsFavoritor.configuration.default_scope])
121
- self.class.build_result_for_scopes scopes do |scope|
122
- favorites.includes(:favoritable).blocked.send("#{scope}_list")
126
+ def blocked_by(scope: ActsAsFavoritor.configuration.default_scope,
127
+ scopes: nil)
128
+ self.class.build_result_for_scopes(scopes || scope) do |s|
129
+ favorites.includes(:favoritable).blocked.send("#{s}_list")
123
130
  .map(&:favoritable)
124
131
  end
125
132
  end
@@ -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.3'
4
+ VERSION = '5.0.0'
5
5
  end
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.3
4
+ version: 5.0.0
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-10-10 00:00:00.000000000 Z
11
+ date: 2020-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord