acts_as_favoritor 1.2.2 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +33 -2
- data/Rakefile +0 -9
- data/acts_as_favoritor.gemspec +2 -2
- data/lib/acts_as_favoritor/configuration.rb +9 -0
- data/lib/acts_as_favoritor/favoritable.rb +2 -0
- data/lib/acts_as_favoritor/favoritor.rb +30 -0
- data/lib/acts_as_favoritor/version.rb +1 -1
- data/lib/generators/acts_as_favoritor_generator.rb +1 -0
- data/lib/generators/templates/acts_as_favoritor.yml.erb +3 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14b98ac10e904c740a31afb1a221de5db6d0bca2
|
4
|
+
data.tar.gz: bb136d5035521b41db3cd888a96bcc991c405862
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee95fe8c8bd1528dea8a57f6fa9c992c5ff449c140fbdf03f6949514e7a6e10d99e1775fb3864c902c6b0090d74ee2c273929923c6468882db2e988e3a42c8ab
|
7
|
+
data.tar.gz: 114e8d8ba89bc8f4f0257847eb7bd8720367088e32c94ea90569186a28dc96ce36f7534ffdf8600c9295706a144bf35c2bf8a3541d91a41e8a6467aa4a1ab6a5
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# `acts_as_favoritor` - Add Favorites to your Rails app
|
2
2
|
|
3
|
-
<img src="https://travis-ci.org/slooob/acts_as_favoritor.svg?branch=master" />
|
3
|
+
<img src="https://travis-ci.org/slooob/acts_as_favoritor.svg?branch=master" /> [![Gem Version](https://badge.fury.io/rb/acts_as_favoritor.svg)](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
|
|
@@ -17,6 +17,7 @@ You are able to differentiate followers, favorites, watchers, votes and whatever
|
|
17
17
|
* [`acts_as_favoritable` methods](#acts_as_favoritable-methods)
|
18
18
|
* [`Favorite` model](#favorite-model)
|
19
19
|
* [Scopes](#scopes)
|
20
|
+
* [Caching](#caching)
|
20
21
|
* [Configuration](#configuration)
|
21
22
|
* [Testing](#testing)
|
22
23
|
* [Test Coverage](#test-coverage)
|
@@ -55,6 +56,8 @@ Now run the generator:
|
|
55
56
|
|
56
57
|
You can set your default scope by passing `--scope custom_scope`. Learn more about scopes [here](#scopes).
|
57
58
|
|
59
|
+
If you specify `--cache true`, `acts_as_favoritor` will be using cache. Learn more about caching [here](#caching).
|
60
|
+
|
58
61
|
You can skip the creation of a configuration file by passing `--skip_configuration`. Learn more about configuring `acts_as_favoritor` [here](#configuration).
|
59
62
|
|
60
63
|
To wrap things up, migrate the changes into your database:
|
@@ -269,6 +272,35 @@ Favorite.watching_list
|
|
269
272
|
Favorite.all_list
|
270
273
|
```
|
271
274
|
|
275
|
+
### Caching
|
276
|
+
|
277
|
+
When you set the option `cache` in `config/acts_as_favoritor` to true, you are able to cache the amount of favorites/favoritables an instance has regarding a scope.
|
278
|
+
|
279
|
+
For that you need to add some database columns:
|
280
|
+
|
281
|
+
*acts_as_favoritor*
|
282
|
+
|
283
|
+
```ruby
|
284
|
+
add_column :users, :favoritor_cache, :text, default: {}.to_yaml, null: false
|
285
|
+
```
|
286
|
+
|
287
|
+
*acts_as_favoritable*
|
288
|
+
|
289
|
+
```ruby
|
290
|
+
add_column :users, :favoritable_cache, :text, default: {}.to_yaml, null: false
|
291
|
+
add_column :books, :favoritable_cache, :text, default: {}.to_yaml, null: false
|
292
|
+
```
|
293
|
+
|
294
|
+
Caches are stored as hashes with scopes as keys:
|
295
|
+
|
296
|
+
```ruby
|
297
|
+
user.favoritor_cache # => { favorite: 1 }
|
298
|
+
second_user.favoritable_cache # => { follow: 1 }
|
299
|
+
book.favoritable_cache # => { favorite: 1 }
|
300
|
+
```
|
301
|
+
|
302
|
+
**Note:** Only scopes who have favorites are included.
|
303
|
+
|
272
304
|
---
|
273
305
|
|
274
306
|
## Configuration
|
@@ -314,7 +346,6 @@ Test coverage can be calculated using SimpleCov. Make sure you have the [simplec
|
|
314
346
|
## To Do
|
315
347
|
|
316
348
|
* Adding magic methods for scopes, e.g.: `user.follow second_user` instead of `user.favorite second_user, scope: [:follow]`
|
317
|
-
* Allow for caching of counters in the database table of `acts_as_favoritable`.
|
318
349
|
|
319
350
|
---
|
320
351
|
|
data/Rakefile
CHANGED
@@ -14,12 +14,3 @@ Rake::TestTask.new(:test) do |t|
|
|
14
14
|
t.pattern = 'test/**/*_test.rb'
|
15
15
|
t.verbose = true
|
16
16
|
end
|
17
|
-
|
18
|
-
desc 'Generate documentation for the acts_as_favoritor gem.'
|
19
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
20
|
-
rdoc.rdoc_dir = 'rdoc'
|
21
|
-
rdoc.title = 'Acts As Favoritor'
|
22
|
-
rdoc.main = 'README.rdoc'
|
23
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
24
|
-
rdoc.rdoc_files.include('README.rdoc', 'lib/**/*.rb')
|
25
|
-
end
|
data/acts_as_favoritor.gemspec
CHANGED
@@ -7,9 +7,9 @@ Gem::Specification.new do |gem|
|
|
7
7
|
gem.platform = Gem::Platform::RUBY
|
8
8
|
gem.summary = 'A Rubygem to add Favorite, Follow, Vote, etc. functionality to ActiveRecord models'
|
9
9
|
gem.description = '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. You are able to differentiate followers, favorites, watchers, votes and whatever else you can imagine through a single relationship. This is accomplished by a double polymorphic relationship on the Favorite model. There is also built in support for blocking/un-blocking favorite records.'
|
10
|
-
gem.authors =
|
10
|
+
gem.authors = 'Slooob'
|
11
11
|
gem.email = 'developer@slooob.com'
|
12
|
-
gem.homepage = 'https://
|
12
|
+
gem.homepage = 'https://developer.slooob.com/open-source'
|
13
13
|
gem.license = 'MIT'
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split("\n")
|
@@ -10,6 +10,8 @@ module ActsAsFavoritor #:nodoc:
|
|
10
10
|
has_many :favorited, as: :favoritable, dependent: :destroy, class_name: 'Favorite'
|
11
11
|
include ActsAsFavoritor::Favoritable::InstanceMethods
|
12
12
|
include ActsAsFavoritor::FavoritorLib
|
13
|
+
|
14
|
+
serialize :favoritable_cache, Hash if ActsAsFavoritor.cache
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
@@ -10,6 +10,8 @@ module ActsAsFavoritor #:nodoc:
|
|
10
10
|
has_many :favorites, as: :favoritor, dependent: :destroy
|
11
11
|
include ActsAsFavoritor::Favoritor::InstanceMethods
|
12
12
|
include ActsAsFavoritor::FavoritorLib
|
13
|
+
|
14
|
+
serialize :favoritor_cache, Hash if ActsAsFavoritor.cache
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
@@ -101,6 +103,12 @@ module ActsAsFavoritor #:nodoc:
|
|
101
103
|
elsif options[:multiple_scopes]
|
102
104
|
results = {}
|
103
105
|
options[:scope].each do |scope|
|
106
|
+
if ActsAsFavoritor.cache
|
107
|
+
self.favoritor_cache[scope] = self.favoritor_cache[scope] + 1 || 1
|
108
|
+
self.save!
|
109
|
+
favoritable.favoritable_cache[scope] = favoritable.favoritable_cache[scope] + 1 || 1
|
110
|
+
favoritable.save!
|
111
|
+
end
|
104
112
|
if self != favoritable && scope != 'all'
|
105
113
|
params = {favoritable_id: favoritable.id, favoritable_type: parent_class_name(favoritable), scope: scope}
|
106
114
|
results[scope] = favorites.where(params).first_or_create!
|
@@ -108,6 +116,12 @@ module ActsAsFavoritor #:nodoc:
|
|
108
116
|
end
|
109
117
|
return results
|
110
118
|
else
|
119
|
+
if ActsAsFavoritor.cache
|
120
|
+
self.favoritor_cache[options[:scope]] = self.favoritor_cache[options[:scope]] + 1 || 1
|
121
|
+
self.save!
|
122
|
+
favoritable.favoritable_cache[options[:scope]] = favoritable.favoritable_cache[options[:scope]] + 1 || 1
|
123
|
+
favoritable.save!
|
124
|
+
end
|
111
125
|
if self != favoritable && options[:scope] != 'all'
|
112
126
|
params = {favoritable_id: favoritable.id, favoritable_type: parent_class_name(favoritable), scope: options[:scope]}
|
113
127
|
return favorites.where(params).first_or_create!
|
@@ -123,12 +137,28 @@ module ActsAsFavoritor #:nodoc:
|
|
123
137
|
elsif options[:multiple_scopes]
|
124
138
|
results = {}
|
125
139
|
options[:scope].each do |scope|
|
140
|
+
if ActsAsFavoritor.cache
|
141
|
+
self.favoritor_cache[scope] = self.favoritor_cache[scope] - 1
|
142
|
+
self.favoritor_cache.delete(scope) unless self.favoritor_cache[scope] > 0
|
143
|
+
self.save!
|
144
|
+
favoritable.favoritable_cache[scope] = favoritable.favoritable_cache[scope] - 1
|
145
|
+
favoritable.favoritable_cache.delete(scope) unless favoritable.favoritable_cache[scope] > 0
|
146
|
+
favoritable.save!
|
147
|
+
end
|
126
148
|
if favorite = get_favorite(favoritable, scope: scope, multiple_scopes: false)
|
127
149
|
results[scope] = favorite.destroy
|
128
150
|
end
|
129
151
|
end
|
130
152
|
return results
|
131
153
|
else
|
154
|
+
if ActsAsFavoritor.cache
|
155
|
+
self.favoritor_cache[options[:scope]] = self.favoritor_cache[options[:scope]] - 1
|
156
|
+
self.favoritor_cache.delete(options[:scope]) unless self.favoritor_cache[options[:scope]] > 0
|
157
|
+
self.save!
|
158
|
+
favoritable.favoritable_cache[options[:scope]] = favoritable.favoritable_cache[options[:scope]] - 1
|
159
|
+
favoritable.favoritable_cache.delete(options[:scope]) unless favoritable.favoritable_cache[options[:scope]] > 0
|
160
|
+
favoritable.save!
|
161
|
+
end
|
132
162
|
if favorite = get_favorite(favoritable, scope: options[:scope], multiple_scopes: false)
|
133
163
|
return favorite.destroy
|
134
164
|
end
|
@@ -8,6 +8,7 @@ class ActsAsFavoritorGenerator < Rails::Generators::Base
|
|
8
8
|
source_root File.join File.dirname(__FILE__), 'templates'
|
9
9
|
desc 'Install acts_as_favoritor'
|
10
10
|
class_option :scope, desc: 'Specify your default scope. Learn more about scopes here: https://github.com/slooob/acts_as_favoritor#scopes', type: :string, default: 'favorite', aliases: '-s'
|
11
|
+
class_option :cache, desc: 'Enable caching. Learn more about caching here: https://github.com/slooob/acts_as_favoritor#caching', type: :boolean, default: false, aliases: '-c'
|
11
12
|
class_option :skip_configuration, desc: 'Skip the creation of the configuration file. Learn more about configuring acts_as_favoritor here: https://github.com/slooob/acts_as_favoritor#configuration', type: :boolean, default: false
|
12
13
|
|
13
14
|
def self.next_migration_number dirname
|
@@ -4,3 +4,6 @@
|
|
4
4
|
|
5
5
|
# Specify your default scope. Learn more about scopes here: https://github.com/slooob/acts_as_favoritor#scopes
|
6
6
|
default_scope: <%= options[:scope] %>
|
7
|
+
|
8
|
+
# Enable caching. Learn more about caching here: https://github.com/slooob/acts_as_favoritor#caching
|
9
|
+
cache: <%= options[:cache] %>
|
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: 1.
|
4
|
+
version: 1.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Slooob
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -175,7 +175,7 @@ files:
|
|
175
175
|
- test/favorite_test.rb
|
176
176
|
- test/schema.rb
|
177
177
|
- test/test_helper.rb
|
178
|
-
homepage: https://
|
178
|
+
homepage: https://developer.slooob.com/open-source
|
179
179
|
licenses:
|
180
180
|
- MIT
|
181
181
|
metadata: {}
|