acts_as_favoritor 3.0.0 → 4.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: 184296038ccc8938d320282f82c180549f13e810ce0d33d45e5999960efa437c
4
- data.tar.gz: 7b4bbb4d41550563d65da40a205cee21ee9f27bb100c47d99e345140992d1b2f
3
+ metadata.gz: 688cadb90a8c8e5fb799d2973aaffa6345e756456640fdba0c3d9f44715b92c2
4
+ data.tar.gz: 3b9a838d6fdf2a011494ca5822ca8f54290738d1a81ddb82f36e051f8f3630cc
5
5
  SHA512:
6
- metadata.gz: 1aeb504450b30f447c5beb3a480a3f26ab71a3d0786b853276a2e2ec8c4c9c6a87a4d25145ab31f954de3206062600a4e733baa7ac37fff9657bf6fe8bda0235
7
- data.tar.gz: 5280a639b92064db05318f2a5b5a3158878e97bb16eb218d2e42f05436ab857e8bdf8fc3528c71f55e9d7d3386652046e8b8e460ecdf3cf7b0327f60dcb6cdca
6
+ metadata.gz: c1a3319925be822442579ec4d022b454b52649b5a95fa5949fdd7498420bb38038889136bb39c2da96f5511713a398eaf3a372a578ad4c8b6d1edd9089188ff5
7
+ data.tar.gz: 52f518e8ab2d8c66c6fdd47f3bc57329745c3cdf28bd1f3ac41b13b3214d4d8ce9bad2549415b3098326437ec2747d90baf99c27411973991adcc82755ac79e2
data/README.md CHANGED
@@ -90,7 +90,7 @@ user = User.find(1)
90
90
  user.favorite(book)
91
91
 
92
92
  # `user` removes `book` from favorites.
93
- user.remove_favorite(book)
93
+ user.unfavorite(book)
94
94
 
95
95
  # Whether `user` has marked `book` as his favorite. Returns `true` or `false`.
96
96
  user.favorited?(book)
@@ -110,17 +110,11 @@ user.favorited_by_type('Book')
110
110
  # Returns the exact same result as `user.favorited_by_type 'User'`.
111
111
  user.favorited_users
112
112
 
113
- # Block a favoritable
114
- user.block(book)
113
+ # Whether `user` has been blocked by `book`. Returns `true` or `false`.
114
+ user.blocked_by?(book)
115
115
 
116
- # Unblock a favoritable
117
- user.unblock(book)
118
-
119
- # Whether `user` has blocked `book`. Returns `true` or `false`.
120
- user.blocked?(book)
121
-
122
- # Returns an array including all blocked Favoritable records.
123
- user.blocks
116
+ # Returns an array including all blocked Favorite records.
117
+ user.blocked_by
124
118
  ```
125
119
 
126
120
  These methods take an optional hash parameter of ActiveRecord options (`:limit`, `:order`, etc...)
@@ -152,7 +146,7 @@ book.unblock(user)
152
146
  book.blocked?(user)
153
147
 
154
148
  # Returns an array including all blocked Favoritor records.
155
- book.blocks
149
+ book.blocked
156
150
  ```
157
151
 
158
152
  These methods take an optional hash parameter of ActiveRecord options (`:limit`, `:order`, etc...)
@@ -162,17 +156,11 @@ These methods take an optional hash parameter of ActiveRecord options (`:limit`,
162
156
  ### `Favorite` model
163
157
 
164
158
  ```ruby
165
- # Scopes
166
- ## Returns all `Favorite` records where `blocked` is `false`.
159
+ # Returns all `Favorite` records where `blocked` is `false`.
167
160
  Favorite.unblocked
168
- ## Returns all `Favorite` records where `blocked` is `true`.
169
- Favorite.blocked
170
- ## Returns an ordered array of the latest create `Favorite` records.
171
- Favorite.descending
172
161
 
173
- # Returns all `Favorite` records in an array, which have been created in a specified timeframe. Default is 2 weeks.
174
- Favorite.recent
175
- Favorite.recent(1.month.ago)
162
+ # Returns all `Favorite` records where `blocked` is `true`.
163
+ Favorite.blocked
176
164
 
177
165
  # Returns all favorites of `user`, including those who were blocked.
178
166
  Favorite.for_favoritor(user)
@@ -193,10 +181,9 @@ So lets see how this works:
193
181
 
194
182
  ```ruby
195
183
  user.favorite(book, scopes: [:favorite, :watching])
196
- user.remove_favorite(book, scopes: [:watching])
184
+ user.unfavorite(book, scopes: [:watching])
197
185
  second_user = User.find(2)
198
186
  user.favorite(second_user, scopes: [:follow])
199
- book.block(user, scopes: [:all]) # applies to all scopes
200
187
  ```
201
188
 
202
189
  That's simple!
@@ -205,7 +192,7 @@ When you call a method which returns something while specifying multiple scopes,
205
192
 
206
193
  ```ruby
207
194
  user.favorited?(book, scopes: [:favorite, :watching]) # => { favorite: true, watching: false }
208
- user.favorited?(book, scopes: [:all]) # => true
195
+ user.favorited?(book, scopes: [:favorite]) # => true
209
196
  ```
210
197
 
211
198
  `acts_as_favoritor` also provides some handy scopes for you to call on the `Favorite` model:
@@ -219,8 +206,6 @@ Favorite.send("#{my_scope}_list")
219
206
  Favorite.favorite_list
220
207
  ### Returns all `Favorite` records where `scope` is `watching`
221
208
  Favorite.watching_list
222
- ### Very unnecessary, but `all_list` returns literally all `Favorite` records
223
- Favorite.all_list
224
209
  ```
225
210
 
226
211
  ### Caching
@@ -11,12 +11,15 @@ module ActsAsFavoritor
11
11
  end
12
12
 
13
13
  class Configuration
14
+ DEFAULT_SCOPE = :favorite
15
+ DEFAULT_CACHE = false
16
+
14
17
  attr_accessor :default_scope
15
18
  attr_accessor :cache
16
19
 
17
20
  def initialize
18
- @default_scope = 'favorite'
19
- @cache = false
21
+ @default_scope = DEFAULT_SCOPE
22
+ @cache = DEFAULT_CACHE
20
23
  end
21
24
  end
22
25
  end
@@ -16,8 +16,8 @@ module ActsAsFavoritor
16
16
  has_many :favorited, as: :favoritable, dependent: :destroy,
17
17
  class_name: 'Favorite'
18
18
 
19
+ extend ActsAsFavoritor::FavoritorLib
19
20
  include ActsAsFavoritor::Favoritable::InstanceMethods
20
- include ActsAsFavoritor::FavoritorLib
21
21
  end
22
22
  end
23
23
 
@@ -45,7 +45,7 @@ module ActsAsFavoritor
45
45
  end
46
46
 
47
47
  def favoritors(scopes: [ActsAsFavoritor.configuration.default_scope])
48
- build_result_for_scopes scopes do |scope|
48
+ self.class.build_result_for_scopes scopes do |scope|
49
49
  favorited.includes(:favoritor).unblocked.send("#{scope}_list")
50
50
  .map(&:favoritor)
51
51
  end
@@ -54,7 +54,7 @@ module ActsAsFavoritor
54
54
  def favoritors_by_type(favoritor_type,
55
55
  scopes: [ActsAsFavoritor.configuration
56
56
  .default_scope])
57
- build_result_for_scopes scopes do |scope|
57
+ self.class.build_result_for_scopes scopes do |scope|
58
58
  favorited.unblocked.send("#{scope}_list")
59
59
  .for_favoritor_type(favoritor_type).map(&:favoritor)
60
60
  end
@@ -62,7 +62,7 @@ module ActsAsFavoritor
62
62
 
63
63
  def favorited_by?(favoritor,
64
64
  scopes: [ActsAsFavoritor.configuration.default_scope])
65
- build_result_for_scopes scopes do |scope|
65
+ self.class.build_result_for_scopes scopes do |scope|
66
66
  favorited.unblocked.send("#{scope}_list").for_favoritor(favoritor)
67
67
  .first.present?
68
68
  end
@@ -70,8 +70,8 @@ module ActsAsFavoritor
70
70
 
71
71
  def block(favoritor,
72
72
  scopes: [ActsAsFavoritor.configuration.default_scope])
73
- build_result_for_scopes scopes do |scope|
74
- get_favorite_for(favoritor, scope).block! || Favorite.create(
73
+ self.class.build_result_for_scopes scopes do |scope|
74
+ get_favorite_for(favoritor, scope)&.block! || Favorite.create(
75
75
  favoritable: self,
76
76
  favoritor: favoritor,
77
77
  blocked: true,
@@ -82,21 +82,21 @@ module ActsAsFavoritor
82
82
 
83
83
  def unblock(favoritor,
84
84
  scopes: [ActsAsFavoritor.configuration.default_scope])
85
- build_result_for_scopes scopes do |scope|
85
+ self.class.build_result_for_scopes scopes do |scope|
86
86
  get_favorite_for(favoritor, scope)&.update(blocked: false)
87
87
  end
88
88
  end
89
89
 
90
90
  def blocked?(favoritor,
91
91
  scopes: [ActsAsFavoritor.configuration.default_scope])
92
- build_result_for_scopes scopes do |scope|
92
+ self.class.build_result_for_scopes scopes do |scope|
93
93
  favorited.blocked.send("#{scope}_list").for_favoritor(favoritor).first
94
94
  .present?
95
95
  end
96
96
  end
97
97
 
98
- def blocks(scopes: [ActsAsFavoritor.configuration.default_scope])
99
- build_result_for_scopes scopes do |scope|
98
+ def blocked(scopes: [ActsAsFavoritor.configuration.default_scope])
99
+ self.class.build_result_for_scopes scopes do |scope|
100
100
  favorited.includes(:favoritor).blocked.send("#{scope}_list")
101
101
  .map(&:favoritor)
102
102
  end
@@ -2,12 +2,11 @@
2
2
 
3
3
  module ActsAsFavoritor
4
4
  module FavoriteScopes
5
- # Allows magic names on send(scope + '_list') - returns favorite records of
6
- # certain scope.
7
- # e.g. favoritors == favoritors.send('favorite_list')
5
+ DEFAULT_PARENTS = [ApplicationRecord, ActiveRecord::Base].freeze
6
+
8
7
  def method_missing(method, *args)
9
8
  if method.to_s[/(.+)_list/]
10
- where(scope: $1.singularize)
9
+ where(scope: $1.singularize.to_sym)
11
10
  else
12
11
  super
13
12
  end
@@ -17,11 +16,6 @@ module ActsAsFavoritor
17
16
  super || method.to_s[/(.+)_list/]
18
17
  end
19
18
 
20
- def all_list
21
- all
22
- end
23
-
24
- # returns favorite records where favoritor is the record passed in.
25
19
  def for_favoritor(favoritor)
26
20
  where(
27
21
  favoritor_id: favoritor.id,
@@ -29,7 +23,6 @@ module ActsAsFavoritor
29
23
  )
30
24
  end
31
25
 
32
- # returns favorite records where favoritable is the record passed in.
33
26
  def for_favoritable(favoritable)
34
27
  where(
35
28
  favoritable_id: favoritable.id,
@@ -37,34 +30,31 @@ module ActsAsFavoritor
37
30
  )
38
31
  end
39
32
 
40
- # returns favorite records where favoritor_type is the record passed in.
41
33
  def for_favoritor_type(favoritor_type)
42
34
  where(favoritor_type: favoritor_type)
43
35
  end
44
36
 
45
- # returns favorite records where favoritable_type is the record passed in.
46
37
  def for_favoritable_type(favoritable_type)
47
38
  where(favoritable_type: favoritable_type)
48
39
  end
49
40
 
50
- # returns favorite records from past 2 weeks with default parameter.
51
- def recent(from)
52
- where('created_at > ?', (from || 2.weeks.ago).to_s(:db))
53
- end
54
-
55
- # returns favorite records in descending order.
56
- def descending
57
- order('favorites.created_at desc')
58
- end
59
-
60
- # returns unblocked favorite records.
61
41
  def unblocked
62
42
  where(blocked: false)
63
43
  end
64
44
 
65
- # returns blocked favorite records.
66
45
  def blocked
67
46
  where(blocked: true)
68
47
  end
48
+
49
+ private
50
+
51
+ def parent_class_name(object)
52
+ if DEFAULT_PARENTS.include?(object.class.superclass) ||
53
+ !object.class.respond_to?(:base_class)
54
+ return object.class.name
55
+ end
56
+
57
+ object.class.base_class.name
58
+ end
69
59
  end
70
60
  end
@@ -15,8 +15,8 @@ module ActsAsFavoritor
15
15
 
16
16
  has_many :favorites, as: :favoritor, dependent: :destroy
17
17
 
18
+ extend ActsAsFavoritor::FavoritorLib
18
19
  include ActsAsFavoritor::Favoritor::InstanceMethods
19
- include ActsAsFavoritor::FavoritorLib
20
20
  end
21
21
  end
22
22
 
@@ -46,45 +46,43 @@ module ActsAsFavoritor
46
46
 
47
47
  def favorite(favoritable,
48
48
  scopes: [ActsAsFavoritor.configuration.default_scope])
49
- build_result_for_scopes scopes do |scope|
50
- return if self == favoritable
51
- return if scope == :all
49
+ self.class.build_result_for_scopes scopes do |scope|
50
+ return nil if self == favoritable
52
51
 
53
52
  inc_cache if ActsAsFavoritor.configuration.cache
54
53
 
55
- favorites.where(
56
- favoritable_id: favoritable.id,
57
- favoritable_type: parent_class_name(favoritable),
58
- scope: scope
59
- ).first_or_create!
54
+ favorites.for_favoritable(favoritable).send("#{scope}_list")
55
+ .first_or_create!
60
56
  end
61
57
  end
62
58
 
63
- def remove_favorite(favoritable,
64
- scopes: [ActsAsFavoritor.configuration.default_scope])
65
- build_result_for_scopes scopes do |scope|
66
- dec_cache if ActsAsFavoritor.configuration.cache
59
+ 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
+ return nil unless favorite_record.present?
67
64
 
68
- get_favorite(favoritable, scope)&.destroy
65
+ dec_cache if ActsAsFavoritor.configuration.cache
66
+ favorite_record.destroy!
69
67
  end
70
68
  end
71
69
 
72
70
  def favorited?(favoritable,
73
71
  scopes: [ActsAsFavoritor.configuration.default_scope])
74
- build_result_for_scopes scopes do |scope|
72
+ self.class.build_result_for_scopes scopes do |scope|
75
73
  Favorite.unblocked.send("#{scope}_list").for_favoritor(self)
76
74
  .for_favoritable(favoritable).size.positive?
77
75
  end
78
76
  end
79
77
 
80
78
  def all_favorites(scopes: [ActsAsFavoritor.configuration.default_scope])
81
- build_result_for_scopes scopes do |scope|
79
+ self.class.build_result_for_scopes scopes do |scope|
82
80
  favorites.unblocked.send("#{scope}_list")
83
81
  end
84
82
  end
85
83
 
86
84
  def all_favorited(scopes: [ActsAsFavoritor.configuration.default_scope])
87
- build_result_for_scopes scopes do |scope|
85
+ self.class.build_result_for_scopes scopes do |scope|
88
86
  favorites.unblocked.send("#{scope}_list").includes(:favoritable)
89
87
  .map(&:favoritable)
90
88
  end
@@ -93,8 +91,8 @@ module ActsAsFavoritor
93
91
  def favorites_by_type(favoritable_type,
94
92
  scopes: [ActsAsFavoritor.configuration
95
93
  .default_scope])
96
- build_result_for_scopes scopes do |scope|
97
- favorites.unblocked.send("#{scope}_list").includes(:favoritable)
94
+ self.class.build_result_for_scopes scopes do |scope|
95
+ favorites.unblocked.send("#{scope}_list")
98
96
  .for_favoritable_type(favoritable_type)
99
97
  end
100
98
  end
@@ -102,41 +100,22 @@ module ActsAsFavoritor
102
100
  def favorited_by_type(favoritable_type,
103
101
  scopes: [ActsAsFavoritor.configuration
104
102
  .default_scope])
105
- build_result_for_scopes scopes do |scope|
106
- favorites.unblocked.send("#{scope}_list")
103
+ self.class.build_result_for_scopes scopes do |scope|
104
+ favorites.unblocked.send("#{scope}_list").includes(:favoritable)
107
105
  .for_favoritable_type(favoritable_type).map(&:favoritable)
108
106
  end
109
107
  end
110
108
 
111
- def block(favoritable,
112
- scopes: [ActsAsFavoritor.configuration.default_scope])
113
- build_result_for_scopes scopes do |scope|
114
- get_favorite(favoritable, scope).block! || Favorite.create(
115
- favoritable: favoritable,
116
- favoritor: self,
117
- blocked: true,
118
- scope: scope
119
- )
120
- end
121
- end
122
-
123
- def unblock(favoritable,
124
- scopes: [ActsAsFavoritor.configuration.default_scope])
125
- build_result_for_scopes scopes do |scope|
126
- get_favorite(favoritable, scope)&.update(blocked: false)
127
- end
128
- end
129
-
130
- def blocked?(favoritable,
131
- scopes: [ActsAsFavoritor.configuration.default_scope])
132
- build_result_for_scopes scopes do |scope|
109
+ def blocked_by?(favoritable,
110
+ scopes: [ActsAsFavoritor.configuration.default_scope])
111
+ self.class.build_result_for_scopes scopes do |scope|
133
112
  Favorite.blocked.send("#{scope}_list").for_favoritor(self)
134
113
  .for_favoritable(favoritable).size.positive?
135
114
  end
136
115
  end
137
116
 
138
- def blocks(scopes: [ActsAsFavoritor.configuration.default_scope])
139
- build_result_for_scopes scopes do |scope|
117
+ def blocked_by(scopes: [ActsAsFavoritor.configuration.default_scope])
118
+ self.class.build_result_for_scopes scopes do |scope|
140
119
  favorites.includes(:favoritable).blocked.send("#{scope}_list")
141
120
  .map(&:favoritable)
142
121
  end
@@ -2,19 +2,6 @@
2
2
 
3
3
  module ActsAsFavoritor
4
4
  module FavoritorLib
5
- private
6
-
7
- DEFAULT_PARENTS = [ApplicationRecord, ActiveRecord::Base].freeze
8
-
9
- # Retrieves the parent class name if using STI.
10
- def parent_class_name(obj)
11
- unless DEFAULT_PARENTS.include? obj.class.superclass
12
- return obj.class.base_class.name
13
- end
14
-
15
- obj.class.name
16
- end
17
-
18
5
  def build_result_for_scopes(scopes)
19
6
  return if scopes.empty?
20
7
 
@@ -26,6 +13,8 @@ module ActsAsFavoritor
26
13
  result
27
14
  end
28
15
 
16
+ private
17
+
29
18
  def sanitized_scopes(scopes)
30
19
  scopes.map(&:to_sym)
31
20
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActsAsFavoritor
4
- VERSION = '3.0.0'
4
+ VERSION = '4.0.0'
5
5
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  ActsAsFavoritor.configure do |config|
4
4
  # Specify your default scope. Learn more about scopes here: https://github.com/jonhue/acts_as_favoritor#scopes
5
- # config.default_scope = 'favorite'
5
+ # config.default_scope = :favorite
6
6
 
7
7
  # Enable caching. Learn more about caching here: https://github.com/jonhue/acts_as_favoritor#caching
8
8
  # config.cache = false
@@ -5,13 +5,19 @@ class ActsAsFavoritorMigration < ActiveRecord::Migration<%= migration_version %>
5
5
  create_table :favorites, force: true do |t|
6
6
  t.references :favoritable, polymorphic: true, null: false
7
7
  t.references :favoritor, polymorphic: true, null: false
8
- t.string :scope, default: ActsAsFavoritor.configuration.default_scope, null: false, index: true
8
+ t.string :scope, default: ActsAsFavoritor.configuration.default_scope,
9
+ null: false,
10
+ index: true
9
11
  t.boolean :blocked, default: false, null: false, index: true
10
12
  t.timestamps
11
13
  end
12
14
 
13
- add_index :favorites, ['favoritor_id', 'favoritor_type'], name: 'fk_favorites'
14
- add_index :favorites, ['favoritable_id', 'favoritable_type'], name: 'fk_favoritables'
15
+ add_index :favorites,
16
+ ['favoritor_id', 'favoritor_type'],
17
+ name: 'fk_favorites'
18
+ add_index :favorites,
19
+ ['favoritable_id', 'favoritable_type'],
20
+ name: 'fk_favoritables'
15
21
  end
16
22
 
17
23
  def self.down
@@ -1,13 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Favorite < ApplicationRecord
4
- extend ActsAsFavoritor::FavoritorLib
5
4
  extend ActsAsFavoritor::FavoriteScopes
6
5
 
7
6
  belongs_to :favoritable, polymorphic: true
8
7
  belongs_to :favoritor, polymorphic: true
9
8
 
10
9
  def block!
11
- update(blocked: true)
10
+ update!(blocked: true)
12
11
  end
13
12
  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: 3.0.0
4
+ version: 4.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-05-04 00:00:00.000000000 Z
11
+ date: 2019-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord