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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82f55876158cb217b9c1a1b4509ca4801a8c342a
4
- data.tar.gz: 63af11cb03f00c80de75a7d3ef497bb79ba30365
3
+ metadata.gz: 14b98ac10e904c740a31afb1a221de5db6d0bca2
4
+ data.tar.gz: bb136d5035521b41db3cd888a96bcc991c405862
5
5
  SHA512:
6
- metadata.gz: 60dea8ed48a7852a799daf4b755f4cf9feb68275180c5180db1e321bf34a4db62742084eafe26b1d7e2417db761ae8abe04a5689f2f5b3a03ae6e8ab15bd6ff5
7
- data.tar.gz: 411342b41fb29a9e5d7d72c99d16ff0d41e754d2f3709066fc7330f69c54f2e4487d17e041fb634bbd6d09f37b344422160d75505ebf96cc20f6c490c43e4096
6
+ metadata.gz: ee95fe8c8bd1528dea8a57f6fa9c992c5ff449c140fbdf03f6949514e7a6e10d99e1775fb3864c902c6b0090d74ee2c273929923c6468882db2e988e3a42c8ab
7
+ data.tar.gz: 114e8d8ba89bc8f4f0257847eb7bd8720367088e32c94ea90569186a28dc96ce36f7534ffdf8600c9295706a144bf35c2bf8a3541d91a41e8a6467aa4a1ab6a5
data/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@
4
4
 
5
5
  * nothing yet
6
6
 
7
+ ### 1.3.2 - 2017-08-31
8
+
9
+ * features
10
+ * add caching functionality to `acts_as_favoritor` & `acts_as_favoritable` model
11
+
7
12
  ### 1.2.2 - 2017-08-24
8
13
 
9
14
  * bugfixes
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
@@ -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 = ['Jonas Hübotter']
10
+ gem.authors = 'Slooob'
11
11
  gem.email = 'developer@slooob.com'
12
- gem.homepage = 'https://github.com/slooob/acts_as_favoritor'
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")
@@ -9,6 +9,15 @@ module ActsAsFavoritor
9
9
  end
10
10
  end
11
11
 
12
+ def self.cache
13
+ config = get_config
14
+ if config&.key :cache
15
+ config[:cache]
16
+ else
17
+ false
18
+ end
19
+ end
20
+
12
21
 
13
22
  private
14
23
 
@@ -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
@@ -1,5 +1,5 @@
1
1
  module ActsAsFavoritor
2
2
 
3
- VERSION = '1.2.2'
3
+ VERSION = '1.3.2'
4
4
 
5
5
  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.2.2
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
- - Jonas Hübotter
7
+ - Slooob
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-24 00:00:00.000000000 Z
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://github.com/slooob/acts_as_favoritor
178
+ homepage: https://developer.slooob.com/open-source
179
179
  licenses:
180
180
  - MIT
181
181
  metadata: {}