acts_as_favoritor 2.0.1 → 2.1.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 +4 -4
- data/LICENSE +0 -0
- data/README.md +18 -57
- data/lib/acts_as_favoritor.rb +8 -8
- data/lib/acts_as_favoritor/configuration.rb +22 -23
- data/lib/acts_as_favoritor/favoritable.rb +365 -292
- data/lib/acts_as_favoritor/favorite_scopes.rb +66 -59
- data/lib/acts_as_favoritor/favoritor.rb +551 -425
- data/lib/acts_as_favoritor/favoritor_lib.rb +39 -49
- data/lib/acts_as_favoritor/railtie.rb +9 -9
- data/lib/acts_as_favoritor/version.rb +3 -3
- data/lib/generators/acts_as_favoritor_generator.rb +29 -29
- data/lib/generators/templates/initializer.rb +9 -9
- data/lib/generators/templates/migration.rb.erb +16 -14
- data/lib/generators/templates/model.rb +9 -9
- metadata +35 -40
- data/CHANGELOG.md +0 -95
- data/lib/generators/templates/README.md +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ff8778bb55338480286174f66774251914fa4ef19aabb79dd5588d3c6bf40dc
|
4
|
+
data.tar.gz: 239c8bfbacb746ac449a4682ec731adf4c85da8493d983bd210627c0ebd105bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
[](https://badge.fury.io/rb/acts_as_favoritor)
|
3
|
+
[](https://badge.fury.io/rb/acts_as_favoritor) 
|
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
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
24
|
-
* [To Do](#to-do)
|
23
|
+
* [To do](#to-do)
|
25
24
|
* [Contributing](#contributing)
|
26
|
-
|
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
|
-
|
67
|
+
acts_as_favoritable
|
71
68
|
end
|
72
69
|
|
73
70
|
class Book < ActiveRecord::Base
|
74
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
338
|
+
5. Run RuboCop
|
342
339
|
|
343
|
-
|
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
|
344
|
+
## To do
|
354
345
|
|
355
|
-
[
|
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](
|
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.
|
data/lib/acts_as_favoritor.rb
CHANGED
@@ -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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
4
|
+
module Favoritable
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
3
8
|
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
16
|
+
return unless ActsAsFavoritor.configuration.cache
|
17
|
+
serialize :favoritable_score, Hash
|
18
|
+
serialize :favoritable_total, Hash
|
19
|
+
end
|
20
|
+
end
|
13
21
|
|
14
|
-
|
15
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
107
|
-
|
108
|
-
|
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
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
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
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
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
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
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
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
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
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
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
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
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
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
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
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
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
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
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
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
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
|
-
|
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
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
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
|
-
|
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
|