acts_as_favoritor 2.0.1 → 2.1.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
  SHA256:
3
- metadata.gz: 1bda840e48829f86862b98f8bb559cd9d7310352af4edc26173d6b5b44ee815a
4
- data.tar.gz: d7e99df4d9541794b1c1d2828e6c0b024625cbe5c5ddb51f1158714ff62eeb87
3
+ metadata.gz: 7ff8778bb55338480286174f66774251914fa4ef19aabb79dd5588d3c6bf40dc
4
+ data.tar.gz: 239c8bfbacb746ac449a4682ec731adf4c85da8493d983bd210627c0ebd105bd
5
5
  SHA512:
6
- metadata.gz: eeb4bdf4c2443059da696a58d65055d4f06e9b233d469a383f13a243239b4e19b61dfd9a039ab234c3af85dd2191d91fc72ce61b596a227752038c9a689a73e7
7
- data.tar.gz: 9f92498cab6a74bbe770b6551aff0e77920a6e9e7f400174298a59578de80cc8d90b6442f80f6d772c2490732a1236e0e88cbe45db30b6aa3c7b19290d398220
6
+ metadata.gz: 262b98216f3e58c124de01bf1e78b29a8aceca911cb46726996e51d2fa7f43162a8f9100d17fc359c7bbb9faa427a58d8973d73de4c7979cafe981385880e931
7
+ data.tar.gz: 025be50ca7f9b0e3671a999a1e385ee6c2b2d89a7f339073d9d82c0c44550adc95cc0818ab5509b6e6f6f4156ff83771dfccd7f969566111bac0733312cb4134
data/LICENSE CHANGED
File without changes
data/README.md CHANGED
@@ -1,6 +1,6 @@
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) <img src="https://travis-ci.org/jonhue/acts_as_favoritor.svg?branch=master" />
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
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
 
@@ -12,20 +12,17 @@ You are able to differentiate followers, favorites, watchers, votes and whatever
12
12
 
13
13
  * [Installation](#installation)
14
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)
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
21
  * [Configuration](#configuration)
22
22
  * [Testing](#testing)
23
- * [Test Coverage](#test-coverage)
24
- * [To Do](#to-do)
23
+ * [To do](#to-do)
25
24
  * [Contributing](#contributing)
26
- * [Contributors](#contributors)
27
- * [Semantic versioning](#semantic-versioning)
28
- * [License](#license)
25
+ * [Semantic versioning](#semantic-versioning)
29
26
 
30
27
  ---
31
28
 
@@ -67,11 +64,11 @@ Add `acts_as_favoritable` to the models you want to be able to get favorited:
67
64
 
68
65
  ```ruby
69
66
  class User < ActiveRecord::Base
70
- acts_as_favoritable
67
+ acts_as_favoritable
71
68
  end
72
69
 
73
70
  class Book < ActiveRecord::Base
74
- acts_as_favoritable
71
+ acts_as_favoritable
75
72
  end
76
73
  ```
77
74
 
@@ -79,7 +76,7 @@ Specify which models can favorite other models by adding `acts_as_favoritor`:
79
76
 
80
77
  ```ruby
81
78
  class User < ActiveRecord::Base
82
- acts_as_favoritor
79
+ acts_as_favoritor
83
80
  end
84
81
  ```
85
82
 
@@ -314,7 +311,7 @@ You can configure acts_as_favoritor by passing a block to `configure`. This can
314
311
 
315
312
  ```ruby
316
313
  ActsAsFavoritor.configure do |config|
317
- config.default_scope = 'follow'
314
+ config.default_scope = 'follow'
318
315
  end
319
316
  ```
320
317
 
@@ -338,21 +335,15 @@ Tests are written with Shoulda on top of `Test::Unit` with Factory Girl being us
338
335
 
339
336
  `$ rake test`
340
337
 
341
- ### Test Coverage
338
+ 5. Run RuboCop
342
339
 
343
- Test coverage can be calculated using SimpleCov. Make sure you have the [simplecov gem](https://github.com/colszowka/simplecov) installed.
344
-
345
- 1. Uncomment SimpleCov in the Gemfile
346
- 2. Uncomment the relevant section in `test/test_helper.rb`
347
- 3. Run tests
348
-
349
- `$ rake test`
340
+ `$ bundle exec rubocop`
350
341
 
351
342
  ---
352
343
 
353
- ## To Do
344
+ ## To do
354
345
 
355
- [Here](https://github.com/jonhue/acts_as_favoritor/projects/1) is the full list of current projects.
346
+ We use [GitHub projects](https://github.com/jonhue/acts_as_favoritor/projects/1) to coordinate the work on this project.
356
347
 
357
348
  To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/acts_as_favoritor/issues/new).
358
349
 
@@ -362,38 +353,8 @@ To propose your ideas, initiate the discussion by adding a [new issue](https://g
362
353
 
363
354
  We hope that you will consider contributing to acts_as_favoritor. Please read this short overview for some information about how to get started:
364
355
 
365
- [Learn more about contributing to this repository](https://github.com/jonhue/acts_as_favoritor/blob/master/CONTRIBUTING.md), [Code of Conduct](https://github.com/jonhue/acts_as_favoritor/blob/master/CODE_OF_CONDUCT.md)
366
-
367
- ### Contributors
368
-
369
- Give the people some :heart: who are working on this project. See them all at:
370
-
371
- https://github.com/jonhue/acts_as_favoritor/graphs/contributors
356
+ [Learn more about contributing to this repository](CONTRIBUTING.md), [Code of Conduct](CODE_OF_CONDUCT.md)
372
357
 
373
358
  ### Semantic Versioning
374
359
 
375
360
  acts_as_favoritor follows Semantic Versioning 2.0 as defined at http://semver.org.
376
-
377
- ## License
378
-
379
- MIT License
380
-
381
- Copyright (c) 2017 Jonas Hübotter
382
-
383
- Permission is hereby granted, free of charge, to any person obtaining a copy
384
- of this software and associated documentation files (the "Software"), to deal
385
- in the Software without restriction, including without limitation the rights
386
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
387
- copies of the Software, and to permit persons to whom the Software is
388
- furnished to do so, subject to the following conditions:
389
-
390
- The above copyright notice and this permission notice shall be included in all
391
- copies or substantial portions of the Software.
392
-
393
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
394
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
395
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
396
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
397
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
398
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
399
- SOFTWARE.
@@ -1,14 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'acts_as_favoritor/version'
2
4
 
3
5
  module ActsAsFavoritor
6
+ require 'acts_as_favoritor/configuration'
4
7
 
5
- require 'acts_as_favoritor/configuration'
6
-
7
- autoload :Favoritor, 'acts_as_favoritor/favoritor'
8
- autoload :Favoritable, 'acts_as_favoritor/favoritable'
9
- autoload :FavoritorLib, 'acts_as_favoritor/favoritor_lib'
10
- autoload :FavoriteScopes, 'acts_as_favoritor/favorite_scopes'
11
-
12
- require 'acts_as_favoritor/railtie' if defined?(Rails::Railtie)
8
+ autoload :Favoritor, 'acts_as_favoritor/favoritor'
9
+ autoload :Favoritable, 'acts_as_favoritor/favoritable'
10
+ autoload :FavoritorLib, 'acts_as_favoritor/favoritor_lib'
11
+ autoload :FavoriteScopes, 'acts_as_favoritor/favorite_scopes'
13
12
 
13
+ require 'acts_as_favoritor/railtie' if defined?(Rails::Railtie)
14
14
  end
@@ -1,23 +1,22 @@
1
- module ActsAsFavoritor
2
-
3
- class << self
4
- attr_accessor :configuration
5
- end
6
-
7
- def self.configure
8
- self.configuration ||= Configuration.new
9
- yield configuration
10
- end
11
-
12
- class Configuration
13
-
14
- attr_accessor :default_scope
15
- attr_accessor :cache
16
-
17
- def initialize
18
- @default_scope = 'favorite'
19
- @cache = false
20
- end
21
-
22
- end
23
- end
1
+ # frozen_string_literal: true
2
+
3
+ module ActsAsFavoritor
4
+ class << self
5
+ attr_accessor :configuration
6
+ end
7
+
8
+ def self.configure
9
+ self.configuration ||= Configuration.new
10
+ yield configuration
11
+ end
12
+
13
+ class Configuration
14
+ attr_accessor :default_scope
15
+ attr_accessor :cache
16
+
17
+ def initialize
18
+ @default_scope = 'favorite'
19
+ @cache = false
20
+ end
21
+ end
22
+ end
@@ -1,319 +1,392 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActsAsFavoritor
2
- module Favoritable
4
+ module Favoritable
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
3
8
 
4
- def self.included base
5
- base.extend ClassMethods
6
- end
9
+ module ClassMethods
10
+ def acts_as_favoritable
11
+ has_many :favorited,
12
+ as: :favoritable, dependent: :destroy, class_name: 'Favorite'
13
+ include ActsAsFavoritor::Favoritable::InstanceMethods
14
+ include ActsAsFavoritor::FavoritorLib
7
15
 
8
- module ClassMethods
9
- def acts_as_favoritable
10
- has_many :favorited, as: :favoritable, dependent: :destroy, class_name: 'Favorite'
11
- include ActsAsFavoritor::Favoritable::InstanceMethods
12
- include ActsAsFavoritor::FavoritorLib
16
+ return unless ActsAsFavoritor.configuration.cache
17
+ serialize :favoritable_score, Hash
18
+ serialize :favoritable_total, Hash
19
+ end
20
+ end
13
21
 
14
- serialize :favoritable_cache, Hash if ActsAsFavoritor.configuration.cache
15
- end
22
+ module InstanceMethods
23
+ # Returns the number of favoritors a record has.
24
+ def favoritors_count(options = {})
25
+ if options.key?(:multiple_scopes) == false
26
+ validate_scopes(__method__, options)
27
+ elsif options[:multiple_scopes]
28
+ results = {}
29
+ options[:scope].each do |scope|
30
+ results[scope] = favorited.unblocked.send(scope + '_list').count
31
+ end
32
+ results
33
+ else
34
+ favorited.unblocked.send(options[:scope] + '_list').count
16
35
  end
36
+ end
17
37
 
18
- module InstanceMethods
19
-
20
- # Returns the number of favoritors a record has.
21
- def favoritors_count options = {}
22
- if options.has_key?(:multiple_scopes) == false
23
- validate_scopes __method__, options
24
- elsif options[:multiple_scopes]
25
- results = {}
26
- options[:scope].each do |scope|
27
- results[scope] = favorited.unblocked.send(scope + '_list').count
28
- end
29
- return results
30
- else
31
- return favorited.unblocked.send(options[:scope] + '_list').count
32
- end
33
- end
34
-
35
- # Returns the favoritors by a given type
36
- def favoritors_by_type favoritor_type, options = {}
37
- if options.has_key?(:multiple_scopes) == false
38
- options[:parameter] = favoritor_type
39
- validate_scopes __method__, options
40
- elsif options[:multiple_scopes]
41
- results = {}
42
- options[:scope].each do |scope|
43
- favorites = favoritor_type.constantize.joins(:favorites).where('favorites.blocked': false,
44
- 'favorites.favoritable_id': id,
45
- 'favorites.favoritable_type': parent_class_name(self),
46
- 'favorites.favoritor_type': favoritor_type,
47
- 'favorites.scope': scope)
48
- if options.has_key? :limit
49
- favorites = favorites.limit options[:limit]
50
- end
51
- if options.has_key? :includes
52
- favorites = favorites.includes options[:includes]
53
- end
54
- results[scope] = favorites
55
- end
56
- return results
57
- else
58
- favorites = favoritor_type.constantize.joins(:favorites).where('favorites.blocked': false,
59
- 'favorites.favoritable_id': id,
60
- 'favorites.favoritable_type': parent_class_name(self),
61
- 'favorites.favoritor_type': favoritor_type,
62
- 'favorites.scope': options[:scope])
63
- if options.has_key? :limit
64
- favorites = favorites.limit options[:limit]
65
- end
66
- if options.has_key? :includes
67
- favorites = favorites.includes options[:includes]
68
- end
69
- return favorites
70
- end
71
- end
72
-
73
- def favoritors_by_type_count favoritor_type, options = {}
74
- if options.has_key?(:multiple_scopes) == false
75
- options[:parameter] = favoritor_type
76
- validate_scopes __method__, options
77
- elsif options[:multiple_scopes]
78
- results = {}
79
- options[:scope].each do |scope|
80
- results[scope] = favorited.unblocked.send(scope + '_list').for_favoritor_type(favoritor_type).count
81
- end
82
- return results
83
- else
84
- return favorited.unblocked.send(options[:scope] + '_list').for_favoritor_type(favoritor_type).count
85
- end
86
- end
87
-
88
- # Allows magic names on favoritors_by_type
89
- # e.g. user_favoritors == favoritors_by_type 'User'
90
- # Allows magic names on favoritors_by_type_count
91
- # e.g. count_user_favoritors == favoritors_by_type_count 'User'
92
- def method_missing m, *args
93
- if m.to_s[/count_(.+)_favoritors/]
94
- favoritors_by_type_count $1.singularize.classify
95
- elsif m.to_s[/(.+)_favoritors/]
96
- favoritors_by_type $1.singularize.classify
97
- elsif m.to_s[/favoritable_(.+)_score/]
98
- favoritable_score[$1.singularize.classify] if ActsAsFavoritor.configuration.cache
99
- elsif m.to_s[/favoritable_(.+)_total/]
100
- favoritable_total[$1.singularize.classify] if ActsAsFavoritor.configuration.cache
101
- else
102
- super
103
- end
38
+ # Returns the favoritors by a given type.
39
+ def favoritors_by_type(favoritor_type, options = {})
40
+ if options.key?(:multiple_scopes) == false
41
+ options[:parameter] = favoritor_type
42
+ validate_scopes(__method__, options)
43
+ elsif options[:multiple_scopes]
44
+ results = {}
45
+ options[:scope].each do |scope|
46
+ favorites = favoritor_type.constantize.joins(:favorites)
47
+ favorites = favorites.where(
48
+ 'favorites.blocked': false,
49
+ 'favorites.favoritable_id': id,
50
+ 'favorites.favoritable_type': parent_class_name(self),
51
+ 'favorites.favoritor_type': favoritor_type,
52
+ 'favorites.scope': scope
53
+ )
54
+ favorites = favorites.limit(options[:limit]) if options.key?(:limit)
55
+ if options.key?(:includes)
56
+ favorites = favorites.includes(options[:includes])
104
57
  end
58
+ results[scope] = favorites
59
+ end
60
+ results
61
+ else
62
+ favorites = favoritor_type.constantize.joins(:favorites)
63
+ favorites = favorites.where(
64
+ 'favorites.blocked': false,
65
+ 'favorites.favoritable_id': id,
66
+ 'favorites.favoritable_type': parent_class_name(self),
67
+ 'favorites.favoritor_type': favoritor_type,
68
+ 'favorites.scope': options[:scope]
69
+ )
70
+ favorites = favorites.limit(options[:limit]) if options.key?(:limit)
71
+ if options.key?(:includes)
72
+ favorites = favorites.includes(options[:includes])
73
+ end
74
+ favorites
75
+ end
76
+ end
105
77
 
106
- def respond_to? m, include_private = false
107
- super || m.to_s[/count_(.+)_favoritors/] || m.to_s[/(.+)_favoritors/]
108
- end
78
+ def favoritors_by_type_count(favoritor_type, options = {})
79
+ if options.key?(:multiple_scopes) == false
80
+ options[:parameter] = favoritor_type
81
+ validate_scopes(__method__, options)
82
+ elsif options[:multiple_scopes]
83
+ results = {}
84
+ options[:scope].each do |scope|
85
+ results[scope] = favorited.unblocked.send(scope + '_list')
86
+ .for_favoritor_type(favoritor_type).count
87
+ end
88
+ results
89
+ else
90
+ favorited.unblocked.send(options[:scope] + '_list')
91
+ .for_favoritor_type(favoritor_type).count
92
+ end
93
+ end
109
94
 
110
- def blocked_favoritors_count options = {}
111
- if options.has_key?(:multiple_scopes) == false
112
- validate_scopes __method__, options
113
- elsif options[:multiple_scopes]
114
- results = {}
115
- options[:scope].each do |scope|
116
- results[scope] = favorited.blocked.send(scope + '_list').count
117
- end
118
- return results
119
- else
120
- return favorited.blocked.send(options[:scope] + '_list').count
121
- end
122
- end
95
+ # Allows magic names on favoritors_by_type
96
+ # e.g. user_favoritors == favoritors_by_type 'User'
97
+ # Allows magic names on favoritors_by_type_count
98
+ # e.g. count_user_favoritors == favoritors_by_type_count 'User'
99
+ def method_missing(method, *args)
100
+ if method.to_s[/count_(.+)_favoritors/]
101
+ favoritors_by_type_count $1.singularize.classify
102
+ elsif method.to_s[/(.+)_favoritors/]
103
+ favoritors_by_type $1.singularize.classify
104
+ elsif ActsAsFavoritor.configuration.cache &&
105
+ method.to_s[/favoritable_(.+)_score/]
106
+ favoritable_score[$1.singularize.classify]
107
+ elsif ActsAsFavoritor.configuration.cache &&
108
+ method.to_s[/favoritable_(.+)_total/]
109
+ favoritable_total[$1.singularize.classify]
110
+ else
111
+ super
112
+ end
113
+ end
123
114
 
124
- # Returns the favorited records scoped
125
- def favoritors_scoped options = {}
126
- if options.has_key?(:multiple_scopes) == false
127
- validate_scopes __method__, options
128
- elsif options[:multiple_scopes]
129
- results = {}
130
- options[:scope].each do |scope|
131
- results[scope] = favorited.send(scope + '_list').includes :favoritor
132
- end
133
- return results
134
- else
135
- return favorited.send(options[:scope] + '_list').includes :favoritor
136
- end
137
- end
115
+ def respond_to_missing?(method, include_private = false)
116
+ super || method.to_s[/count_(.+)_favoritors/] ||
117
+ method.to_s[/(.+)_favoritors/]
118
+ end
138
119
 
139
- def favoritors options = {}
140
- if options.has_key?(:multiple_scopes) == false
141
- validate_scopes __method__, options
142
- elsif options[:multiple_scopes]
143
- results = {}
144
- options[:scope].each do |scope|
145
- favoritors_scope = favoritors_scoped(scope: scope, multiple_scopes: false).unblocked
146
- favoritors_scope = apply_options_to_scope favoritors_scope, options
147
- results[scope] = favoritors_scope.to_a.collect{ |f| f.favoritor }
148
- end
149
- return results
150
- else
151
- favoritors_scope = favoritors_scoped(scope: options[:scope], multiple_scopes: false).unblocked
152
- favoritors_scope = apply_options_to_scope favoritors_scope, options
153
- return favoritors_scope.to_a.collect{ |f| f.favoritor }
154
- end
155
- end
120
+ def blocked_favoritors_count(options = {})
121
+ if options.key?(:multiple_scopes) == false
122
+ validate_scopes(__method__, options)
123
+ elsif options[:multiple_scopes]
124
+ results = {}
125
+ options[:scope].each do |scope|
126
+ results[scope] = favorited.blocked.send(scope + '_list').count
127
+ end
128
+ results
129
+ else
130
+ favorited.blocked.send(options[:scope] + '_list').count
131
+ end
132
+ end
156
133
 
157
- def blocks options = {}
158
- if options.has_key?(:multiple_scopes) == false
159
- validate_scopes __method__, options
160
- elsif options[:multiple_scopes]
161
- results = {}
162
- options[:scope].each do |scope|
163
- blocked_favoritors_scope = favoritors_scoped(scope: scope, multiple_scopes: false).blocked
164
- blocked_favoritors_scope = apply_options_to_scope blocked_favoritors_scope, options
165
- results[scope] = blocked_favoritors_scope.to_a.collect{ |f| f.favoritor }
166
- end
167
- return results
168
- else
169
- blocked_favoritors_scope = favoritors_scoped(scope: options[:scope], multiple_scopes: false).blocked
170
- blocked_favoritors_scope = apply_options_to_scope blocked_favoritors_scope, options
171
- return blocked_favoritors_scope.to_a.collect{ |f| f.favoritor }
172
- end
173
- end
134
+ # Returns the favorited records scoped.
135
+ def favoritors_scoped(options = {})
136
+ if options.key?(:multiple_scopes) == false
137
+ validate_scopes(__method__, options)
138
+ elsif options[:multiple_scopes]
139
+ results = {}
140
+ options[:scope].each do |scope|
141
+ results[scope] = favorited.send(scope + '_list')
142
+ .includes(:favoritor)
143
+ end
144
+ results
145
+ else
146
+ favorited.send(options[:scope] + '_list').includes(:favoritor)
147
+ end
148
+ end
174
149
 
175
- # Returns true if the current instance has blocked the passed record
176
- # Returns false if the current instance has not blocked the passed record
177
- def blocked? favoritor, options = {}
178
- if options.has_key?(:multiple_scopes) == false
179
- options[:parameter] = favoritor
180
- validate_scopes __method__, options
181
- elsif options[:multiple_scopes]
182
- results = {}
183
- options[:scope].each do |scope|
184
- results[scope] = favorited.blocked.send(scope + '_list').for_favoritor(favoritor).first.present?
185
- end
186
- return results
187
- else
188
- return favorited.blocked.send(options[:scope] + '_list').for_favoritor(favoritor).first.present?
189
- end
190
- end
150
+ def favoritors(options = {})
151
+ if options.key?(:multiple_scopes) == false
152
+ validate_scopes(__method__, options)
153
+ elsif options[:multiple_scopes]
154
+ results = {}
155
+ options[:scope].each do |scope|
156
+ favoritors_scope = favoritors_scoped(
157
+ scope: scope, multiple_scopes: false
158
+ ).unblocked
159
+ favoritors_scope = apply_options_to_scope(
160
+ favoritors_scope, options
161
+ )
162
+ results[scope] = favoritors_scope.to_a.collect(&:favoritor)
163
+ end
164
+ results
165
+ else
166
+ favoritors_scope = favoritors_scoped(
167
+ scope: options[:scope], multiple_scopes: false
168
+ ).unblocked
169
+ favoritors_scope = apply_options_to_scope(
170
+ favoritors_scope, options
171
+ )
172
+ favoritors_scope.to_a.collect(&:favoritor)
173
+ end
174
+ end
191
175
 
192
- # Returns true if the current instance is favorited by the passed record
193
- # Returns false if the current instance is blocked by the passed record or no favorite is found
194
- def favorited_by? favoritor, options = {}
195
- if options.has_key?(:multiple_scopes) == false
196
- options[:parameter] = favoritor
197
- validate_scopes __method__, options
198
- elsif options[:multiple_scopes]
199
- results = {}
200
- options[:scope].each do |scope|
201
- results[scope] = favorited.unblocked.send(scope + '_list').for_favoritor(favoritor).first.present?
202
- end
203
- return results
204
- else
205
- return favorited.unblocked.send(options[:scope] + '_list').for_favoritor(favoritor).first.present?
206
- end
207
- end
176
+ def blocks(options = {})
177
+ if options.key?(:multiple_scopes) == false
178
+ validate_scopes(__method__, options)
179
+ elsif options[:multiple_scopes]
180
+ results = {}
181
+ options[:scope].each do |scope|
182
+ blocked_favoritors_scope = favoritors_scoped(
183
+ scope: scope, multiple_scopes: false
184
+ ).blocked
185
+ blocked_favoritors_scope = apply_options_to_scope(
186
+ blocked_favoritors_scope, options
187
+ )
188
+ results[scope] = blocked_favoritors_scope.to_a.collect(&:favoritor)
189
+ end
190
+ results
191
+ else
192
+ blocked_favoritors_scope = favoritors_scoped(
193
+ scope: options[:scope], multiple_scopes: false
194
+ ).blocked
195
+ blocked_favoritors_scope = apply_options_to_scope(
196
+ blocked_favoritors_scope, options
197
+ )
198
+ blocked_favoritors_scope.to_a.collect(&:favoritor)
199
+ end
200
+ end
208
201
 
209
- # Returns true if this instance has been favorited by the object passed as an argument.
210
- # Returns nil if this instance has not been favorited by the object passed as an argument.
211
- # Returns false if this instance has blocked the object passed as an argument.
212
- def favoritor_type favoritor, options = {}
213
- if options.has_key?(:multiple_scopes) == false
214
- options[:parameter] = favoritor
215
- validate_scopes __method__, options
216
- elsif options[:multiple_scopes]
217
- results = {}
218
- options[:scope].each do |scope|
219
- if favorited.unblocked.send(scope + '_list').for_favoritor(favoritor).first.present?
220
- results[scope] = true
221
- elsif favorited.blocked.send(scope + '_list').for_favoritor(favoritor).first.present?
222
- results[scope] = false
223
- else
224
- results[scope] = nil
225
- end
226
- end
227
- return results
228
- else
229
- if favorited.unblocked.send(options[:scope] + '_list').for_favoritor(favoritor).first.present?
230
- return true
231
- elsif favorited.blocked.send(options[:scope] + '_list').for_favoritor(favoritor).first.present?
232
- return false
233
- else
234
- return nil
235
- end
236
- end
237
- end
202
+ # Returns true if the current instance has blocked the passed record.
203
+ # Returns false if the current instance has not blocked the passed record.
204
+ def blocked?(favoritor, options = {})
205
+ if options.key?(:multiple_scopes) == false
206
+ options[:parameter] = favoritor
207
+ validate_scopes(__method__, options)
208
+ elsif options[:multiple_scopes]
209
+ results = {}
210
+ options[:scope].each do |scope|
211
+ results[scope] = favorited.blocked.send(scope + '_list')
212
+ .for_favoritor(favoritor).first.present?
213
+ end
214
+ results
215
+ else
216
+ favorited.blocked.send(options[:scope] + '_list')
217
+ .for_favoritor(favoritor).first.present?
218
+ end
219
+ end
238
220
 
239
- def block favoritor, options = {}
240
- if options.has_key?(:multiple_scopes) == false
241
- options[:parameter] = favoritor
242
- validate_scopes __method__, options
243
- elsif options[:multiple_scopes]
244
- results = {}
245
- options[:scope].each do |scope|
246
- results[scope] = get_favorite_for(favoritor, scope: scope, multiple_scopes: false) ? block_existing_favorite(favoritor, scope: scope, multiple_scopes: false) : block_future_favorite(favoritor, scope: scope, multiple_scopes: false)
247
- end
248
- return results
249
- else
250
- return get_favorite_for(favoritor, scope: options[:scope], multiple_scopes: false) ? block_existing_favorite(favoritor, scope: options[:scope], multiple_scopes: false) : block_future_favorite(favoritor, scope: options[:scope], multiple_scopes: false)
251
- end
252
- end
221
+ # Returns true if the current instance is favorited by the passed record.
222
+ # Returns false if the current instance is blocked by the passed record or
223
+ # no favorite is found.
224
+ def favorited_by?(favoritor, options = {})
225
+ if options.key?(:multiple_scopes) == false
226
+ options[:parameter] = favoritor
227
+ validate_scopes(__method__, options)
228
+ elsif options[:multiple_scopes]
229
+ results = {}
230
+ options[:scope].each do |scope|
231
+ results[scope] = favorited.unblocked.send(scope + '_list')
232
+ .for_favoritor(favoritor).first.present?
233
+ end
234
+ results
235
+ else
236
+ favorited.unblocked.send(options[:scope] + '_list')
237
+ .for_favoritor(favoritor).first.present?
238
+ end
239
+ end
253
240
 
254
- def unblock favoritor, options = {}
255
- if options.has_key?(:multiple_scopes) == false
256
- options[:parameter] = favoritor
257
- validate_scopes __method__, options
258
- elsif options[:multiple_scopes]
259
- results = {}
260
- options[:scope].each do |scope|
261
- results[scope] = get_favorite_for(favoritor, scope: scope, multiple_scopes: false)&.update_attribute :blocked, false
262
- end
263
- return results
264
- else
265
- return get_favorite_for(favoritor, scope: options[:scope], multiple_scopes: false)&.update_attribute :blocked, false
266
- end
241
+ # Returns true if this instance has been favorited by the object passed as
242
+ # an argument. Returns nil if this instance has not been favorited by the
243
+ # object passed as an argument. Returns false if this instance has blocked
244
+ # the object passed as an argument.
245
+ def favoritor_type(favoritor, options = {})
246
+ if options.key?(:multiple_scopes) == false
247
+ options[:parameter] = favoritor
248
+ validate_scopes(__method__, options)
249
+ elsif options[:multiple_scopes]
250
+ results = {}
251
+ options[:scope].each do |scope|
252
+ if favorited.unblocked.send(scope + '_list')
253
+ .for_favoritor(favoritor).first.present?
254
+ results[scope] = true
255
+ elsif favorited.blocked.send(scope + '_list')
256
+ .for_favoritor(favoritor).first.present?
257
+ results[scope] = false
258
+ else
259
+ results[scope] = nil
267
260
  end
261
+ end
262
+ results
263
+ elsif favorited.unblocked.send(options[:scope] + '_list')
264
+ .for_favoritor(favoritor).first.present?
265
+ true
266
+ elsif favorited.blocked.send(options[:scope] + '_list')
267
+ .for_favoritor(favoritor).first.present?
268
+ false
269
+ end
270
+ end
268
271
 
269
- def get_favorite_for favoritor, options = {}
270
- if options.has_key?(:multiple_scopes) == false &&
271
- options[:parameter] = favoritor
272
- validate_scopes __method__, options
273
- elsif options[:multiple_scopes]
274
- results = {}
275
- options[:scope].each do |scope|
276
- results[scope] = favorited.send(scope + '_list').for_favoritor(favoritor).first
277
- end
278
- return results
279
- else
280
- return favorited.send(options[:scope] + '_list').for_favoritor(favoritor).first
281
- end
282
- end
272
+ def block(favoritor, options = {})
273
+ if options.key?(:multiple_scopes) == false
274
+ options[:parameter] = favoritor
275
+ validate_scopes(__method__, options)
276
+ elsif options[:multiple_scopes]
277
+ results = {}
278
+ options[:scope].each do |scope|
279
+ favorite = get_favorite_for(
280
+ favoritor, scope: scope, multiple_scopes: false
281
+ )
282
+ results[scope] = if favorite
283
+ block_existing_favorite(
284
+ favoritor, scope: scope, multiple_scopes: false
285
+ )
286
+ else
287
+ block_future_favorite(
288
+ favoritor, scope: scope, multiple_scopes: false
289
+ )
290
+ end
291
+ end
292
+ results
293
+ else
294
+ favorite = get_favorite_for(
295
+ favoritor, scope: options[:scope], multiple_scopes: false
296
+ )
297
+ if favorite
298
+ block_existing_favorite(
299
+ favoritor, scope: options[:scope], multiple_scopes: false
300
+ )
301
+ else
302
+ block_future_favorite(
303
+ favoritor, scope: options[:scope], multiple_scopes: false
304
+ )
305
+ end
306
+ end
307
+ end
283
308
 
284
- private
309
+ def unblock(favoritor, options = {})
310
+ if options.key?(:multiple_scopes) == false
311
+ options[:parameter] = favoritor
312
+ validate_scopes(__method__, options)
313
+ elsif options[:multiple_scopes]
314
+ results = {}
315
+ options[:scope].each do |scope|
316
+ results[scope] = get_favorite_for(
317
+ favoritor, scope: scope, multiple_scopes: false
318
+ )&.update(blocked: false)
319
+ end
320
+ results
321
+ else
322
+ get_favorite_for(
323
+ favoritor, scope: options[:scope], multiple_scopes: false
324
+ )&.update(blocked: false)
325
+ end
326
+ end
285
327
 
286
- def block_future_favorite favoritor, options = {}
287
- if options.has_key?(:multiple_scopes) == false
288
- options[:parameter] = favoritor
289
- validate_scopes __method__, options
290
- elsif options[:multiple_scopes]
291
- results = {}
292
- options[:scope].each do |scope|
293
- results[scope] = Favorite.create favoritable: self, favoritor: favoritor, blocked: true, scope: scope
294
- end
295
- return results
296
- else
297
- return Favorite.create favoritable: self, favoritor: favoritor, blocked: true, scope: options[:scope]
298
- end
299
- end
328
+ def get_favorite_for(favoritor, options = {})
329
+ if options.key?(:multiple_scopes) == false
330
+ options[:parameter] = favoritor
331
+ validate_scopes(__method__, options)
332
+ elsif options[:multiple_scopes]
333
+ results = {}
334
+ options[:scope].each do |scope|
335
+ results[scope] = favorited.send(scope + '_list')
336
+ .for_favoritor(favoritor).first
337
+ end
338
+ results
339
+ else
340
+ favorited.send(options[:scope] + '_list').for_favoritor(favoritor)
341
+ .first
342
+ end
343
+ end
300
344
 
301
- def block_existing_favorite favoritor, options = {}
302
- if options.has_key?(:multiple_scopes) == false
303
- options[:parameter] = favoritor
304
- validate_scopes __method__, options
305
- elsif options[:multiple_scopes]
306
- results = {}
307
- options[:scope].each do |scope|
308
- results[scope] = get_favorite_for(favoritor, scope: scope, multiple_scopes: false).block!
309
- end
310
- return results
311
- else
312
- return get_favorite_for(favoritor, scope: options[:scope], multiple_scopes: false).block!
313
- end
314
- end
345
+ private
315
346
 
347
+ def block_future_favorite(favoritor, options = {})
348
+ if options.key?(:multiple_scopes) == false
349
+ options[:parameter] = favoritor
350
+ validate_scopes(__method__, options)
351
+ elsif options[:multiple_scopes]
352
+ results = {}
353
+ options[:scope].each do |scope|
354
+ results[scope] = Favorite.create(
355
+ favoritable: self,
356
+ favoritor: favoritor,
357
+ blocked: true,
358
+ scope: scope
359
+ )
360
+ end
361
+ results
362
+ else
363
+ Favorite.create(
364
+ favoritable: self,
365
+ favoritor: favoritor,
366
+ blocked: true,
367
+ scope: options[:scope]
368
+ )
316
369
  end
370
+ end
317
371
 
372
+ def block_existing_favorite(favoritor, options = {})
373
+ if options.key?(:multiple_scopes) == false
374
+ options[:parameter] = favoritor
375
+ validate_scopes(__method__, options)
376
+ elsif options[:multiple_scopes]
377
+ results = {}
378
+ options[:scope].each do |scope|
379
+ results[scope] = get_favorite_for(
380
+ favoritor, scope: scope, multiple_scopes: false
381
+ ).block!
382
+ end
383
+ results
384
+ else
385
+ get_favorite_for(
386
+ favoritor, scope: options[:scope], multiple_scopes: false
387
+ ).block!
388
+ end
389
+ end
318
390
  end
391
+ end
319
392
  end