acts_as_favoritor 4.0.1 → 4.0.2

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: c9a4ceadc5d7cd5517f8f4f6d234b5588f991e43271cf268be6df619400be3ac
4
- data.tar.gz: 9aaa2da1aad9087ddd85fe0b9bc7454c27fba2d704fe3a63b314fae77861826f
3
+ metadata.gz: ba4bae7bff5166a5903eccb9ed03fdc8905844309dac8b509d805dc945f09a4e
4
+ data.tar.gz: 61061b39d5f29094fe329c00aac91df4988667522c945f859c4762381da4a117
5
5
  SHA512:
6
- metadata.gz: f277da7298d714bb96c01c3899ba973d631b324164a6472730480a6d50cc70892d401725004377addc8d29287348a10954f034e813e2e7ad69d6f21f200cb3f0
7
- data.tar.gz: 65e9de4bf1b195b9d831efa02a86e91f2248fc1e5629e9c729a98ab146351a38dd609290adebd48cef236bf605b2f92cc132ad2350c93e2fb68f9f9c86271752
6
+ metadata.gz: eb97712eb98769614cd99a910633ec0b397feec7d55282f42884915c8246732f0b1b611ace2dae13c5360c998ba49f3ac02b0c9a8a7421b9ac2a3e57714493e0
7
+ data.tar.gz: 726d2a2aec78589821dabc017d5f53b8a142d961a3caa21eae436d5d6cbda4b231d613305ae8099aae4340addc09be508b68d09479b104d199c3fc586ec1c0cc
data/README.md CHANGED
@@ -92,48 +92,44 @@ user.favorite(book)
92
92
  # `user` removes `book` from favorites.
93
93
  user.unfavorite(book)
94
94
 
95
- # Whether `user` has marked `book` as his favorite. Returns `true` or `false`.
95
+ # Whether `user` has marked `book` as his favorite.
96
96
  user.favorited?(book)
97
97
 
98
- # Returnes `user`'s favorites that have not been blocked as an array of `Favorite` records.
98
+ # Returns an Active Record relation of `user`'s `Favorite` records that have not been blocked.
99
99
  user.all_favorites
100
100
 
101
- # Returns all favorited objects of `user` as an array (unblocked). This can be a collection of different object types, e.g.: `User`, `Book`.
101
+ # Returns an array of all unblocked favorited objects of `user`. This can be a collection of different object types, e.g.: `User`, `Book`.
102
102
  user.all_favorited
103
103
 
104
- # Returns an array of `Favorite` records where the `favoritable_type` is `Book`.
104
+ # Returns an Active Record relation of `Favorite` records where the `favoritable_type` is `Book`.
105
105
  user.favorites_by_type('Book')
106
106
 
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`.
107
+ # Returns an Active Record relation of all favorited objects of `user` where `favoritable_type` is 'Book'.
108
108
  user.favorited_by_type('Book')
109
109
 
110
- # Returns the exact same result as `user.favorited_by_type 'User'`.
110
+ # Returns the exact same as `user.favorited_by_type('User')`.
111
111
  user.favorited_users
112
112
 
113
- # Whether `user` has been blocked by `book`. Returns `true` or `false`.
113
+ # Whether `user` has been blocked by `book`.
114
114
  user.blocked_by?(book)
115
115
 
116
- # Returns an array including all blocked Favorite records.
116
+ # Returns an array of all favoritables that blocked `user`.
117
117
  user.blocked_by
118
118
  ```
119
119
 
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
120
  ### `acts_as_favoritable` methods
125
121
 
126
122
  ```ruby
127
123
  # Returns all favoritors of a model that `acts_as_favoritable`
128
124
  book.favoritors
129
125
 
130
- # Returns an array of records with type `User` following `book`.
126
+ # Returns an Active Record relation of records with type `User` following `book`.
131
127
  book.favoritors_by_type('User')
132
128
 
133
- # Returns the exact same as `book.favoritors_by_type 'User'`.
129
+ # Returns the exact same as `book.favoritors_by_type('User')`.
134
130
  book.user_favoritors
135
131
 
136
- # Whether `book` has been favorited by `user`. Returns `true` or `false`.
132
+ # Whether `book` has been favorited by `user`.
137
133
  book.favorited_by?(user)
138
134
 
139
135
  # Block a favoritor
@@ -142,30 +138,26 @@ book.block(user)
142
138
  # Unblock a favoritor
143
139
  book.unblock(user)
144
140
 
145
- # Whether `book` has blocked `user` as favoritor. Returns `true` or `false`.
141
+ # Whether `book` has blocked `user` as favoritor.
146
142
  book.blocked?(user)
147
143
 
148
- # Returns an array including all blocked Favoritor records.
144
+ # Returns an array of all blocked favoritors.
149
145
  book.blocked
150
146
  ```
151
147
 
152
- These methods take an optional hash parameter of ActiveRecord options (`:limit`, `:order`, etc...)
153
-
154
- favoritors_by_type, favoritors, blocks
155
-
156
148
  ### `Favorite` model
157
149
 
158
150
  ```ruby
159
- # Returns all `Favorite` records where `blocked` is `false`.
151
+ # Returns an Active Record relation of all `Favorite` records where `blocked` is `false`.
160
152
  Favorite.unblocked
161
153
 
162
- # Returns all `Favorite` records where `blocked` is `true`.
154
+ # Returns an Active Record relation of all `Favorite` records where `blocked` is `true`.
163
155
  Favorite.blocked
164
156
 
165
- # Returns all favorites of `user`, including those who were blocked.
157
+ # Returns an Active Record relation of all favorites of `user`, including those who were blocked.
166
158
  Favorite.for_favoritor(user)
167
159
 
168
- # Returns all favoritors of `book`, including those who were blocked.
160
+ # Returns an Active Record relation of all favoritors of `book`, including those who were blocked.
169
161
  Favorite.for_favoritable(book)
170
162
  ```
171
163
 
@@ -55,8 +55,11 @@ module ActsAsFavoritor
55
55
  scopes: [ActsAsFavoritor.configuration
56
56
  .default_scope])
57
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
+ favoritor_type.constantize.includes(:favorites)
59
+ .where(favorites: {
60
+ blocked: false, favoritable_id: id,
61
+ favoritable_type: self.class.name, scope: scope
62
+ })
60
63
  end
61
64
  end
62
65
 
@@ -49,7 +49,7 @@ module ActsAsFavoritor
49
49
  self.class.build_result_for_scopes scopes do |scope|
50
50
  return nil if self == favoritable
51
51
 
52
- inc_cache(scope) if ActsAsFavoritor.configuration.cache
52
+ inc_cache(favoritable, scope) if ActsAsFavoritor.configuration.cache
53
53
 
54
54
  favorites.for_favoritable(favoritable).send("#{scope}_list")
55
55
  .first_or_create!
@@ -62,7 +62,7 @@ module ActsAsFavoritor
62
62
  favorite_record = get_favorite(favoritable, scope)
63
63
  return nil unless favorite_record.present?
64
64
 
65
- dec_cache(scope) if ActsAsFavoritor.configuration.cache
65
+ dec_cache(favoritable, scope) if ActsAsFavoritor.configuration.cache
66
66
  favorite_record.destroy!
67
67
  end
68
68
  end
@@ -101,8 +101,11 @@ module ActsAsFavoritor
101
101
  scopes: [ActsAsFavoritor.configuration
102
102
  .default_scope])
103
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)
104
+ favoritable_type.constantize.includes(:favorited)
105
+ .where(favorites: {
106
+ blocked: false, favoritor_id: id,
107
+ favoritor_type: self.class.name, scope: scope
108
+ })
106
109
  end
107
110
  end
108
111
 
@@ -129,7 +132,7 @@ module ActsAsFavoritor
129
132
  end
130
133
 
131
134
  # rubocop:disable Metrics/AbcSize
132
- def inc_cache(scope)
135
+ def inc_cache(favoritable, scope)
133
136
  favoritor_score[scope] = (favoritor_score[scope] || 0) + 1
134
137
  favoritor_total[scope] = (favoritor_total[scope] || 0) + 1
135
138
  save!
@@ -141,7 +144,7 @@ module ActsAsFavoritor
141
144
  favoritable.save!
142
145
  end
143
146
 
144
- def dec_cache(scope)
147
+ def dec_cache(favoritable, scope)
145
148
  favoritor_score[scope] = (favoritor_score[scope] || 0) - 1
146
149
  favoritor_score.delete(scope) unless favoritor_score[scope].positive?
147
150
  save!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActsAsFavoritor
4
- VERSION = '4.0.1'
4
+ VERSION = '4.0.2'
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.1
4
+ version: 4.0.2
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-10 00:00:00.000000000 Z
11
+ date: 2019-05-17 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